-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
443 lines (391 loc) · 16 KB
/
Copy pathindex.ts
File metadata and controls
443 lines (391 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource, AdminUser } from "adminforth";
import type { PluginOptions } from './types.js';
import pLimit from 'p-limit';
export default class ImportExport extends AdminForthPlugin {
options: PluginOptions;
emailField: AdminForthResourceColumn;
authResourceId: string;
adminforth: IAdminForth;
auditLogPlugin: Record<string, any> | undefined;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
private isRowValid(row: Record<string, unknown>): string[] {
let errors = [];
for (const col of Object.keys(row)) {
const resourceCol = this.resourceConfig.columns.find(c => c.name === col);
if (!resourceCol) {
errors.push(`Column '${col}' not found in resource configuration.`);
continue;
}
if (resourceCol.backendOnly) {
errors.push(`Column '${col}' is backend only and cannot be imported.`);
}
if (resourceCol.enum && !resourceCol.enum.some(e => e.value === row[col])) {
errors.push(`Column '${col}' has an enum of [${resourceCol.enum.map(e => e.label).join(', ')}] but got value '${row[col]}'.`);
}
}
return errors;
}
private tryToAuditLogAction(actionName: 'import' | 'export', actionDetails: string, adminUser: AdminUser, headers?: Record<string, string> ) {
if (!this.auditLogPlugin) {
console.warn('AuditLogPlugin not found, skipping audit log for action:', actionDetails);
return;
}
try {
this.auditLogPlugin.logCustomAction({
resourceId: this.resourceConfig.resourceId,
recordId: null,
actionId: actionName,
oldData: null,
data: {
details: actionDetails,
},
user: adminUser,
headers: headers || {},
});
} catch (e) {
console.error('Failed to log action to AuditLogPlugin:', e);
}
}
private async ensureAnyAllowed(
adminUser: AdminUser,
checks: { source: ActionCheckSource; action: AllowedActionsEnum }[],
meta: Record<string, unknown> = {}
): Promise<{ ok: boolean; error?: string }> {
for (const { source, action } of checks) {
const { allowedActions } = await interpretResource(
adminUser,
this.resourceConfig,
meta,
source,
this.adminforth
);
if (allowedActions[action] === true) {
return { ok: true };
}
}
return {
ok: false,
error: 'Action is not allowed',
};
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
if (!resourceConfig.options.pageInjections) {
resourceConfig.options.pageInjections = {};
}
if (!resourceConfig.options.pageInjections.list) {
resourceConfig.options.pageInjections.list = {};
}
if (!resourceConfig.options.pageInjections.list.threeDotsDropdownItems) {
resourceConfig.options.pageInjections.list.threeDotsDropdownItems = [];
}
(resourceConfig.options.pageInjections.list.threeDotsDropdownItems as AdminForthComponentDeclaration[]).push({
file: this.componentPath('ExportCsv.vue'),
meta: { pluginInstanceId: this.pluginInstanceId, select: 'all' }
}, {
file: this.componentPath('ExportCsv.vue'),
meta: { pluginInstanceId: this.pluginInstanceId, select: 'filtered' }
}, {
file: this.componentPath('ImportCsv.vue'),
meta: { pluginInstanceId: this.pluginInstanceId }
});
// simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
}
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
// optional method where you can safely check field types after database discovery was performed
try {
this.auditLogPlugin = this.adminforth.getPluginByClassName('AuditLogPlugin');
} catch (e) {
console.warn('Failed to get AuditLogPlugin for imort-export plugin. Audit logging will be skipped.');
}
}
instanceUniqueRepresentation(pluginOptions: any) : string {
// optional method to return unique string representation of plugin instance.
// Needed if plugin can have multiple instances on one resource
return `${this.pluginInstanceId}`;
}
setupEndpoints(server: IHttpServer) {
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/export-csv`,
handler: async ({ body, adminUser, headers }) => {
const { filters, sort } = body;
const access = await this.ensureAnyAllowed(
adminUser,
[
{ source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
{ source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
],
{ requestBody: body }
);
if (!access.ok) {
return { ok: false, error: access.error };
}
const rawFilterError = rejectApiRawFilters(body.filters);
if (rawFilterError) {
return rawFilterError;
}
const data = await this.adminforth.connectors[this.resourceConfig.dataSource].getData({
resource: this.resourceConfig,
limit: 1e6,
offset: 0,
filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(filters),
sort,
getTotals: true,
});
// prepare data for PapaParse unparse
const columns = this.resourceConfig.columns.filter((col) => !col.virtual && !col.backendOnly);
const columnsToForceQuote = columns.map(col => {
return col.type !== AdminForthDataTypes.FLOAT
&& col.type !== AdminForthDataTypes.INTEGER
&& col.type !== AdminForthDataTypes.BOOLEAN;
})
const fields = columns.map((col) => col.name);
const rows = data.data.map((row) => {
return columns.map((col) => row[col.name]);
});
this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
return {
data: { fields, data: rows },
columnsToForceQuote,
exportedCount: data.total,
ok: true
};
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/import-csv`,
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
const { data } = body;
const createEditAccess = await this.ensureAnyAllowed(
adminUser,
[
{ source: ActionCheckSource.CreateRequest, action: AllowedActionsEnum.create },
{ source: ActionCheckSource.EditRequest, action: AllowedActionsEnum.edit }
],
{ requestBody: body }
);
if (!createEditAccess.ok) {
return { ok: false, error: createEditAccess.error };
}
const columns = this.getColumnNames(data);
const { errors, resourceColumns } = this.validateColumns(columns);
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
if (errors.length > 0) {
return { ok: false, errors };
}
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
const rows = this.buildRowsFromData(data, columns, resourceColumns, { coerceTypes: true });
console.log('Prepared rows for import:', rows);
this.tryToAuditLogAction('import', `Import CSV with ${Object.keys(data).length} columns`, adminUser, headers);
let importedCount = 0;
let updatedCount = 0;
const limit = pLimit(100);
await Promise.all(rows.map((row) => limit(async () => {
try {
const rowErrors = await this.isRowValid(row);
if (rowErrors.length > 0) {
errors.push(...rowErrors);
return;
}
const recordId = primaryKeyColumn ? row[primaryKeyColumn.name] as string : undefined;
if (primaryKeyColumn && recordId) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
.list([Filters.EQ(primaryKeyColumn.name, recordId)]);
if (existingRecord.length > 0) {
const connector = this.adminforth.connectors[resource.dataSource];
const oldRecord = await connector.getRecordByPrimaryKey(resource, recordId)
if (!oldRecord) {
const primaryKeyColumn = resource.columns.find((col) => col.primaryKey);
return { error: `Record with ${primaryKeyColumn.name} ${recordId} not found` };
}
const { error } = await this.adminforth.updateResourceRecord({
resource, updates: row, adminUser, oldRecord, recordId, response,
extra: { body, query, headers, cookies, requestUrl, response }
});
if (error) {
return { error };
}
updatedCount++;
return;
}
}
await this.adminforth.createResourceRecord({
resource: resource,
record: row,
adminUser: adminUser,
extra: { body, query, headers, cookies, requestUrl, response }
});
importedCount++;
} catch (e) {
errors.push(e.message);
}
})));
return { ok: true, importedCount, updatedCount, errors };
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
const { data } = body;
const access = await this.ensureAnyAllowed(
adminUser,
[{ source: ActionCheckSource.CreateRequest, action: AllowedActionsEnum.create }],
{ requestBody: body }
);
if (!access.ok) {
return { ok: false, error: access.error };
}
const columns = this.getColumnNames(data);
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
const { errors, resourceColumns } = this.validateColumns(columns);
if (errors.length > 0) {
return { ok: false, errors };
}
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
const rows = this.buildRowsFromData(data, columns, resourceColumns, { coerceTypes: true });
this.tryToAuditLogAction('import', `Import CSV (new only) with ${Object.keys(data).length} columns`, adminUser, headers);
let importedCount = 0;
const limit = pLimit(100);
await Promise.all(rows.map((row) => limit(async () => {
try {
const rowErrors = await this.isRowValid(row);
if (rowErrors.length > 0) {
errors.push(...rowErrors);
return;
}
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
if (existingRecord.length > 0) {
return;
}
}
await this.adminforth.createResourceRecord({
resource: resource,
record: row,
adminUser: adminUser,
extra: { body, query, headers, cookies, requestUrl, response }
});
importedCount++;
} catch (e) {
errors.push(e.message);
}
})));
return { ok: true, importedCount, errors };
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/check-records`,
handler: async ({ body, adminUser }) => {
const access = await this.ensureAnyAllowed(
adminUser,
[
{ source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
{ source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
],
{ requestBody: body }
);
if (!access.ok) {
return { ok: false, error: access.error };
}
const { data } = body as { data: Record<string, unknown[]> };
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
const columns = this.getColumnNames(data);
const rows = this.buildRowsFromData(data, columns, undefined, { coerceTypes: false });
const primaryKeys = rows
.map(row => primaryKeyColumn ? row[primaryKeyColumn.name] : undefined)
.filter(key => key !== undefined && key !== null && key !== '');
const existingRecords = await this.adminforth
.resource(this.resourceConfig.resourceId)
.list([{
field: primaryKeyColumn.name,
operator: AdminForthFilterOperators.IN,
value: primaryKeys,
}]);
return {
ok: true,
total: rows.length,
existingCount: existingRecords.length,
newCount: rows.length - existingRecords.length,
};
}
});
}
private getColumnNames(data: Record<string, unknown[]>): string[] {
return Object.keys(data ?? {});
}
private validateColumns(columns: string[]): {
errors: string[];
resourceColumns: AdminForthResourceColumn[];
} {
const errors: string[] = [];
const resourceColumns: AdminForthResourceColumn[] = [];
columns.forEach((col) => {
const resourceColumn = this.resourceConfig.columns.find((c) => c.name === col);
if (!resourceColumn) {
const similar = suggestIfTypo(this.resourceConfig.columns.map((c) => c.name), col);
errors.push(
`Column '${col}' defined in CSV not found in resource '${this.resourceConfig.resourceId}'. ${
similar
? `If you mean '${similar}', rename it in CSV`
: 'If column is in database but not in resource configuration, add it with showIn:[]'
}`
);
return;
}
resourceColumns.push(resourceColumn);
});
return { errors, resourceColumns };
}
private buildRowsFromData(
data: Record<string, unknown[]>,
columns: string[],
resourceColumns?: AdminForthResourceColumn[],
{ coerceTypes }: { coerceTypes: boolean } = { coerceTypes: true }
) {
const columnValues: unknown[][] = Object.values(data ?? {});
if (columns.length === 0 || columnValues.length === 0) {
return [];
}
const rows: Record<string, unknown>[] = [];
const rowCount = columnValues[0].length;
for (let i = 0; i < rowCount; i++) {
const row: Record<string, unknown> = {};
for (let j = 0; j < columns.length; j++) {
const val = columnValues[j][i];
const resourceCol = resourceColumns ? resourceColumns[j] : undefined;
row[columns[j]] = coerceTypes
? this.coerceValue(resourceCol, val)
: val;
}
rows.push(row);
}
return rows;
}
private coerceValue(resourceCol: AdminForthResourceColumn | undefined, val: unknown): unknown {
if (!resourceCol || val === '') {
return val;
}
if (
(resourceCol.type === AdminForthDataTypes.INTEGER
|| resourceCol.type === AdminForthDataTypes.FLOAT)
) {
return +val;
}
if (resourceCol.type === AdminForthDataTypes.BOOLEAN) {
if (typeof val === 'string') {
return val.toLowerCase() === 'true' || val === '1';
}
return val === 1 || val === true;
}
return val;
}
}