ChengLei Shao 61e7a89067
refactor: export action (#5665)
* chore: export xlsx command

* chore: container service

* chore: report export progress

* chore: export

* chore: performInsert

* chore: export command

* chore: base exporter

* chore: limit option

* chore: export

* chore: export

* chore: types

* chore: ws socket with auth

* refactor: refactor exporter, change addRows method to handleRow method to support row-by-row processing

* chore: xlsx exporter

* chore: base exporter

* chore: export limit

* chore: import action

* chore: load websocket

* chore: import action

* chore: object to cli args

* chore: import options

* chore: import action

* chore: import runner

* chore: import action

* chore: import options

* chore: import options

* chore: plugin load event

* chore: test

* chore: i18n

* chore: i18n

* chore: i18n

* fix: ws auth status

* chore: cache in data source

* chore: load datasource

* chore: import with field alias

* fix: build

* fix: test

* fix: build

* fix: import schema settings

* fix: import action

* fix: import progress

* chore: template creator

* fix: import error message

* fix: import error message

* chore: test

* chore: workflow dispatch event

* chore: export alias

* fix: typo

* chore: error render

* fix: event error

* chore: send message to tags

* fix: test

* fix: import action setting
2024-12-31 20:16:03 +08:00

105 lines
3.0 KiB
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { Plugin } from '@nocobase/server';
import { downloadXlsxTemplate, importXlsx } from './actions';
import { importMiddleware } from './middleware';
import { ImportError, ImportValidationError } from './errors';
export class PluginActionImportServer extends Plugin {
beforeLoad() {
this.app.on('afterInstall', async () => {
if (!this.app.db.getRepository('roles')) {
return;
}
const roleNames = ['admin', 'member'];
const roles = await this.app.db.getRepository('roles').find({
filter: {
name: roleNames,
},
});
for (const role of roles) {
await this.app.db.getRepository('roles').update({
filter: {
name: role.name,
},
values: {
strategy: {
...role.strategy,
actions: [...role.strategy.actions, 'importXlsx'],
},
},
});
}
});
}
async load() {
this.app.dataSourceManager.afterAddDataSource((dataSource) => {
dataSource.resourceManager.use(importMiddleware);
dataSource.resourceManager.registerActionHandler('downloadXlsxTemplate', downloadXlsxTemplate);
dataSource.resourceManager.registerActionHandler('importXlsx', importXlsx);
dataSource.acl.setAvailableAction('importXlsx', {
displayName: '{{t("Import")}}',
allowConfigureFields: true,
type: 'new-data',
onNewRecord: true,
});
dataSource.acl.allow('*', 'downloadXlsxTemplate', 'loggedIn');
});
const errorHandlerPlugin = this.app.getPlugin<any>('error-handler');
errorHandlerPlugin.errorHandler.register(
(err) => err instanceof ImportValidationError,
(err: ImportValidationError, ctx) => {
ctx.status = 400;
ctx.body = {
errors: [
{
message: ctx.i18n.t(err.code, {
...err.params,
ns: 'action-import',
}),
},
],
};
},
);
errorHandlerPlugin.errorHandler.register(
(err) => err.name === 'ImportError',
(err: ImportError, ctx) => {
ctx.status = 400;
const causeError = err.cause;
errorHandlerPlugin.errorHandler.renderError(causeError, ctx);
ctx.body = {
errors: [
{
message: ctx.i18n.t('import-error', {
ns: 'action-import',
rowData: err.rowData,
rowIndex: err.rowIndex,
causeMessage: ctx.body.errors[0].message,
}),
},
],
};
},
);
}
}
export default PluginActionImportServer;
export * from './services/xlsx-importer';
export * from './services/template-creator';