111 lines
4.1 KiB
TypeScript
111 lines
4.1 KiB
TypeScript
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import { Trigger, CreateTrigger, UpdateTrigger } from 'oak-domain/lib/types/Trigger';
|
||
import { CreateOperationData as CreateUserEntityGrantData } from '../oak-app-domain/UserEntityGrant/Schema';
|
||
import { EntityDict } from '../oak-app-domain/EntityDict';
|
||
|
||
import { OakRowInconsistencyException, OakUserException } from 'oak-domain/lib/types';
|
||
import { assert } from 'oak-domain/lib/utils/assert';
|
||
import { BRC } from '../types/RuntimeCxt';
|
||
|
||
const triggers: Trigger<EntityDict, 'userEntityGrant', BRC<EntityDict>>[] = [
|
||
{
|
||
name: '当创建userEntityGrant时,尝试为之创建一个wechatQrCode',
|
||
entity: 'userEntityGrant',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, params) => {
|
||
const { data, filter } = operation;
|
||
const fn = async (
|
||
userEntityGrantData: CreateUserEntityGrantData
|
||
) => {
|
||
const { userId } = context.getToken()!;
|
||
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 generateNewIdAsync(),
|
||
action: 'create',
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
props: {
|
||
pathname: claimUrl || '/userEntityGrant/claim',
|
||
props: {
|
||
oakId: id,
|
||
},
|
||
},
|
||
type: userEntityGrantData.qrCodeType,
|
||
},
|
||
}
|
||
];
|
||
};
|
||
if (data instanceof Array) {
|
||
assert('授权不存在一对多的情况');
|
||
} else {
|
||
await fn(data);
|
||
}
|
||
return 0;
|
||
},
|
||
} as CreateTrigger<EntityDict, 'userEntityGrant', BRC<EntityDict>>,
|
||
{
|
||
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 generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
expired: true,
|
||
expiresAt: Date.now(),
|
||
},
|
||
}
|
||
];
|
||
|
||
return 1;
|
||
},
|
||
} as UpdateTrigger<EntityDict, 'userEntityGrant', BRC<EntityDict>>,
|
||
{
|
||
name: '当claim一个userEntityGrant时,如果是单次的,则将之过期',
|
||
entity: 'userEntityGrant',
|
||
action: 'claim',
|
||
when: 'before',
|
||
priority: 13,
|
||
fn: async ({ operation }, context) => {
|
||
const { data, filter } = operation as EntityDict['userEntityGrant']['Update'];
|
||
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;
|
||
}
|
||
}
|
||
];
|
||
export default triggers; |