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

88 lines
3.0 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.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("oak-domain/lib/utils/assert");
const uuid_1 = require("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;
(0, assert_1.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 (0, uuid_1.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 (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
isArticle: false,
},
filter: {
id: article.articleMenuId,
},
}, {});
}
}
return 0;
},
},
];
exports.default = triggers;