oak-general-business/es/triggers/article.js

86 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { assert } from 'oak-domain/lib/utils/assert';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
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,
},
filter: {
id: data.id,
},
}, {});
if (article) {
await context.operate('articleMenu', {
id: await generateNewIdAsync(),
action: 'update',
data: {
isArticle: true,
},
filter: {
id: article.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;
},
},
];
export default triggers;