2023-04-25 13:12:14 +08:00
|
|
|
import { JOB_STATUS } from '../constants';
|
|
|
|
import FlowNodeModel from '../models/FlowNode';
|
2022-02-20 21:23:39 +08:00
|
|
|
|
|
|
|
export default {
|
2022-06-24 23:28:49 +08:00
|
|
|
async run(node: FlowNodeModel, input, processor) {
|
2023-04-25 13:12:14 +08:00
|
|
|
const { collection, params: { appends = [], ...params } = {} } = node.config;
|
2022-02-20 21:23:39 +08:00
|
|
|
|
2023-03-10 16:36:58 +08:00
|
|
|
const { repository, model } = (<typeof FlowNodeModel>node.constructor).database.getCollection(collection);
|
2023-05-16 09:45:45 +08:00
|
|
|
const options = processor.getParsedValue(params, node);
|
2023-03-10 16:36:58 +08:00
|
|
|
const result = await repository.create({
|
2022-04-14 00:05:13 +08:00
|
|
|
...options,
|
2022-11-15 13:35:10 +08:00
|
|
|
context: {
|
2023-04-25 13:12:14 +08:00
|
|
|
executionId: processor.execution.id,
|
2022-11-15 13:35:10 +08:00
|
|
|
},
|
2023-04-25 13:12:14 +08:00
|
|
|
transaction: processor.transaction,
|
2022-02-20 21:23:39 +08:00
|
|
|
});
|
|
|
|
|
2023-03-24 23:54:07 +08:00
|
|
|
if (result && appends.length) {
|
2023-04-25 13:12:14 +08:00
|
|
|
const includeFields = appends.filter((field) => !result.get(field) || !result[field]);
|
2023-03-10 16:36:58 +08:00
|
|
|
const included = await model.findByPk(result[model.primaryKeyAttribute], {
|
|
|
|
attributes: [model.primaryKeyAttribute],
|
|
|
|
include: includeFields,
|
2023-04-25 13:12:14 +08:00
|
|
|
transaction: processor.transaction,
|
2023-03-10 16:36:58 +08:00
|
|
|
});
|
2023-04-25 13:12:14 +08:00
|
|
|
includeFields.forEach((field) => {
|
2023-03-24 23:54:07 +08:00
|
|
|
const value = included!.get(field);
|
2023-04-25 13:12:14 +08:00
|
|
|
result.set(field, Array.isArray(value) ? value.map((item) => item.toJSON()) : value.toJSON(), { raw: true });
|
2023-03-10 16:36:58 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-20 21:23:39 +08:00
|
|
|
return {
|
2022-05-13 13:26:49 +08:00
|
|
|
// NOTE: get() for non-proxied instance (#380)
|
2023-03-24 23:54:07 +08:00
|
|
|
result: result?.toJSON(),
|
2023-04-25 13:12:14 +08:00
|
|
|
status: JOB_STATUS.RESOLVED,
|
2022-02-20 21:23:39 +08:00
|
|
|
};
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
|
|
|
};
|