@adminforth/import-export 1.4.27 → 1.5.0
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/build.log +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +32 -1
- package/index.ts +35 -2
- package/package.json +1 -1
package/build.log
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -6,8 +6,10 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
6
6
|
emailField: AdminForthResourceColumn;
|
|
7
7
|
authResourceId: string;
|
|
8
8
|
adminforth: IAdminForth;
|
|
9
|
+
auditLogPlugin: Record<string, any> | undefined;
|
|
9
10
|
constructor(options: PluginOptions);
|
|
10
11
|
private isRowValid;
|
|
12
|
+
private tryToAuditLogAction;
|
|
11
13
|
modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource): Promise<void>;
|
|
12
14
|
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource): void;
|
|
13
15
|
instanceUniqueRepresentation(pluginOptions: any): string;
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,28 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
31
31
|
}
|
|
32
32
|
return errors;
|
|
33
33
|
}
|
|
34
|
+
tryToAuditLogAction(actionName, actionDetails, adminUser, headers) {
|
|
35
|
+
if (!this.auditLogPlugin) {
|
|
36
|
+
console.warn('AuditLogPlugin not found, skipping audit log for action:', actionDetails);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
this.auditLogPlugin.logCustomAction({
|
|
41
|
+
resourceId: this.resourceConfig.resourceId,
|
|
42
|
+
recordId: null,
|
|
43
|
+
actionId: actionName,
|
|
44
|
+
oldData: null,
|
|
45
|
+
data: {
|
|
46
|
+
details: actionDetails,
|
|
47
|
+
},
|
|
48
|
+
user: adminUser,
|
|
49
|
+
headers: headers || {},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
console.error('Failed to log action to AuditLogPlugin:', e);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
34
56
|
modifyResourceConfig(adminforth, resourceConfig) {
|
|
35
57
|
const _super = Object.create(null, {
|
|
36
58
|
modifyResourceConfig: { get: () => super.modifyResourceConfig }
|
|
@@ -61,6 +83,12 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
61
83
|
}
|
|
62
84
|
validateConfigAfterDiscover(adminforth, resourceConfig) {
|
|
63
85
|
// optional method where you can safely check field types after database discovery was performed
|
|
86
|
+
try {
|
|
87
|
+
this.auditLogPlugin = this.adminforth.getPluginByClassName('AuditLogPlugin');
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
console.warn('Failed to get AuditLogPlugin for imort-export plugin. Audit logging will be skipped.');
|
|
91
|
+
}
|
|
64
92
|
}
|
|
65
93
|
instanceUniqueRepresentation(pluginOptions) {
|
|
66
94
|
// optional method to return unique string representation of plugin instance.
|
|
@@ -71,7 +99,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
71
99
|
server.endpoint({
|
|
72
100
|
method: 'POST',
|
|
73
101
|
path: `/plugin/${this.pluginInstanceId}/export-csv`,
|
|
74
|
-
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
|
|
102
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, headers }) {
|
|
75
103
|
const { filters, sort } = body;
|
|
76
104
|
const rawFilterError = rejectApiRawFilters(body.filters);
|
|
77
105
|
if (rawFilterError) {
|
|
@@ -96,6 +124,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
96
124
|
const rows = data.data.map((row) => {
|
|
97
125
|
return columns.map((col) => row[col.name]);
|
|
98
126
|
});
|
|
127
|
+
this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
|
|
99
128
|
return {
|
|
100
129
|
data: { fields, data: rows },
|
|
101
130
|
columnsToForceQuote,
|
|
@@ -109,6 +138,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
109
138
|
path: `/plugin/${this.pluginInstanceId}/import-csv`,
|
|
110
139
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
|
|
111
140
|
const { data } = body;
|
|
141
|
+
this.tryToAuditLogAction('import', `Import CSV with ${Object.keys(data).length} columns`, adminUser, headers);
|
|
112
142
|
const columns = this.getColumnNames(data);
|
|
113
143
|
const { errors, resourceColumns } = this.validateColumns(columns);
|
|
114
144
|
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
|
|
@@ -170,6 +200,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
170
200
|
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
171
201
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
|
|
172
202
|
const { data } = body;
|
|
203
|
+
this.tryToAuditLogAction('import', `Import CSV (new only) with ${Object.keys(data).length} columns`, adminUser, headers);
|
|
173
204
|
const columns = this.getColumnNames(data);
|
|
174
205
|
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
|
|
175
206
|
const { errors, resourceColumns } = this.validateColumns(columns);
|
package/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters } from "adminforth";
|
|
2
|
-
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource } from "adminforth";
|
|
2
|
+
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource, AdminUser } from "adminforth";
|
|
3
3
|
import type { PluginOptions } from './types.js';
|
|
4
4
|
import pLimit from 'p-limit';
|
|
5
5
|
|
|
@@ -8,6 +8,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
8
8
|
emailField: AdminForthResourceColumn;
|
|
9
9
|
authResourceId: string;
|
|
10
10
|
adminforth: IAdminForth;
|
|
11
|
+
auditLogPlugin: Record<string, any> | undefined;
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
constructor(options: PluginOptions) {
|
|
@@ -33,6 +34,29 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
33
34
|
return errors;
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
private tryToAuditLogAction(actionName: 'import' | 'export', actionDetails: string, adminUser: AdminUser, headers?: Record<string, string> ) {
|
|
38
|
+
if (!this.auditLogPlugin) {
|
|
39
|
+
console.warn('AuditLogPlugin not found, skipping audit log for action:', actionDetails);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
this.auditLogPlugin.logCustomAction({
|
|
44
|
+
resourceId: this.resourceConfig.resourceId,
|
|
45
|
+
recordId: null,
|
|
46
|
+
actionId: actionName,
|
|
47
|
+
oldData: null,
|
|
48
|
+
data: {
|
|
49
|
+
details: actionDetails,
|
|
50
|
+
|
|
51
|
+
},
|
|
52
|
+
user: adminUser,
|
|
53
|
+
headers: headers || {},
|
|
54
|
+
});
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.error('Failed to log action to AuditLogPlugin:', e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
36
60
|
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
37
61
|
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
38
62
|
if (!resourceConfig.options.pageInjections) {
|
|
@@ -61,6 +85,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
61
85
|
|
|
62
86
|
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
63
87
|
// optional method where you can safely check field types after database discovery was performed
|
|
88
|
+
try {
|
|
89
|
+
this.auditLogPlugin = this.adminforth.getPluginByClassName('AuditLogPlugin');
|
|
90
|
+
} catch (e) {
|
|
91
|
+
console.warn('Failed to get AuditLogPlugin for imort-export plugin. Audit logging will be skipped.');
|
|
92
|
+
}
|
|
64
93
|
}
|
|
65
94
|
|
|
66
95
|
instanceUniqueRepresentation(pluginOptions: any) : string {
|
|
@@ -73,7 +102,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
73
102
|
server.endpoint({
|
|
74
103
|
method: 'POST',
|
|
75
104
|
path: `/plugin/${this.pluginInstanceId}/export-csv`,
|
|
76
|
-
handler: async ({ body }) => {
|
|
105
|
+
handler: async ({ body, adminUser, headers }) => {
|
|
77
106
|
const { filters, sort } = body;
|
|
78
107
|
const rawFilterError = rejectApiRawFilters(body.filters);
|
|
79
108
|
if (rawFilterError) {
|
|
@@ -103,6 +132,8 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
103
132
|
return columns.map((col) => row[col.name]);
|
|
104
133
|
});
|
|
105
134
|
|
|
135
|
+
this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
|
|
136
|
+
|
|
106
137
|
return {
|
|
107
138
|
data: { fields, data: rows },
|
|
108
139
|
columnsToForceQuote,
|
|
@@ -117,6 +148,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
117
148
|
path: `/plugin/${this.pluginInstanceId}/import-csv`,
|
|
118
149
|
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
|
|
119
150
|
const { data } = body;
|
|
151
|
+
this.tryToAuditLogAction('import', `Import CSV with ${Object.keys(data).length} columns`, adminUser, headers);
|
|
120
152
|
const columns = this.getColumnNames(data);
|
|
121
153
|
const { errors, resourceColumns } = this.validateColumns(columns);
|
|
122
154
|
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
|
|
@@ -184,6 +216,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
184
216
|
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
185
217
|
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
|
|
186
218
|
const { data } = body;
|
|
219
|
+
this.tryToAuditLogAction('import', `Import CSV (new only) with ${Object.keys(data).length} columns`, adminUser, headers);
|
|
187
220
|
const columns = this.getColumnNames(data);
|
|
188
221
|
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
|
|
189
222
|
const { errors, resourceColumns } = this.validateColumns(columns);
|