116 lines
3.9 KiB
TypeScript
116 lines
3.9 KiB
TypeScript
import { Filter } from './../oak-app-domain/Essay/Schema';
|
||
import { IdAction } from './../oak-app-domain/User/Action';
|
||
import { EntityDict } from '@oak-app-domain';
|
||
import { RemoveTrigger, Trigger } from 'oak-domain/lib/types';
|
||
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
|
||
import assert from 'assert';
|
||
import Operation from 'antd/es/transfer/operation';
|
||
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
|
||
const triggers: Trigger<EntityDict, 'essay', BackendRuntimeContext>[] = [
|
||
{
|
||
name: '创建essay时自动补全内容',
|
||
entity: 'essay',
|
||
action: 'create',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
assert(data && !(data instanceof Array));
|
||
// 若不存在meta,则初始化一个空对象
|
||
data.meta = data.meta || {};
|
||
// data.isTop = data.isTop || false;
|
||
assert(data.content, 'content is required');
|
||
if (!data.summary) {
|
||
// 自动截取前面100个字符,加上省略号
|
||
data.summary =
|
||
data.content.length > 100
|
||
? data.content.slice(0, 100) + '...'
|
||
: data.content;
|
||
}
|
||
return 1;
|
||
},
|
||
},
|
||
// action: setTop的时候设置isTop为true
|
||
{
|
||
name: '置顶时设置isTop为true',
|
||
entity: 'essay',
|
||
action: 'setTop',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
assert(data && !(data instanceof Array));
|
||
// data.isTop = true;
|
||
return 1;
|
||
},
|
||
},
|
||
// action: cancelTop的时候设置isTop为false
|
||
{
|
||
name: '取消置顶时设置isTop为false',
|
||
entity: 'essay',
|
||
action: 'cancelTop',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
assert(data && !(data instanceof Array));
|
||
// data.isTop = false;
|
||
return 1;
|
||
},
|
||
},
|
||
// 调用like的action的时候,创建点赞的记录
|
||
{
|
||
name: '调用like的action的时候,创建点赞的记录',
|
||
entity: 'essay',
|
||
action: 'like',
|
||
when: 'before',
|
||
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
assert(filter && filter.id, "filter is required");
|
||
const essayId = filter.id;
|
||
const userId = context.getCurrentUserId();
|
||
//创建点赞记录
|
||
const opers=await context.operate("like", {
|
||
id: await generateNewIdAsync(),
|
||
action: "create",
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
essayId : essayId as string,
|
||
userId,
|
||
|
||
}
|
||
},option)
|
||
|
||
return opers.like?.create||0;
|
||
},
|
||
},
|
||
// 调用unlike的action的时候,移除点赞的记录
|
||
{
|
||
name: '调用like的action的时候,删除点赞的记录',
|
||
entity: 'essay',
|
||
action: 'unlike',
|
||
when: 'before',
|
||
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter, data} = operation;
|
||
assert(filter && filter.id, "filter is required");
|
||
const essayId = filter.id;
|
||
const userId = context.getCurrentUserId();
|
||
//创建点赞记录
|
||
const opers=await context.operate("like", {
|
||
id: await generateNewIdAsync(),
|
||
action: "remove",
|
||
data: {
|
||
},
|
||
filter: {
|
||
essayId,
|
||
userId,
|
||
}
|
||
},option)
|
||
|
||
return opers.like?.remove||0;
|
||
},
|
||
},
|
||
];
|
||
|
||
export default triggers;
|