46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import { desc } from '../oak-app-domain/Passport/Storage';
|
||
import { assert } from 'oak-domain/lib/utils/assert';
|
||
const { attributes: { type: passportType } } = desc;
|
||
const triggers = [
|
||
{
|
||
name: '添加system时,添加相应的passport',
|
||
entity: 'system',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }) => {
|
||
const { data } = operation;
|
||
assert(!(data instanceof Array));
|
||
data.passport$system = await Promise.all(passportType.enumeration.map(async (type) => ({
|
||
id: await generateNewIdAsync(),
|
||
action: 'create',
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
type,
|
||
enabled: false,
|
||
}
|
||
})));
|
||
return 1;
|
||
}
|
||
},
|
||
{
|
||
name: '当system删除前,删除相关的passports',
|
||
entity: 'system',
|
||
action: 'remove',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
await context.operate('passport', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
system: filter,
|
||
},
|
||
}, option);
|
||
return 1;
|
||
},
|
||
},
|
||
];
|
||
export default triggers;
|