|
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'; |
3 | 9 |
|
4 | 10 | import { difference } from './utils/difference';
|
5 | 11 | import { getLogPropertyName } from './utils/get-log-property-name';
|
6 | 12 | import { LoggerFeatureOptions } from './logger.feature';
|
7 | 13 |
|
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 | + } |
10 | 26 |
|
11 |
| - context.initialRecord = id ? await context.resource.findOne(id) : {}; |
12 | 27 | return request;
|
13 | 28 | };
|
14 | 29 |
|
@@ -81,60 +96,95 @@ export const createLogAction =
|
81 | 96 | options = {},
|
82 | 97 | }: CreateLogActionParams = {}): After<ActionResponse> =>
|
83 | 98 | 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; |
84 | 142 | const {
|
85 | 143 | resourceName = 'Log',
|
86 | 144 | propertiesMapping = {},
|
87 | 145 | userIdAttribute,
|
88 | 146 | } = options ?? {};
|
89 |
| - const { currentAdmin, _admin } = context; |
90 |
| - const { params, method } = request; |
| 147 | + |
91 | 148 | const Log = _admin.findResource(resourceName);
|
92 | 149 | const ModifiedResource = _admin.findResource(params.resourceId);
|
93 | 150 |
|
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; |
101 | 154 |
|
102 | 155 | 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); |
107 | 157 | if (!modifiedRecord) {
|
108 |
| - return response; |
| 158 | + return; |
109 | 159 | }
|
110 | 160 |
|
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 | + |
117 | 169 | await Log.create({
|
118 | 170 | [getLogPropertyName('recordTitle', propertiesMapping)]:
|
119 | 171 | getRecordTitle(modifiedRecord),
|
120 | 172 | [getLogPropertyName('resource', propertiesMapping)]: params.resourceId,
|
121 | 173 | [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, |
126 | 175 | [getLogPropertyName('user', propertiesMapping)]: adminId,
|
127 | 176 | [getLogPropertyName('difference', propertiesMapping)]: JSON.stringify(
|
128 | 177 | difference(
|
129 | 178 | newParamsToCompare,
|
130 | 179 | flat.flatten(
|
131 |
| - JSON.parse(JSON.stringify(context.initialRecord.params)) |
| 180 | + initialRecord?.params |
| 181 | + ? JSON.parse(JSON.stringify(await initialRecord.params)) |
| 182 | + : {} |
132 | 183 | )
|
133 | 184 | )
|
134 | 185 | ),
|
135 | 186 | });
|
136 | 187 | } catch (e) {
|
137 | 188 | console.error(e);
|
138 | 189 | }
|
139 |
| - return response; |
140 | 190 | };
|
0 commit comments