add audit log repo to document

This commit is contained in:
Mads Rasmussen
2024-04-30 11:38:37 +02:00
parent c63461b104
commit 47bf0f8f7a
5 changed files with 111 additions and 0 deletions

View File

@@ -0,0 +1 @@
export { UmbDocumentAuditLogRepository } from './repository/index.js';

View File

@@ -0,0 +1,34 @@
import type { UmbAuditLogRequestArgs } from '../types.js';
import { UmbDocumentAuditLogServerDataSource } from './document-audit-log.server.data-source.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
/**
* Repository for the document audit log
* @export
* @class UmbDocumentAuditLogRepository
* @extends {UmbRepositoryBase}
*/
export class UmbDocumentAuditLogRepository extends UmbRepositoryBase {
#dataSource: UmbDocumentAuditLogServerDataSource;
/**
* Creates an instance of UmbDocumentAuditLogRepository.
* @param {UmbControllerHost} host
* @memberof UmbDocumentAuditLogRepository
*/
constructor(host: UmbControllerHost) {
super(host);
this.#dataSource = new UmbDocumentAuditLogServerDataSource(host);
}
/**
* Request the audit log for a document
* @param {UmbAuditLogRequestArgs} args
* @return {*}
* @memberof UmbDocumentAuditLogRepository
*/
async requestAuditLog(args: UmbAuditLogRequestArgs) {
return this.#dataSource.getAuditLog(args);
}
}

View File

@@ -0,0 +1,57 @@
import type { UmbAuditLogRequestArgs } from '../types.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { DocumentService } from '@umbraco-cms/backoffice/external/backend-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
/**
* Server data source for the document audit log
* @export
* @class UmbAuditLogServerDataSource
*/
export class UmbDocumentAuditLogServerDataSource {
#host: UmbControllerHost;
/**
* Creates an instance of UmbAuditLogServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbAuditLogServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Get the audit log for a document
* @param {UmbAuditLogRequestArgs} args
* @return {*}
* @memberof UmbDocumentAuditLogServerDataSource
*/
async getAuditLog(args: UmbAuditLogRequestArgs) {
const { data, error } = await tryExecuteAndNotify(
this.#host,
DocumentService.getDocumentByIdAuditLog({
id: args.unique,
orderDirection: args.orderDirection,
sinceDate: args.sinceDate,
skip: args.skip,
take: args.take,
}),
);
if (data) {
const mappedItems = data.items.map((item) => {
return {
user: item.user ? { unique: item.user.id } : null,
timestamp: item.timestamp,
logType: item.logType,
comment: item.comment,
parameters: item.parameters,
};
});
return { data: { items: mappedItems, total: data.total } };
}
return { error };
}
}

View File

@@ -0,0 +1 @@
export { UmbDocumentAuditLogRepository } from './document-audit-log.repository.js';

View File

@@ -0,0 +1,18 @@
import type { UmbReferenceByUnique } from '@umbraco-cms/backoffice/models';
import type { UmbDirectionType } from '@umbraco-cms/backoffice/utils';
export interface UmbAuditLogRequestArgs {
unique: string;
orderDirection?: UmbDirectionType;
sinceDate?: string;
skip?: number;
take?: number;
}
export interface UmbDocumentAuditLogModel {
user: UmbReferenceByUnique;
timestamp: string;
logType: AuditTypeModel;
comment?: string | null;
parameters?: string | null;
}