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

269 lines
9.5 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 types_1 = require("oak-domain/lib/types");
const assert_1 = require("oak-domain/lib/utils/assert");
const uuid_1 = require("oak-domain/lib/utils/uuid");
const lodash_1 = require("oak-domain/lib/utils/lodash");
const constants_1 = require("../config/constants");
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,
articleMenu: {
id: 1,
isArticle: 1,
}
},
filter: {
id: data.id,
},
}, {});
const { articleMenuId, articleMenu } = article;
if (!articleMenu?.isArticle) {
await context.operate('articleMenu', {
id: await (0, uuid_1.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 (0, uuid_1.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 (0, uuid_1.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 = (0, lodash_1.uniq)(articles.map((ele) => ele.articleMenuId));
for (const articleMenuId of articleMenuIds) {
if (articleMenuId) {
await context.operate('articleMenu', {
id: await (0, uuid_1.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 = (0, lodash_1.uniq)(articles.map((ele) => ele.articleMenuId));
const now = Date.now();
for (const articleMenuId of articleMenuIds) {
if (articleMenuId) {
await context.operate('articleMenu', {
id: await (0, uuid_1.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;
(0, assert_1.assert)(data);
const { id: articleId } = data;
if (articleId) {
const selectionId = await (0, uuid_1.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, `${constants_1.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, `${constants_1.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 types_1.OakRowInconsistencyException('error::article.cantUpdateAfterDelete', "oak-general-business");
}
return 1;
}
},
];
exports.default = triggers;