refactor: purge api regular (#1415)

Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#1415
This commit is contained in:
sealday 2024-07-31 14:40:27 +08:00
parent 2666fc045c
commit 2b3bd07831
13 changed files with 8 additions and 192 deletions

View File

@ -6,7 +6,6 @@ import { WorkflowLink } from './components/WorkflowLink';
import { ExecutionPage } from './ExecutionPage'; import { ExecutionPage } from './ExecutionPage';
import { PluginActionTrigger } from './features/action-trigger'; import { PluginActionTrigger } from './features/action-trigger';
import { PluginAggregate } from './features/aggregate'; import { PluginAggregate } from './features/aggregate';
import { PluginAPIRegularClient } from './features/api-regular';
import PluginApproval from './features/approval'; import PluginApproval from './features/approval';
import PluginWorkflowDataMappingClient from './features/data-mapping'; import PluginWorkflowDataMappingClient from './features/data-mapping';
import { PluginDelay } from './features/delay'; import { PluginDelay } from './features/delay';
@ -91,7 +90,6 @@ export class PluginWorkflow extends Plugin {
await this.pm.add(PluginWorkflowJsonParseClient); await this.pm.add(PluginWorkflowJsonParseClient);
await this.pm.add(PluginWorkflowJSParseClient); await this.pm.add(PluginWorkflowJSParseClient);
await this.pm.add(PluginWorkflowDataMappingClient); await this.pm.add(PluginWorkflowDataMappingClient);
await this.pm.add(PluginAPIRegularClient);
await this.pm.add(PluginWorkflowInterceptor); await this.pm.add(PluginWorkflowInterceptor);
await this.pm.add(PluginVariables); await this.pm.add(PluginVariables);
await this.pm.add(PluginResponse); await this.pm.add(PluginResponse);

View File

@ -1,3 +0,0 @@
import { Plugin } from '@tachybase/client';
export class KitAPIRegularConfiguration extends Plugin {}

View File

@ -1,11 +0,0 @@
import { Plugin } from '@tachybase/client';
import { KitAPIRegularConfiguration } from './configuration/kit';
import { KitAPIRegularUsage } from './usage/kit';
export class PluginAPIRegularClient extends Plugin {
async afterAdd() {
await this.app.pm.add(KitAPIRegularConfiguration);
await this.app.pm.add(KitAPIRegularUsage);
}
}

View File

@ -3,6 +3,7 @@ import { Plugin } from '@tachybase/client';
import { tval } from '../../locale'; import { tval } from '../../locale';
import { PluginWorkflow } from '../../Plugin'; import { PluginWorkflow } from '../../Plugin';
import { OmniActionTrigger } from './OmniActionTrigger'; import { OmniActionTrigger } from './OmniActionTrigger';
import { KitAPIRegularUsage } from './usage/kit';
import { useFormWorkflowCustomActionProps } from './useFormWorkflowCustomActionProps'; import { useFormWorkflowCustomActionProps } from './useFormWorkflowCustomActionProps';
import { useRecordWorkflowCustomTriggerActionProps } from './useRecordWorkflowCustomTriggerActionProps'; import { useRecordWorkflowCustomTriggerActionProps } from './useRecordWorkflowCustomTriggerActionProps';
@ -48,6 +49,9 @@ const triggerWorkflowLinkItem = {
schema: { ...triggerWorkflowAction.schema, 'x-component': 'Action.Link' }, schema: { ...triggerWorkflowAction.schema, 'x-component': 'Action.Link' },
}; };
export class PluginOmniTrigger extends Plugin { export class PluginOmniTrigger extends Plugin {
async afterAdd() {
await this.app.pm.add(KitAPIRegularUsage);
}
async load() { async load() {
this.app.pm.get<PluginWorkflow>('workflow').registerTrigger('general-action', OmniActionTrigger); this.app.pm.get<PluginWorkflow>('workflow').registerTrigger('general-action', OmniActionTrigger);
this.app.addScopes({ this.app.addScopes({

View File

@ -79,7 +79,7 @@ export default class PluginWorkflowServer extends Plugin {
this.addFeature(PluginWorkflowJSONParseServer); this.addFeature(PluginWorkflowJSONParseServer);
this.addFeature(PluginWorkflowJSParseServer); this.addFeature(PluginWorkflowJSParseServer);
this.addFeature(PluginWorkflowDataMappingServer); this.addFeature(PluginWorkflowDataMappingServer);
this.addFeature(PluginWorkflowAPIRegularServer); // this.addFeature(PluginWorkflowAPIRegularServer);
this.addFeature(PluginInterception); this.addFeature(PluginInterception);
this.addFeature(PluginVariables); this.addFeature(PluginVariables);
this.addFeature(PluginResponse); this.addFeature(PluginResponse);

View File

@ -1,160 +0,0 @@
import { Context as ActionContext, Next } from '@tachybase/actions';
import { parseCollectionName } from '@tachybase/data-source-manager';
import { Model, modelAssociationByKey } from '@tachybase/database';
import Application, { DefaultContext } from '@tachybase/server';
import { get } from 'lodash';
import { BelongsTo, HasOne } from 'sequelize';
import WorkflowPlugin, { toJSON, WorkflowModel } from '../..';
import { NAMESPACE_TRIGGER_API_REGULAR } from '../../../common/constants';
import Trigger from '../../triggers';
interface Context extends ActionContext, DefaultContext {}
export class APIRegularTrigger extends Trigger {
constructor(workflow: WorkflowPlugin) {
super(workflow);
// workflow.app.use(this.middleware, { after: 'dataSource' });
}
async middleware(context: Context, next: Next) {
const {
resourceName,
actionName,
params: { triggerWorkflows },
} = context.action;
if (resourceName === 'workflows' && actionName === 'trigger') {
return this.triggerAction(context, next);
}
await next();
if (!triggerWorkflows) {
return;
}
// if (!['create', 'update'].includes(actionName)) {
// return;
// }
return this.trigger(context);
}
async triggerAction(context: Context, next: Next) {
const { triggerWorkflows } = context.action.params;
if (!triggerWorkflows) {
return context.throw(400);
}
context.status = 202;
await next();
this.trigger(context);
}
private async trigger(context: Context) {
const { triggerWorkflows = '', values } = context.action.params;
const dataSourceHeader = context.get('x-data-source') || 'main';
const { currentUser, currentRole } = context.state;
const { model: UserModel } = this.workflow.db.getCollection('users');
const userInfo = {
user: UserModel.build(currentUser).desensitize(),
roleName: currentRole,
};
const triggers = triggerWorkflows.split(',').map((trigger) => trigger.split('!'));
const workflowRepo = this.workflow.db.getRepository('workflows');
const workflows = (
await workflowRepo.find({
filter: {
key: triggers.map((trigger) => trigger[0]),
current: true,
type: NAMESPACE_TRIGGER_API_REGULAR,
enabled: true,
},
})
).filter((workflow) => Boolean(workflow.config.collection));
const syncGroup = [];
const asyncGroup = [];
for (const workflow of workflows) {
const { collection, appends = [] } = workflow.config;
const [dataSourceName, collectionName] = parseCollectionName(collection);
const trigger = triggers.find((trigger) => trigger[0] == workflow.key);
const event = [workflow];
if (context.action.resourceName !== 'workflows') {
if (!context.body) {
continue;
}
if (dataSourceName !== dataSourceHeader) {
continue;
}
const { body: data } = context;
for (const row of Array.isArray(data) ? data : [data]) {
let payload = row;
if (trigger[1]) {
const paths = trigger[1].split('.');
for (const field of paths) {
if (payload.get(field)) {
payload = payload.get(field);
} else {
const association = <HasOne | BelongsTo>modelAssociationByKey(payload, field);
payload = await payload[association.accessors.get]();
}
}
}
const model = payload.constructor;
if (payload instanceof Model) {
if (collectionName !== model.collection.name) {
continue;
}
if (appends.length) {
payload = await model.collection.repository.findOne({
filterByTk: payload.get(model.primaryKeyAttribute),
appends,
});
}
}
// this.workflow.trigger(workflow, { data: toJSON(payload), ...userInfo });
event.push({ data: toJSON(payload), ...userInfo });
}
} else {
const { model, repository } = (<Application>context.app).dataSourceManager.dataSources
.get(dataSourceName)
.collectionManager.getCollection(collectionName);
let data = trigger[1] ? get(values, trigger[1]) : values;
const pk = get(data, model.primaryKeyAttribute);
if (appends.length && pk != null) {
data = await repository.findOne({
filterByTk: pk,
appends,
});
}
// this.workflow.trigger(workflow, {
// data,
// ...userInfo,
// });
event.push({ data, ...userInfo });
}
(workflow.sync ? syncGroup : asyncGroup).push(event);
}
for (const event of syncGroup) {
await this.workflow.trigger(event[0], event[1], { httpContext: context });
}
for (const event of asyncGroup) {
this.workflow.trigger(event[0], event[1]);
}
this.workflow.noticeManager.notify('workflow:regular', {
msg: 'start',
});
}
on(workflow: WorkflowModel): void {}
off(workflow: WorkflowModel): void {}
}

View File

@ -1 +0,0 @@
export { default } from './plugin';

View File

@ -1,14 +0,0 @@
import { Plugin } from '@tachybase/server';
import WorkflowPlugin from '../..';
import { NAMESPACE_TRIGGER_API_REGULAR } from '../../../common/constants';
import { APIRegularTrigger } from './APIRegular.trigger';
export class PluginWorkflowAPIRegularServer extends Plugin {
async load() {
const pluginWorkflow: any = this.app.pm.get(WorkflowPlugin);
pluginWorkflow.registerTrigger(NAMESPACE_TRIGGER_API_REGULAR, new APIRegularTrigger(pluginWorkflow));
}
}
export default PluginWorkflowAPIRegularServer;

View File

@ -122,6 +122,9 @@ export class OmniTrigger extends Trigger {
for (const event of asyncGroup) { for (const event of asyncGroup) {
this.workflow.trigger(event[0], event[1]); this.workflow.trigger(event[0], event[1]);
} }
this.workflow.noticeManager.notify('workflow:regular', {
msg: 'start',
});
await next(); await next();
}; };
constructor(workflow) { constructor(workflow) {