UNPKG

@wepublish/api

Version:
109 lines 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.onFindPage = exports.onFindArticle = void 0; const tslib_1 = require("tslib"); // @TODO: move into cron job const onFindArticle = (prisma) => (params, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; if (!(params.model === 'Article' && params.action.startsWith('find'))) { return next(params); } // skip the call inside this middleware to not create an infinite loop if ((_f = (_e = (_d = (_c = (_b = (_a = params.args) === null || _a === void 0 ? void 0 : _a.where) === null || _b === void 0 ? void 0 : _b.pending) === null || _c === void 0 ? void 0 : _c.is) === null || _d === void 0 ? void 0 : _d.AND) === null || _e === void 0 ? void 0 : _e.publishAt) === null || _f === void 0 ? void 0 : _f.lte) { return next(params); } const articles = yield prisma.article.findMany({ where: { pending: { is: { AND: { publishAt: { lte: new Date() } } } } }, include: { pending: true, published: true } }); yield Promise.all(articles.map(({ id, publishedId, pending }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield prisma.article.update({ where: { id }, data: { published: { connect: { id: pending.id } }, pending: { disconnect: true } } }); yield prisma.articleRevision.delete({ where: { id: publishedId || undefined } }); }))); return next(params); }); exports.onFindArticle = onFindArticle; // @TODO: move into cron job const onFindPage = (prisma) => (params, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _g, _h, _j, _k, _l, _m; if (!(params.model === 'Page' && params.action.startsWith('find'))) { return next(params); } // skip the call inside this middleware to not create an infinite loop if ((_m = (_l = (_k = (_j = (_h = (_g = params.args) === null || _g === void 0 ? void 0 : _g.where) === null || _h === void 0 ? void 0 : _h.pending) === null || _j === void 0 ? void 0 : _j.is) === null || _k === void 0 ? void 0 : _k.AND) === null || _l === void 0 ? void 0 : _l.publishAt) === null || _m === void 0 ? void 0 : _m.lte) { return next(params); } const pages = yield prisma.page.findMany({ where: { pending: { is: { AND: { publishAt: { lte: new Date() } } } } }, include: { pending: true, published: true } }); yield Promise.all(pages.map(({ id, publishedId, pending }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield prisma.page.update({ where: { id }, data: { published: { connect: { id: pending.id } }, pending: { disconnect: true } } }); yield prisma.pageRevision.delete({ where: { id: publishedId || undefined } }); }))); return next(params); }); exports.onFindPage = onFindPage; //# sourceMappingURL=events.js.map