267 lines
9.3 KiB
JavaScript
267 lines
9.3 KiB
JavaScript
import { OakRowInconsistencyException } from 'oak-domain/lib/types';
|
||
import { assert } from 'oak-domain/lib/utils/assert';
|
||
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import { uniq } from 'oak-domain/lib/utils/lodash';
|
||
import { DATA_SUBSCRIBER_KEYS } from '../config/constants';
|
||
const triggers = [
|
||
{
|
||
name: '在创建文章后,将文章所属分类的【isArticle】置为【true】',
|
||
entity: 'article',
|
||
action: 'create',
|
||
when: 'after',
|
||
fn: async (event, context) => {
|
||
const { operation: { data, filter }, } = event;
|
||
assert(!(data instanceof Array)); // 不可能是成组创建
|
||
if (data) {
|
||
const [article] = await context.select('article', {
|
||
data: {
|
||
id: 1,
|
||
name: 1,
|
||
content: 1,
|
||
articleMenuId: 1,
|
||
articleMenu: {
|
||
id: 1,
|
||
isArticle: 1,
|
||
}
|
||
},
|
||
filter: {
|
||
id: data.id,
|
||
},
|
||
}, {});
|
||
const { articleMenuId, articleMenu } = article;
|
||
if (!articleMenu?.isArticle) {
|
||
await context.operate('articleMenu', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
isArticle: true,
|
||
},
|
||
filter: {
|
||
id: articleMenuId,
|
||
},
|
||
}, {});
|
||
}
|
||
}
|
||
return 0;
|
||
},
|
||
},
|
||
{
|
||
name: '在删除文章前,将文章所属分类的【isArticle】置为【false】',
|
||
entity: 'article',
|
||
action: 'remove',
|
||
when: 'before',
|
||
fn: async (event, context) => {
|
||
const { operation: { data, filter }, } = event;
|
||
const [article] = await context.select('article', {
|
||
data: {
|
||
id: 1,
|
||
name: 1,
|
||
content: 1,
|
||
articleMenuId: 1,
|
||
},
|
||
filter,
|
||
}, {});
|
||
if (article) {
|
||
const articles = await context.select('article', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
articleMenuId: article.articleMenuId,
|
||
id: {
|
||
$ne: article.id,
|
||
},
|
||
},
|
||
}, {});
|
||
if (articles.length === 0) {
|
||
await context.operate('articleMenu', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
isArticle: false,
|
||
},
|
||
filter: {
|
||
id: article.articleMenuId,
|
||
},
|
||
}, {});
|
||
}
|
||
}
|
||
return 0;
|
||
},
|
||
},
|
||
{
|
||
name: '创建文章时,更新文章所属分类的最近编辑时间',
|
||
entity: 'article',
|
||
action: 'create',
|
||
when: 'after',
|
||
priority: 99,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
await context.operate('articleMenu', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
latestAt: Date.now(),
|
||
},
|
||
filter: {
|
||
id: data.articleMenuId,
|
||
},
|
||
}, {});
|
||
return 1;
|
||
},
|
||
},
|
||
{
|
||
name: '文章更新时,更新文章所属分类的最近编辑时间',
|
||
entity: 'article',
|
||
action: 'update',
|
||
when: 'before',
|
||
attributes: ['$$updateAt$$'],
|
||
check: (operation) => {
|
||
const { data } = operation;
|
||
return typeof data.$$updateAt$$ === 'number' && data.$$updateAt$$ > 0;
|
||
},
|
||
priority: 99,
|
||
asRoot: true,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { data, filter } = operation;
|
||
const articles = await context.select('article', {
|
||
data: {
|
||
id: 1,
|
||
articleMenuId: 1,
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
let count = 0;
|
||
if (articles && articles.length > 0) {
|
||
const articleMenuIds = uniq(articles.map((ele) => ele.articleMenuId));
|
||
for (const articleMenuId of articleMenuIds) {
|
||
if (articleMenuId) {
|
||
await context.operate('articleMenu', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
latestAt: data.$$updateAt$$,
|
||
},
|
||
filter: {
|
||
id: articleMenuId,
|
||
},
|
||
}, {});
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
return count;
|
||
},
|
||
},
|
||
{
|
||
name: '在删除文章前,更新文章所属分类的最近编辑时间',
|
||
entity: 'article',
|
||
action: 'remove',
|
||
when: 'before',
|
||
priority: 99,
|
||
asRoot: true,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { data, filter } = operation;
|
||
const articles = await context.select('article', {
|
||
data: {
|
||
id: 1,
|
||
articleMenuId: 1,
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
let count = 0;
|
||
if (articles && articles.length > 0) {
|
||
const articleMenuIds = uniq(articles.map((ele) => ele.articleMenuId));
|
||
const now = Date.now();
|
||
for (const articleMenuId of articleMenuIds) {
|
||
if (articleMenuId) {
|
||
await context.operate('articleMenu', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
latestAt: now,
|
||
},
|
||
filter: {
|
||
id: articleMenuId,
|
||
},
|
||
}, {});
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
},
|
||
{
|
||
name: '当article创建时,通知相应的事件订阅',
|
||
entity: 'article',
|
||
action: 'create',
|
||
when: 'after',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
assert(data);
|
||
const { id: articleId } = data;
|
||
if (articleId) {
|
||
const selectionId = await generateNewIdAsync();
|
||
const [article] = await context.select('article', {
|
||
id: selectionId,
|
||
data: {
|
||
id: 1,
|
||
name: 1,
|
||
$$updateAt$$: 1,
|
||
articleMenuId: 1,
|
||
articleMenu: {
|
||
id: 1,
|
||
entity: 1,
|
||
entityId: 1,
|
||
isArticle: 1,
|
||
parentId: 1,
|
||
latestAt: 1,
|
||
}
|
||
},
|
||
filter: {
|
||
id: articleId,
|
||
}
|
||
}, {});
|
||
const entityId = article?.articleMenu?.entityId;
|
||
if (entityId) {
|
||
context.saveOperationToEvent(selectionId, `${DATA_SUBSCRIBER_KEYS.articleCreate}-${entityId}`);
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
},
|
||
{
|
||
name: '当article更新时,通知相应的事件订阅',
|
||
entity: 'article',
|
||
action: 'update',
|
||
when: 'before',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, filter } = operation;
|
||
const { id: articleId } = filter;
|
||
if (articleId) {
|
||
context.saveOperationToEvent(id, `${DATA_SUBSCRIBER_KEYS.articleUpdate}-${articleId}`);
|
||
}
|
||
return 1;
|
||
}
|
||
},
|
||
{
|
||
name: '当article更新前检查是否被删除',
|
||
entity: 'article',
|
||
action: 'update',
|
||
when: 'before',
|
||
priority: 1,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter, data } = operation;
|
||
const count = await context.count('article', {
|
||
filter,
|
||
}, { forUpdate: true });
|
||
if (count === 0) {
|
||
throw new OakRowInconsistencyException('error::article.cantUpdateAfterDelete', "oak-general-business");
|
||
}
|
||
return 1;
|
||
}
|
||
},
|
||
];
|
||
export default triggers;
|