545 lines
22 KiB
JavaScript
545 lines
22 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||
const triggers = [
|
||
{
|
||
name: '删除application前,将关联的applicationPassport删除',
|
||
entity: 'application',
|
||
action: 'remove',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
await context.operate('applicationPassport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
application: filter,
|
||
},
|
||
}, option);
|
||
return 1;
|
||
}
|
||
},
|
||
{
|
||
name: 'web applicaiton清空微信网站配置时,将相应的applicationPassport删除',
|
||
entity: 'application',
|
||
action: 'update',
|
||
when: 'before',
|
||
check: (operation) => {
|
||
const { data } = operation;
|
||
return !!data.config;
|
||
},
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter, data } = operation;
|
||
const applications = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
config: 1,
|
||
type: 1,
|
||
systemId: 1,
|
||
},
|
||
filter,
|
||
}, {});
|
||
let count = 0;
|
||
for (const application of applications) {
|
||
if (application.type === 'web') {
|
||
const { appId: newAppId, appSecret: newAppSecret } = data?.config?.wechat || {};
|
||
if (!newAppId || !newAppSecret) {
|
||
const [passport] = await context.select('passport', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
enabled: true,
|
||
systemId: application.systemId,
|
||
type: 'wechatWeb',
|
||
},
|
||
count: 1,
|
||
indexFrom: 0,
|
||
}, { forUpdate: true });
|
||
if (passport) {
|
||
await context.operate('applicationPassport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
passportId: passport.id,
|
||
applicationId: application.id,
|
||
},
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
},
|
||
{
|
||
name: 'wechatPublic/wechatMp application更新appId后,创建/关闭相关的passport',
|
||
entity: 'application',
|
||
action: 'update',
|
||
when: 'after',
|
||
check: (operation) => {
|
||
const { data } = operation;
|
||
return !!data.config;
|
||
},
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
const applications = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
config: 1,
|
||
type: 1,
|
||
systemId: 1,
|
||
},
|
||
filter,
|
||
}, {});
|
||
let count = 0;
|
||
for (const application of applications) {
|
||
if (application.type === 'wechatPublic') {
|
||
const { appId, appSecret, isService } = application.config || {};
|
||
if (appId) {
|
||
const [passport] = await context.select('passport', {
|
||
data: {
|
||
id: 1,
|
||
enabled: 1,
|
||
},
|
||
filter: {
|
||
systemId: application.systemId,
|
||
type: 'wechatPublicForWeb',
|
||
config: {
|
||
appId: appId,
|
||
}
|
||
},
|
||
count: 1,
|
||
indexFrom: 0,
|
||
}, { forUpdate: true });
|
||
if (appSecret && isService) {
|
||
if (!passport) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
type: 'wechatPublicForWeb',
|
||
enabled: false,
|
||
config: {
|
||
appId,
|
||
},
|
||
systemId: application.systemId,
|
||
},
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
else {
|
||
//关闭已启用的passport
|
||
if (passport && passport.enabled) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'update',
|
||
data: {
|
||
enabled: false,
|
||
},
|
||
filter: {
|
||
id: passport.id,
|
||
}
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else if (application.type === 'wechatMp') {
|
||
const { appId, appSecret, } = application.config || {};
|
||
if (appId) {
|
||
const [passport] = await context.select('passport', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
systemId: application.systemId,
|
||
type: 'wechatMpForWeb',
|
||
config: {
|
||
appId: appId,
|
||
}
|
||
},
|
||
count: 1,
|
||
indexFrom: 0,
|
||
}, { forUpdate: true });
|
||
if (appSecret) {
|
||
if (!passport) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
type: 'wechatMpForWeb',
|
||
enabled: false,
|
||
config: {
|
||
appId,
|
||
},
|
||
systemId: application.systemId,
|
||
},
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
else {
|
||
if (passport) {
|
||
//关闭已启用的passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'update',
|
||
data: {
|
||
enabled: false,
|
||
},
|
||
filter: {
|
||
id: passport.id,
|
||
}
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//若清空了appId,则删除原来appId对应的passport
|
||
const systemId = context.getSystemId();
|
||
const allApplications = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
config: 1,
|
||
type: 1,
|
||
systemId: 1,
|
||
},
|
||
filter: {
|
||
systemId,
|
||
type: {
|
||
$in: ['wechatMp', 'wechatPublic']
|
||
}
|
||
}
|
||
}, {});
|
||
const pAppIds = allApplications.filter((app) => app.type === 'wechatPublic' && app.config.appId && app.config.appId !== '').map((ele) => ele.config.appId);
|
||
const mAppIds = allApplications.filter((app) => app.type === 'wechatMp' && app.config.appId && app.config.appId !== '').map((ele) => ele.config.appId);
|
||
let removePFilter = {
|
||
systemId,
|
||
type: 'wechatPublicForWeb',
|
||
}, removeMFilter = {
|
||
systemId,
|
||
type: 'wechatMpForWeb',
|
||
};
|
||
if (pAppIds && pAppIds.length > 0) {
|
||
Object.assign(removePFilter, {
|
||
config: {
|
||
appId: {
|
||
$nin: pAppIds
|
||
}
|
||
}
|
||
});
|
||
}
|
||
if (mAppIds && mAppIds.length > 0) {
|
||
Object.assign(removeMFilter, {
|
||
config: {
|
||
appId: {
|
||
$nin: mAppIds
|
||
}
|
||
}
|
||
});
|
||
}
|
||
//删除passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: removePFilter,
|
||
}, option);
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: removeMFilter,
|
||
}, option);
|
||
return count;
|
||
}
|
||
},
|
||
{
|
||
name: 'wechatMp applicaiton清空普通链接二维码规则配置时,将相应的passport禁用',
|
||
entity: 'application',
|
||
action: 'update',
|
||
when: 'after',
|
||
check: (operation) => {
|
||
const { data } = operation;
|
||
return !!data.config;
|
||
},
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
const applications = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
config: 1,
|
||
type: 1,
|
||
systemId: 1,
|
||
},
|
||
filter,
|
||
}, {});
|
||
let count = 0;
|
||
for (const application of applications) {
|
||
if (application.type === 'wechatMp') {
|
||
const { qrCodePrefix, appId } = application.config || {};
|
||
if (appId && !qrCodePrefix) {
|
||
const [passport] = await context.select('passport', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
enabled: true,
|
||
systemId: application.systemId,
|
||
type: 'wechatMpForWeb',
|
||
config: {
|
||
appId,
|
||
}
|
||
},
|
||
count: 1,
|
||
indexFrom: 0,
|
||
}, { forUpdate: true });
|
||
if (passport) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'update',
|
||
data: {
|
||
enabled: false,
|
||
config: {},
|
||
},
|
||
filter: {
|
||
id: passport.id,
|
||
}
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
},
|
||
{
|
||
name: '删除application前,将相关的passport删除',
|
||
entity: 'application',
|
||
action: 'remove',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
const applications = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
type: 1,
|
||
config: 1,
|
||
systemId: 1,
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
for (const application of applications) {
|
||
const { id, type, systemId } = application;
|
||
if (type === 'wechatMp') {
|
||
const { appId } = application.config;
|
||
//删除该小程序appId对应的类型为wechatMpForWeb的passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatMpForWeb',
|
||
config: {
|
||
appId,
|
||
}
|
||
}
|
||
}, option);
|
||
//若不存在其他小程序applicaiton,删除类型为wehcatMp的passport
|
||
const otherMpApp = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatMp',
|
||
id: {
|
||
$ne: id,
|
||
},
|
||
},
|
||
}, { forUpdate: true });
|
||
if (!(otherMpApp && otherMpApp.length > 0)) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatMp',
|
||
}
|
||
}, option);
|
||
}
|
||
}
|
||
else if (type === 'wechatPublic') {
|
||
const { appId } = application.config;
|
||
//删除该公众号appId对应的类型为wechatPublicForWeb的passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatPublicForWeb',
|
||
config: {
|
||
appId,
|
||
}
|
||
}
|
||
}, option);
|
||
//若不存在其他公众号applicaiton,删除类型为wehcatPublic的passport
|
||
const otherPublicApp = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatPublic',
|
||
id: {
|
||
$ne: id,
|
||
},
|
||
},
|
||
}, { forUpdate: true });
|
||
if (!(otherPublicApp && otherPublicApp.length > 0)) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatPublic',
|
||
}
|
||
}, option);
|
||
}
|
||
}
|
||
else if (type === 'web') {
|
||
//若不存在其他网站applicaiton,删除类型为wechatWeb的passport
|
||
const otherWebApp = await context.select('application', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
systemId,
|
||
type: 'web',
|
||
id: {
|
||
$ne: id,
|
||
},
|
||
},
|
||
}, { forUpdate: true });
|
||
if (!(otherWebApp && otherWebApp.length > 0)) {
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'remove',
|
||
data: {},
|
||
filter: {
|
||
systemId,
|
||
type: 'wechatWeb',
|
||
}
|
||
}, option);
|
||
}
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
},
|
||
{
|
||
name: '添加application时,添加相应的passport',
|
||
entity: 'application',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { data } = operation;
|
||
(0, assert_1.assert)(!(data instanceof Array));
|
||
const { type, systemId } = data;
|
||
const passports = await context.select('passport', {
|
||
data: {
|
||
id: 1,
|
||
type: 1,
|
||
},
|
||
filter: {
|
||
systemId,
|
||
}
|
||
}, { forUpdate: true });
|
||
const pTypes = (0, lodash_1.union)(passports?.map((ele) => ele.type));
|
||
if (type === 'web') {
|
||
if (!pTypes.includes('wechatWeb')) {
|
||
//类型为web的application,但不存在wechatWeb的passport时创建passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
systemId,
|
||
type: 'wechatWeb',
|
||
enabled: false,
|
||
},
|
||
}, option);
|
||
}
|
||
}
|
||
else if (type === 'wechatMp') {
|
||
if (!pTypes.includes('wechatMp')) {
|
||
//类型为webMp的application,但不存在wechatMp的passport时创建passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
systemId,
|
||
type: 'wechatMp',
|
||
enabled: false,
|
||
},
|
||
}, option);
|
||
}
|
||
}
|
||
else if (type === 'wechatPublic') {
|
||
if (!pTypes.includes('wechatPublic')) {
|
||
//类型为webMp的application,但不存在wechatMp的passport时创建passport
|
||
await context.operate('passport', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
systemId,
|
||
type: 'wechatPublic',
|
||
enabled: false,
|
||
},
|
||
}, option);
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
},
|
||
{
|
||
name: 'application创建时,为其config附上对应的type',
|
||
entity: 'application',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { data } = operation;
|
||
(0, assert_1.assert)(!(data instanceof Array));
|
||
const { type, config } = data;
|
||
const config2 = config || {};
|
||
if (!config?.type) {
|
||
Object.assign(config2, {
|
||
type: type,
|
||
});
|
||
}
|
||
data.config = config2;
|
||
return 1;
|
||
}
|
||
},
|
||
];
|
||
exports.default = triggers;
|