oak-pay-business/es/triggers/abstract.js

75 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import assert from 'assert';
// 这里一定要注意两者的先后顺序如果有注入更多的payEntity的话
import { accountEntities } from '../checkers/abstractChecker';
const triggers = [
...accountEntities.filter(ele => !!ele).map((entity) => [
{
name: `${entity}的帐户生成时则生成对应的withDrawAccount`,
entity,
action: 'create',
when: 'before',
fn: async ({ operation }) => {
const { data } = operation;
assert(!(data instanceof Array));
const data2 = data;
data2.withdrawChannel$entity = [
{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
enabled: !!data2.allowWithdrawTransfer,
systemId: data2.systemId,
}
}
];
return 1;
}
},
{
name: `${entity}的allowWithdrawTransfer发生改变时同时修改对应的withdrawChannel的enabled值`,
entity,
action: 'update',
when: 'before',
check(operation) {
return operation.data.hasOwnProperty('allowWithdrawTransfer');
},
fn: async ({ operation }) => {
const { data } = operation;
assert(!(data instanceof Array));
const data2 = data;
data2.withdrawChannel$entity = [
{
id: await generateNewIdAsync(),
action: data2.allowWithdrawTransfer ? 'enable' : 'disable',
data: {
enabled: !!data2.allowWithdrawTransfer,
}
}
];
return 1;
}
},
{
name: `${entity}的帐户的allowWithdrawTransfer发生改变时同时修改对应的withdrawChannel的enabled值`,
entity,
action: 'remove',
when: 'after',
fn: async ({ operation }, context) => {
const { filter } = operation;
await context.operate('withdrawChannel', {
id: await generateNewIdAsync(),
action: 'remove',
data: {},
filter: {
[entity]: filter,
}
}, { dontCollect: true });
return 1;
}
},
]).flat(),
];
export default triggers;