103 lines
3.6 KiB
JavaScript
103 lines
3.6 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||
const triggers = [
|
||
{
|
||
name: '当创建userEntityGrant时,尝试为之创建一个wechatQrCode',
|
||
entity: 'userEntityGrant',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, params) => {
|
||
const { data, filter } = operation;
|
||
const fn = async (userEntityGrantData) => {
|
||
const { userId } = context.getToken();
|
||
(0, assert_1.assert)(userId);
|
||
const { id, claimUrl } = userEntityGrantData;
|
||
Object.assign(userEntityGrantData, {
|
||
granterId: userId,
|
||
expired: false,
|
||
});
|
||
if (!userEntityGrantData.expiresAt) {
|
||
Object.assign(userEntityGrantData, {
|
||
expiresAt: Date.now() + 300 * 1000,
|
||
});
|
||
}
|
||
userEntityGrantData.wechatQrCode$entity = [
|
||
{
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
props: {
|
||
pathname: claimUrl || '/userEntityGrant/claim',
|
||
props: {
|
||
oakId: id,
|
||
},
|
||
},
|
||
type: userEntityGrantData.qrCodeType,
|
||
},
|
||
}
|
||
];
|
||
};
|
||
if (data instanceof Array) {
|
||
(0, assert_1.assert)('授权不存在一对多的情况');
|
||
}
|
||
else {
|
||
await fn(data);
|
||
}
|
||
return 0;
|
||
},
|
||
},
|
||
{
|
||
name: '当userEntityGrant过期时,使其相关的wechatQrCode也过期',
|
||
entity: 'userEntityGrant',
|
||
action: ['update', 'claim'],
|
||
check: (operation) => {
|
||
const { data } = operation;
|
||
return !!data.expired;
|
||
},
|
||
priority: 32,
|
||
when: 'before',
|
||
fn: async ({ operation }, context) => {
|
||
const { data, filter } = operation;
|
||
data.wechatQrCode$entity = [
|
||
{
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'update',
|
||
data: {
|
||
expired: true,
|
||
expiresAt: Date.now(),
|
||
},
|
||
}
|
||
];
|
||
return 1;
|
||
},
|
||
},
|
||
{
|
||
name: '当claim一个userEntityGrant时,如果是单次的,则将之过期',
|
||
entity: 'userEntityGrant',
|
||
action: 'claim',
|
||
when: 'before',
|
||
priority: 13,
|
||
fn: async ({ operation }, context) => {
|
||
const { data, filter } = operation;
|
||
(0, assert_1.assert)(typeof filter.id === 'string');
|
||
const [userEntityGrant] = await context.select('userEntityGrant', {
|
||
data: {
|
||
id: 1,
|
||
multiple: 1,
|
||
},
|
||
filter
|
||
}, { dontCollect: true });
|
||
if (!userEntityGrant.multiple) {
|
||
data.expired = true;
|
||
data.expiresAt = Date.now();
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
}
|
||
];
|
||
exports.default = triggers;
|