Skip to content

Commit

Permalink
feat: audit logs (#3454)
Browse files Browse the repository at this point in the history
  • Loading branch information
neatbyte-vnobis committed Nov 1, 2023
1 parent bf6849c commit ddbf234
Show file tree
Hide file tree
Showing 164 changed files with 7,088 additions and 150 deletions.
1 change: 1 addition & 0 deletions apps/api/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@webiny/api-admin-users-so-ddb": "0.0.0",
"@webiny/api-apw": "0.0.0",
"@webiny/api-apw-scheduler-so-ddb": "0.0.0",
"@webiny/api-audit-logs": "0.0.0",
"@webiny/api-file-manager": "0.0.0",
"@webiny/api-file-manager-ddb": "0.0.0",
"@webiny/api-file-manager-s3": "0.0.0",
Expand Down
4 changes: 3 additions & 1 deletion apps/api/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { createApwGraphQL, createApwPageBuilderContext } from "@webiny/api-apw";
import { createStorageOperations as createApwSaStorageOperations } from "@webiny/api-apw-scheduler-so-ddb";
import { createAco } from "@webiny/api-aco";
import { createAcoPageBuilderContext } from "@webiny/api-page-builder-aco";
import { createAuditLogs } from "@webiny/api-audit-logs";

// Imports plugins created via scaffolding utilities.
import scaffoldsPlugins from "./plugins/scaffolds";
Expand Down Expand Up @@ -119,7 +120,8 @@ export const handler = createHandler({
name: "number-input"
}
});
})
}),
createAuditLogs()
],
http: { debug }
});
5 changes: 5 additions & 0 deletions apps/api/graphql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
{
"path": "../../../packages/api-apw/tsconfig.build.json"
},
{
"path": "../../../packages/api-audit-logs/tsconfig.build.json"
},
{
"path": "../../../packages/api-file-manager/tsconfig.build.json"
},
Expand Down Expand Up @@ -117,6 +120,8 @@
"@webiny/api-admin-users-so-ddb": ["../../../packages/api-admin-users-so-ddb/src"],
"@webiny/api-apw/*": ["../../../packages/api-apw/src/*"],
"@webiny/api-apw": ["../../../packages/api-apw/src"],
"@webiny/api-audit-logs/*": ["../../../packages/api-audit-logs/src/*"],
"@webiny/api-audit-logs": ["../../../packages/api-audit-logs/src"],
"@webiny/api-file-manager/*": ["../../../packages/api-file-manager/src/*"],
"@webiny/api-file-manager": ["../../../packages/api-file-manager/src"],
"@webiny/api-file-manager-ddb/*": ["../../../packages/api-file-manager-ddb/src/*"],
Expand Down
52 changes: 47 additions & 5 deletions packages/api-aco/src/apps/AcoApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import WebinyError from "@webiny/error";
import structuredClone from "@ungap/structured-clone";
import {
AcoContext,
AcoRequestAction,
AcoSearchRecordCrudBase,
CreateSearchRecordParams,
IAcoApp,
IAcoAppModifyFieldCallableCallback,
IAcoAppOnAnyRequest,
IAcoAppOnEntry,
IAcoAppOnEntryList,
IAcoAppParams,
ListSearchRecordsParams,
ListSearchRecordTagsParams,
Expand All @@ -25,35 +29,64 @@ export class AcoApp implements IAcoApp {
public readonly context: AcoContext;
public readonly model: CmsModel;
private readonly fields: CmsModelField[];
private readonly onEntry?: IAcoAppOnEntry;
private readonly onEntryList?: IAcoAppOnEntryList;
private readonly onAnyRequest?: IAcoAppOnAnyRequest;

public get search(): AcoSearchRecordCrudBase {
return {
create: async <TData>(data: CreateSearchRecordParams<TData>) => {
return this.context.aco.search.create<TData>(this.getModel(), data);
await this.execOnAnyRequest("create");
const result = await this.context.aco.search.create<TData>(this.getModel(), data);
if (!this.onEntry) {
return result;
}
return this.onEntry(result);
},
update: async <TData>(id: string, data: SearchRecord<TData>) => {
await this.execOnAnyRequest("update");
/**
* Required to have as any atm as TS is breaking on the return type.
*/
return (await this.context.aco.search.update<TData>(
const result = await this.context.aco.search.update<TData>(
this.getModel(),
id,
data
)) as any;
);
if (!this.onEntry) {
return result;
}
return this.onEntry(result);
},
move: async (id: string, folderId?: string) => {
await this.execOnAnyRequest("move");
return this.context.aco.search.move(this.getModel(), id, folderId);
},
get: async <TData>(id: string) => {
return this.context.aco.search.get<TData>(this.getModel(), id);
await this.execOnAnyRequest("fetch");
const result = await this.context.aco.search.get<TData>(this.getModel(), id);
if (!result || !this.onEntry) {
return result;
}
return this.onEntry(result);
},
list: async <TData>(params: ListSearchRecordsParams) => {
return this.context.aco.search.list<TData>(this.getModel(), params);
await this.execOnAnyRequest("fetch");
const result = await this.context.aco.search.list<TData>(this.getModel(), params);
const onEntryList = this.onEntryList;
if (!onEntryList) {
return result;
}
const [entries, meta] = result;
const items = await onEntryList(entries);
return [items, meta];
},
delete: async (id: string): Promise<Boolean> => {
await this.execOnAnyRequest("delete");
return this.context.aco.search.delete(this.getModel(), id);
},
listTags: async (params: ListSearchRecordTagsParams) => {
await this.execOnAnyRequest("fetch");
return this.context.aco.search.listTags(this.getModel(), params);
}
};
Expand All @@ -73,6 +106,8 @@ export class AcoApp implements IAcoApp {
private constructor(context: AcoContext, params: IAcoAppParams) {
this.context = context;
this.name = params.name;
this.onEntry = params.onEntry;
this.onEntryList = params.onEntryList;
this.model = structuredClone(params.model);
/**
* We can safely define the api name of the model as we control everything here.
Expand Down Expand Up @@ -141,4 +176,11 @@ export class AcoApp implements IAcoApp {
}
this.fields[index] = cb(structuredClone(this.fields[index]));
}

private async execOnAnyRequest(action: AcoRequestAction): Promise<void> {
if (!this.onAnyRequest) {
return;
}
await this.onAnyRequest(this.context, action);
}
}
3 changes: 2 additions & 1 deletion packages/api-aco/src/record/record.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export interface SearchRecordTag {
count: number;
}

export interface ListSearchRecordsWhere {
export interface ListSearchRecordsWhere<TData extends GenericSearchData = GenericSearchData> {
type: string;
location?: {
folderId: string;
};
tags_in?: string[];
tags_startsWith?: string;
tags_not_startsWith?: string;
data?: TData;
}

export interface ListSearchRecordsParams {
Expand Down
11 changes: 10 additions & 1 deletion packages/api-aco/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { CmsContext, CmsModel, CmsModelField } from "@webiny/api-headless-cms/ty
import {
AcoSearchRecordCrud,
AcoSearchRecordCrudBase,
AcoSearchRecordStorageOperations
AcoSearchRecordStorageOperations,
SearchRecord
} from "~/record/record.types";
import { AcoFolderCrud, AcoFolderStorageOperations } from "~/folder/folder.types";
import { AcoFilterCrud, AcoFilterStorageOperations } from "~/filter/filter.types";
Expand Down Expand Up @@ -113,12 +114,20 @@ export interface IAcoApp {
removeField: IAcoAppRemoveFieldCallable;
modifyField: IAcoAppModifyFieldCallable;
}
// TODO: determine correct type
export type IAcoAppOnEntry<T = any> = (entry: SearchRecord<T>) => Promise<SearchRecord<T>>;
export type IAcoAppOnEntryList<T = any> = (entry: SearchRecord<T>[]) => Promise<SearchRecord<T>[]>;
export type AcoRequestAction = "create" | "update" | "delete" | "move" | "fetch";
export type IAcoAppOnAnyRequest = (context: AcoContext, action: AcoRequestAction) => Promise<void>;

export interface IAcoAppParams {
name: string;
apiName: string;
model: CmsModel;
fields: CmsModelField[];
onEntry?: IAcoAppOnEntry;
onEntryList?: IAcoAppOnEntryList;
onAnyRequest?: IAcoAppOnAnyRequest;
}

export type IAcoAppsOptions = CreateAcoParams;
Expand Down

0 comments on commit ddbf234

Please sign in to comment.