Skip to content

Commit 9829781

Browse files
author
Przemysław Szejna
committed
feat: fix handling new action and add handler for bulkDelete action
1 parent d3d945a commit 9829781

File tree

3 files changed

+1378
-1281
lines changed

3 files changed

+1378
-1281
lines changed

src/log.action.ts

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
import { ActionResponse, After, Before, flat } from 'adminjs';
2-
import { merge } from 'lodash';
1+
import {
2+
ActionContext,
3+
ActionRequest,
4+
ActionResponse,
5+
After,
6+
Before,
7+
flat,
8+
} from 'adminjs';
39

410
import { difference } from './utils/difference';
511
import { getLogPropertyName } from './utils/get-log-property-name';
612
import { LoggerFeatureOptions } from './logger.feature';
713

8-
export const rememberInitialRecord: Before = async (request, context) => {
9-
const id = context.record?.id();
14+
export const rememberInitialRecord: Before = async (
15+
request: ActionRequest,
16+
context: ActionContext
17+
) => {
18+
if (request.params.action === 'bulkDelete') {
19+
context.initialRecords = request.query?.recordIds
20+
? await context.resource.findMany(request.query.recordIds.split(','))
21+
: [];
22+
} else {
23+
const id = context.record?.id();
24+
context.initialRecord = id ? await context.resource.findOne(id) : {};
25+
}
1026

11-
context.initialRecord = id ? await context.resource.findOne(id) : {};
1227
return request;
1328
};
1429

@@ -81,60 +96,95 @@ export const createLogAction =
8196
options = {},
8297
}: CreateLogActionParams = {}): After<ActionResponse> =>
8398
async (response, request, context) => {
99+
const { records, record } = context;
100+
const { params, method } = request;
101+
102+
if (
103+
(onlyForPostMethod && method !== 'post') ||
104+
Object.keys(record?.errors || {}).length
105+
) {
106+
return response;
107+
}
108+
109+
const persistLog = createPersistLogAction(request, context, options);
110+
111+
if (request.params.action === 'bulkDelete') {
112+
await Promise.all(
113+
(records || []).map(async record => {
114+
const recordId = (await record.params).id;
115+
await persistLog(
116+
recordId,
117+
record,
118+
context.initialRecords.find(r => {
119+
return r.params.id === recordId;
120+
})
121+
);
122+
})
123+
);
124+
} else {
125+
const recordId =
126+
params.recordId ||
127+
(typeof record?.params?.id === 'string'
128+
? record?.params?.id
129+
: record?.params?.id?.());
130+
if (recordId) {
131+
await persistLog(recordId, context.record, context.initialRecord);
132+
}
133+
}
134+
135+
return response;
136+
};
137+
138+
const createPersistLogAction =
139+
(request, context, options) => async (recordId, record, initialRecord) => {
140+
const { currentAdmin, _admin } = context;
141+
const { params } = request;
84142
const {
85143
resourceName = 'Log',
86144
propertiesMapping = {},
87145
userIdAttribute,
88146
} = options ?? {};
89-
const { currentAdmin, _admin } = context;
90-
const { params, method } = request;
147+
91148
const Log = _admin.findResource(resourceName);
92149
const ModifiedResource = _admin.findResource(params.resourceId);
93150

94-
if (!params.recordId || (onlyForPostMethod && method !== 'post')) {
95-
return response;
96-
}
97-
98-
let adminId;
99-
if (userIdAttribute) adminId = currentAdmin?.[userIdAttribute];
100-
else adminId = currentAdmin?.id ?? currentAdmin?._id ?? currentAdmin;
151+
const adminId = userIdAttribute
152+
? currentAdmin?.[userIdAttribute]
153+
: currentAdmin?.id ?? currentAdmin?._id ?? currentAdmin;
101154

102155
try {
103-
const modifiedRecord = merge(
104-
JSON.parse(JSON.stringify(context.record)),
105-
await ModifiedResource.findOne(params.recordId)
106-
);
156+
const modifiedRecord = await ModifiedResource.findOne(recordId);
107157
if (!modifiedRecord) {
108-
return response;
158+
return;
109159
}
110160

111-
const newParamsToCompare =
112-
params.action === 'delete'
113-
? {}
114-
: flat.flatten<any, any>(
115-
JSON.parse(JSON.stringify(modifiedRecord.params))
116-
);
161+
const newParamsToCompare = ['delete', 'bulkDelete'].includes(
162+
params.action
163+
)
164+
? {}
165+
: flat.flatten<object, object>(
166+
JSON.parse(JSON.stringify(modifiedRecord.params))
167+
);
168+
117169
await Log.create({
118170
[getLogPropertyName('recordTitle', propertiesMapping)]:
119171
getRecordTitle(modifiedRecord),
120172
[getLogPropertyName('resource', propertiesMapping)]: params.resourceId,
121173
[getLogPropertyName('action', propertiesMapping)]: params.action,
122-
[getLogPropertyName('recordId', propertiesMapping)]:
123-
params.recordId ?? typeof modifiedRecord.id === 'string'
124-
? modifiedRecord.id
125-
: modifiedRecord.id?.(),
174+
[getLogPropertyName('recordId', propertiesMapping)]: recordId,
126175
[getLogPropertyName('user', propertiesMapping)]: adminId,
127176
[getLogPropertyName('difference', propertiesMapping)]: JSON.stringify(
128177
difference(
129178
newParamsToCompare,
130179
flat.flatten(
131-
JSON.parse(JSON.stringify(context.initialRecord.params))
180+
initialRecord?.params
181+
? JSON.parse(JSON.stringify(await initialRecord.params))
182+
: {}
132183
)
133184
)
134185
),
135186
});
136187
} catch (e) {
137188
console.error(e);
138189
}
139-
return response;
140190
};

src/logger.feature.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const loggerFeature = (options: LoggerFeatureOptions): FeatureType => {
4343
before: rememberInitialRecord,
4444
after: createLogAction({ options }),
4545
},
46+
bulkDelete: {
47+
before: rememberInitialRecord,
48+
after: createLogAction({ onlyForPostMethod: true, options }),
49+
},
4650
},
4751
});
4852
};

0 commit comments

Comments
 (0)