@adminforth/crud-approve-plugin 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +56 -52
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -341,7 +341,7 @@ export default class CRUDApprovePlugin extends AdminForthPlugin {
|
|
|
341
341
|
response.status = 400;
|
|
342
342
|
return { error: 'Diff record is not pending' };
|
|
343
343
|
}
|
|
344
|
-
|
|
344
|
+
let beforeSaveResp;
|
|
345
345
|
if (approved === true) {
|
|
346
346
|
const resource = this.adminforth.config.resources.find(
|
|
347
347
|
(res) => res.resourceId == diffRecord[this.options.resourceColumns.resourceIdColumnName]
|
|
@@ -349,73 +349,74 @@ export default class CRUDApprovePlugin extends AdminForthPlugin {
|
|
|
349
349
|
const diffData = diffRecord[this.options.resourceColumns.dataColumnName];
|
|
350
350
|
const extra = diffRecord[this.options.resourceColumns.extraColumnName] || {};
|
|
351
351
|
extra.body = body;
|
|
352
|
+
extra.cookies = cookies;
|
|
352
353
|
let oldRecord = undefined;
|
|
353
354
|
if (action !== AllowedActionsEnum.create) {
|
|
354
355
|
oldRecord = await this.adminforth.connectors[resource.dataSource].getRecordByPrimaryKey(
|
|
355
356
|
resource, diffRecord[this.options.resourceColumns.recordIdColumnName]
|
|
356
357
|
);
|
|
357
358
|
}
|
|
358
|
-
|
|
359
|
+
beforeSaveResp = await this.callBeforeSaveHooks(
|
|
359
360
|
resource, action as AllowedActionsEnum, diffData['newRecord'],
|
|
360
361
|
adminUser, diffRecord[this.options.resourceColumns.recordIdColumnName],
|
|
361
362
|
diffData['newRecord'], oldRecord, this.adminforth, extra
|
|
362
363
|
);
|
|
363
|
-
if (beforeSaveResp.error) {
|
|
364
|
-
if (beforeSaveResp.error === 'Operation aborted by hook') {
|
|
365
|
-
return beforeSaveResp;
|
|
366
|
-
}
|
|
364
|
+
if (beforeSaveResp.error !== 'Operation aborted by hook') {
|
|
367
365
|
response.status = 500;
|
|
368
366
|
return { error: `Failed to apply approved changes: ${beforeSaveResp.error}` };
|
|
369
367
|
}
|
|
370
368
|
|
|
371
|
-
let recordUpdateResult;
|
|
372
|
-
const connector = this.adminforth.connectors[resource.dataSource];
|
|
373
|
-
if (action === AllowedActionsEnum.create) {
|
|
374
|
-
recordUpdateResult = await this.createRecord(resource, diffData, adminUser);
|
|
375
|
-
} else if (action === AllowedActionsEnum.edit) {
|
|
376
|
-
recordUpdateResult = await this.editRecord(
|
|
377
|
-
resource, diffData, diffRecord[this.options.resourceColumns.recordIdColumnName], connector
|
|
378
|
-
);
|
|
379
|
-
} else if (action === AllowedActionsEnum.delete) {
|
|
380
|
-
recordUpdateResult = await this.deleteRecord(
|
|
381
|
-
resource, diffRecord[this.options.resourceColumns.recordIdColumnName], connector
|
|
382
|
-
);
|
|
383
|
-
}
|
|
384
|
-
if (recordUpdateResult?.error) {
|
|
385
|
-
response.status = 500;
|
|
386
|
-
console.error('Error applying approved changes:', recordUpdateResult);
|
|
387
|
-
return { error: `Failed to apply approved changes: ${recordUpdateResult.error}` };
|
|
388
|
-
}
|
|
389
369
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
afterSaveResp = await this.callAfterSaveHooks(
|
|
410
|
-
resource, action as AllowedActionsEnum, newRecord, adminUser,
|
|
411
|
-
diffRecord[this.options.resourceColumns.recordIdColumnName],
|
|
412
|
-
{}, diffData['oldRecord'], this.adminforth, { body }
|
|
413
|
-
);
|
|
414
|
-
}
|
|
370
|
+
if (beforeSaveResp.error !== 'Operation aborted by hook') {
|
|
371
|
+
let recordUpdateResult;
|
|
372
|
+
const connector = this.adminforth.connectors[resource.dataSource];
|
|
373
|
+
if (action === AllowedActionsEnum.create) {
|
|
374
|
+
recordUpdateResult = await this.createRecord(resource, diffData, adminUser);
|
|
375
|
+
} else if (action === AllowedActionsEnum.edit) {
|
|
376
|
+
recordUpdateResult = await this.editRecord(
|
|
377
|
+
resource, diffData, diffRecord[this.options.resourceColumns.recordIdColumnName], connector
|
|
378
|
+
);
|
|
379
|
+
} else if (action === AllowedActionsEnum.delete) {
|
|
380
|
+
recordUpdateResult = await this.deleteRecord(
|
|
381
|
+
resource, diffRecord[this.options.resourceColumns.recordIdColumnName], connector
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
if (recordUpdateResult?.error) {
|
|
385
|
+
response.status = 500;
|
|
386
|
+
console.error('Error applying approved changes:', recordUpdateResult);
|
|
387
|
+
return { error: `Failed to apply approved changes: ${recordUpdateResult.error}` };
|
|
388
|
+
}
|
|
415
389
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
390
|
+
let afterSaveResp;
|
|
391
|
+
if (action === AllowedActionsEnum.create) {
|
|
392
|
+
const newRecord = recordUpdateResult.createdRecord;
|
|
393
|
+
afterSaveResp = await this.callAfterSaveHooks(
|
|
394
|
+
resource, action as AllowedActionsEnum, newRecord, adminUser,
|
|
395
|
+
diffRecord[this.options.resourceColumns.recordIdColumnName],
|
|
396
|
+
newRecord, {}, this.adminforth, { body }
|
|
397
|
+
);
|
|
398
|
+
} else if (action === AllowedActionsEnum.edit) {
|
|
399
|
+
const newRecord = diffData['newRecord'];
|
|
400
|
+
const oldRecord = await this.adminforth.connectors[resource.dataSource].getRecordByPrimaryKey(
|
|
401
|
+
resource, diffRecord[this.options.resourceColumns.recordIdColumnName]
|
|
402
|
+
);
|
|
403
|
+
afterSaveResp = await this.callAfterSaveHooks(
|
|
404
|
+
resource, action as AllowedActionsEnum, newRecord, adminUser,
|
|
405
|
+
recordId, newRecord, oldRecord, this.adminforth, { body }
|
|
406
|
+
);
|
|
407
|
+
} else if (action === AllowedActionsEnum.delete) {
|
|
408
|
+
const newRecord = diffData['newRecord'];
|
|
409
|
+
afterSaveResp = await this.callAfterSaveHooks(
|
|
410
|
+
resource, action as AllowedActionsEnum, newRecord, adminUser,
|
|
411
|
+
diffRecord[this.options.resourceColumns.recordIdColumnName],
|
|
412
|
+
{}, diffData['oldRecord'], this.adminforth, { body }
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
if (afterSaveResp?.error) {
|
|
417
|
+
response.status = 500;
|
|
418
|
+
return { error: `Failed to apply approved changes: ${afterSaveResp.error}` };
|
|
419
|
+
}
|
|
419
420
|
}
|
|
420
421
|
}
|
|
421
422
|
const r = await this.adminforth.updateResourceRecord({
|
|
@@ -435,6 +436,9 @@ export default class CRUDApprovePlugin extends AdminForthPlugin {
|
|
|
435
436
|
response.status = 500;
|
|
436
437
|
return { error: `Failed to update diff record status: ${r.error}` };
|
|
437
438
|
}
|
|
439
|
+
if (beforeSaveResp?.error === 'Operation aborted by hook') {
|
|
440
|
+
return beforeSaveResp;
|
|
441
|
+
}
|
|
438
442
|
return { ok: true };
|
|
439
443
|
}
|
|
440
444
|
})
|