|
| 1 | +import CRUDApprovePlugin from '../../plugins/af-crud-approve-plugin/index.js'; |
| 2 | +import { AdminForthDataTypes } from '../../adminforth/index.js'; |
| 3 | +import type { AdminForthResourceInput, AdminUser } from '../../adminforth/index.js'; |
| 4 | + |
| 5 | +async function allowedForSuperAdmin({ adminUser }: { adminUser: AdminUser }): Promise<boolean> { |
| 6 | + return adminUser.dbUser.role === 'superadmin'; |
| 7 | +} |
| 8 | + |
| 9 | +export const crudApprovePlugin = new CRUDApprovePlugin({ |
| 10 | + resourceColumns: { |
| 11 | + idColumnName: 'id', |
| 12 | + recordIdColumnName: 'record_id', |
| 13 | + resourceIdColumnName: 'resource_id', |
| 14 | + actionColumnName: 'action', |
| 15 | + dataColumnName: 'data', |
| 16 | + userIdColumnName: 'user_id', |
| 17 | + responserIdColumnName: 'responser_id', |
| 18 | + statusColumnName: 'status', |
| 19 | + createdAtColumnName: 'created_at', |
| 20 | + extraColumnName: 'extra', |
| 21 | + }, |
| 22 | + // dev-demo is usually used with a single superadmin login, so allow the author |
| 23 | + // of the request to approve it himself. Never do this in production: it makes |
| 24 | + // the four-eyes principle void. |
| 25 | + allowSelfApproval: true, |
| 26 | +}); |
| 27 | + |
| 28 | +export default { |
| 29 | + dataSource: 'sqlite', |
| 30 | + table: 'crud_manual_approve', |
| 31 | + resourceId: 'crud_manual_approve', |
| 32 | + label: 'CRUD approvals', |
| 33 | + recordLabel: (r: any) => `${r.resource_id} / ${r.action}`, |
| 34 | + columns: [ |
| 35 | + { |
| 36 | + name: 'id', |
| 37 | + primaryKey: true, |
| 38 | + showIn: { list: false, show: true, edit: false, create: false }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'record_id', |
| 42 | + label: 'Record ID', |
| 43 | + showIn: { all: true, edit: false, create: false }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'resource_id', |
| 47 | + label: 'Resource', |
| 48 | + showIn: { all: true, edit: false, create: false }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'action', |
| 52 | + enum: [ |
| 53 | + { value: 'create', label: 'Create' }, |
| 54 | + { value: 'edit', label: 'Edit' }, |
| 55 | + { value: 'delete', label: 'Delete' }, |
| 56 | + ], |
| 57 | + showIn: { all: true, edit: false, create: false }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: 'data', |
| 61 | + type: AdminForthDataTypes.JSON, |
| 62 | + sortable: false, |
| 63 | + showIn: { all: true, edit: false, create: false }, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'user_id', |
| 67 | + label: 'Requested by', |
| 68 | + foreignResource: { |
| 69 | + resourceId: 'adminuser', |
| 70 | + }, |
| 71 | + showIn: { all: true, edit: false, create: false }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'responser_id', |
| 75 | + label: 'Responded by', |
| 76 | + foreignResource: { |
| 77 | + resourceId: 'adminuser', |
| 78 | + }, |
| 79 | + showIn: { all: true, edit: false, create: false }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: 'status', |
| 83 | + enum: [ |
| 84 | + { value: 1, label: 'Pending' }, |
| 85 | + { value: 2, label: 'Approved' }, |
| 86 | + { value: 3, label: 'Rejected' }, |
| 87 | + ], |
| 88 | + showIn: { all: true, edit: false, create: false }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: 'created_at', |
| 92 | + type: AdminForthDataTypes.DATETIME, |
| 93 | + allowMinMaxQuery: true, |
| 94 | + showIn: { all: true, edit: false, create: false }, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'extra', |
| 98 | + type: AdminForthDataTypes.JSON, |
| 99 | + showIn: { all: false }, |
| 100 | + backendOnly: true, |
| 101 | + }, |
| 102 | + ], |
| 103 | + options: { |
| 104 | + listPageSize: 10, |
| 105 | + allowedActions: { |
| 106 | + create: false, |
| 107 | + edit: false, |
| 108 | + delete: false, |
| 109 | + // this is not only a UI matter: the plugin resolves `show` on every |
| 110 | + // approve/reject request, so it is the actual approval permission |
| 111 | + show: allowedForSuperAdmin, |
| 112 | + filter: allowedForSuperAdmin, |
| 113 | + }, |
| 114 | + }, |
| 115 | + // cast for the same reason as in adminuser.ts: the plugin repo has its own |
| 116 | + // adminforth copy, so its IAdminForthPlugin is a different type identity |
| 117 | + plugins: [crudApprovePlugin] as any, |
| 118 | +} as AdminForthResourceInput; |
0 commit comments