168 lines
6.8 KiB
JavaScript
168 lines
6.8 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const Storage_1 = require("../oak-app-domain/Passport/Storage");
|
||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||
const validator_1 = require("oak-domain/lib/utils/validator");
|
||
const { attributes: { type: passportType } } = Storage_1.desc;
|
||
const triggers = [
|
||
{
|
||
name: '添加system时,添加短信与密码的passport',
|
||
entity: 'system',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }) => {
|
||
const { data } = operation;
|
||
(0, assert_1.assert)(!(data instanceof Array));
|
||
data.passport$system = [
|
||
//仅创建短信与密码的passport,其他type由相应的application/Email的更新来创建
|
||
{
|
||
//短信登录默认为模拟发送
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
type: 'sms',
|
||
config: {
|
||
mockSend: true,
|
||
},
|
||
enabled: true,
|
||
}
|
||
},
|
||
{
|
||
//密码登录
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.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 (0, uuid_1.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 && (0, validator_1.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 (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.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 (0, uuid_1.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 (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: removeFilter,
|
||
}, option);
|
||
}
|
||
return count;
|
||
}
|
||
}
|
||
];
|
||
exports.default = triggers;
|