oak-general-business/es/triggers/system.js

166 lines
6.6 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 { desc } from '../oak-app-domain/Passport/Storage';
import { assert } from 'oak-domain/lib/utils/assert';
import { isEmail } from 'oak-domain/lib/utils/validator';
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 = [
//仅创建短信与密码的passport,其他type由相应的application/Email的更新来创建
{
//短信登录默认为模拟发送
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
type: 'sms',
config: {
mockSend: true,
},
enabled: true,
}
},
{
//密码登录
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
type: 'password',
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;
},
},
{
name: '当system的config中email更新时创建/关闭相关的passport',
entity: 'system',
action: 'update',
when: 'after',
check: (operation) => {
const { data } = operation;
return !!data.config;
},
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const systems = await context.select('system', {
data: {
id: 1,
config: 1,
},
filter,
}, {});
let count = 0;
for (const system of systems) {
const { Emails } = system.config || {};
let removeFilter = {
systemId: system.id,
type: 'email',
};
if (Emails && Emails.length > 0) {
for (const email of Emails) {
const { host, port, account, password, } = email;
if (account && isEmail(account)) {
const [passport] = await context.select('passport', {
data: {
id: 1,
enabled: 1,
},
filter: {
systemId: system.id,
type: 'email',
config: {
account,
}
},
}, { forUpdate: true });
if (host && host !== '' && port && password && password !== '') {
if (!passport) {
//创建account为该email的passport
await context.operate('passport', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
type: 'email',
enabled: false,
config: {
account,
subject: '',
},
systemId: system.id,
},
}, option);
count++;
}
}
else {
if (passport && passport.enabled) {
//当前account的email设置不全将启用了的passport关闭
await context.operate('passport', {
id: await generateNewIdAsync(),
action: 'update',
data: {
enabled: false,
},
filter: {
id: passport.id,
}
}, option);
count++;
}
}
}
}
}
const emailAccounts = Emails?.filter((ele) => !!ele.account).map((ele) => ele.account);
if (emailAccounts && emailAccounts.length > 0) {
Object.assign(removeFilter, {
config: {
account: {
$nin: emailAccounts
}
}
});
}
//删除passport
await context.operate('passport', {
id: await generateNewIdAsync(),
action: 'remove',
data: {},
filter: removeFilter,
}, option);
}
return count;
}
}
];
export default triggers;