UNPKG

@wepublish/api

Version:
399 lines 17.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateArticle = exports.publishArticle = exports.unpublishArticle = exports.duplicateArticle = exports.createArticle = exports.deleteArticleById = void 0; const tslib_1 = require("tslib"); const client_1 = require("@prisma/client"); const error_1 = require("../../error"); const permissions_1 = require("../permissions"); const api_1 = require("../../../../permissions-api/src"); const fullArticleInclude = { draft: { include: { properties: true, authors: true, socialMediaAuthors: true } }, pending: { include: { properties: true, authors: true, socialMediaAuthors: true } }, published: { include: { properties: true, authors: true, socialMediaAuthors: true } } }; const deleteArticleById = (id, authenticate, prisma) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanDeleteArticle, roles); const article = yield prisma.article.findUnique({ where: { id }, include: fullArticleInclude }); if (!article) { throw new error_1.NotFound('article', id); } yield prisma.$transaction([ prisma.article.delete({ where: { id } }), prisma.articleRevision.deleteMany({ where: { id: { in: [article.draftId, article.pendingId, article.publishedId].filter(Boolean) } } }) ]); return article; }); exports.deleteArticleById = deleteArticleById; const createArticle = (input, authenticate, article) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanCreateArticle, roles); const { shared, hidden, properties, authorIDs, socialMediaAuthorIDs } = input, data = tslib_1.__rest(input, ["shared", "hidden", "properties", "authorIDs", "socialMediaAuthorIDs"]); return article.create({ data: { shared, hidden, draft: { create: Object.assign(Object.assign({}, data), { properties: { createMany: { data: properties } }, authors: { createMany: { data: authorIDs.map(authorId => ({ authorId })) } }, socialMediaAuthors: { createMany: { data: socialMediaAuthorIDs.map(authorId => ({ authorId })) } }, revision: 0 }) } }, include: fullArticleInclude }); }); exports.createArticle = createArticle; const duplicateArticle = (id, authenticate, articles, articleClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a, _b; const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanCreateArticle, roles); const article = yield articles.load(id); if (!article) { throw new error_1.NotFound('article', id); } const _c = ((_b = (_a = article.draft) !== null && _a !== void 0 ? _a : article.pending) !== null && _b !== void 0 ? _b : article.published), { id: _id, updatedAt: _updatedAt, createdAt: _createdAt, publishedAt: _publishedAt, slug: _slug, properties, authors, socialMediaAuthors } = _c, articleRevision = tslib_1.__rest(_c, ["id", "updatedAt", "createdAt", "publishedAt", "slug", "properties", "authors", "socialMediaAuthors"]); const duplicatedProperties = properties.map(property => ({ key: property.key, value: property.value, public: property.public })); const input = Object.assign(Object.assign({}, articleRevision), { blocks: articleRevision.blocks || client_1.Prisma.JsonNull, properties: { createMany: { data: duplicatedProperties } }, authors: { createMany: { data: authors.map(({ authorId }) => ({ authorId })) } }, socialMediaAuthors: { createMany: { data: socialMediaAuthors.map(({ authorId }) => ({ authorId })) } } }); return articleClient.create({ data: { shared: article.shared, hidden: article.hidden, draft: { create: input } }, include: fullArticleInclude }); }); exports.duplicateArticle = duplicateArticle; const unpublishArticle = (id, authenticate, articleClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _d, _e; const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanPublishArticle, roles); const article = yield articleClient.findUnique({ where: { id }, include: fullArticleInclude }); if (!article) { throw new error_1.NotFound('article', id); } const _f = ((_e = (_d = article.draft) !== null && _d !== void 0 ? _d : article.pending) !== null && _e !== void 0 ? _e : article.published), { id: revisionId, properties, authors, socialMediaAuthors } = _f, revision = tslib_1.__rest(_f, ["id", "properties", "authors", "socialMediaAuthors"]); const duplicatedProperties = properties.map(property => ({ key: property.key, value: property.value, public: property.public })); return articleClient.update({ where: { id }, data: { draft: { upsert: { create: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishAt: null, publishedAt: null, updatedAt: null, properties: { createMany: { data: duplicatedProperties } }, authors: { createMany: { data: authors.map(({ authorId }) => ({ authorId })) } }, socialMediaAuthors: { createMany: { data: socialMediaAuthors.map(({ authorId }) => ({ authorId })) } } }), update: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishAt: null, publishedAt: null, updatedAt: null, properties: { deleteMany: { articleRevisionId: revisionId }, createMany: { data: duplicatedProperties } }, authors: { deleteMany: { revisionId }, createMany: { data: authors.map(({ authorId }) => ({ authorId })) } }, socialMediaAuthors: { deleteMany: { revisionId }, createMany: { data: socialMediaAuthors.map(({ authorId }) => ({ authorId })) } } }) } }, pending: { delete: Boolean(article.pendingId) }, published: { delete: Boolean(article.publishedId) } }, include: fullArticleInclude }); }); exports.unpublishArticle = unpublishArticle; const publishArticle = (id, input, authenticate, prisma) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _g, _h, _j, _k, _l, _m, _o, _p; const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanPublishArticle, roles); const publishAt = (_g = input.publishAt) !== null && _g !== void 0 ? _g : new Date(); const publishedAt = input.publishedAt; const updatedAt = input.updatedAt; const article = yield prisma.article.findUnique({ where: { id }, include: fullArticleInclude }); if (!article) throw new error_1.NotFound('article', id); if (!article.draft) return null; const _q = article.draft, { id: revisionId, properties, authors, socialMediaAuthors } = _q, revision = tslib_1.__rest(_q, ["id", "properties", "authors", "socialMediaAuthors"]); const duplicatedProperties = properties.map(property => ({ key: property.key, value: property.value, public: property.public })); const publishedArticle = yield prisma.article.findFirst({ where: { OR: [ { published: { is: { slug: revision.slug } } }, { pending: { is: { slug: revision.slug } } } ] }, include: { pending: true, published: true } }); if (publishedArticle && publishedArticle.id !== id) { throw new error_1.DuplicateArticleSlugError(publishedArticle.id, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion (publishedArticle.published || publishedArticle.pending).slug); } if (publishAt > new Date()) { const deletedOldRevisions = prisma.articleRevision.deleteMany({ where: { id: { in: [(_h = article.pending) === null || _h === void 0 ? void 0 : _h.id].filter((id) => Boolean(id)) } } }); const [, updatedArticle] = yield prisma.$transaction([ deletedOldRevisions, prisma.article.update({ where: { id }, data: { pending: { create: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishAt, publishedAt: (_k = publishedAt !== null && publishedAt !== void 0 ? publishedAt : (_j = article === null || article === void 0 ? void 0 : article.published) === null || _j === void 0 ? void 0 : _j.publishedAt) !== null && _k !== void 0 ? _k : publishAt, updatedAt: updatedAt !== null && updatedAt !== void 0 ? updatedAt : publishAt, properties: { createMany: { data: duplicatedProperties } }, authors: { createMany: { data: authors.map(({ authorId }) => ({ authorId })) } }, socialMediaAuthors: { createMany: { data: socialMediaAuthors.map(({ authorId }) => ({ authorId })) } } }) }, draft: { delete: true } }, include: fullArticleInclude }) ]); return updatedArticle; } const deletedOldRevisions = prisma.articleRevision.deleteMany({ where: { id: { in: [(_l = article.pending) === null || _l === void 0 ? void 0 : _l.id, (_m = article.published) === null || _m === void 0 ? void 0 : _m.id].filter((id) => Boolean(id)) } } }); const [, updatedArticle] = yield prisma.$transaction([ deletedOldRevisions, prisma.article.update({ where: { id }, data: { published: { create: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishedAt: (_p = publishedAt !== null && publishedAt !== void 0 ? publishedAt : (_o = article.published) === null || _o === void 0 ? void 0 : _o.publishAt) !== null && _p !== void 0 ? _p : publishAt, updatedAt: updatedAt !== null && updatedAt !== void 0 ? updatedAt : publishAt, publishAt: null, properties: { createMany: { data: duplicatedProperties } }, authors: { createMany: { data: authors.map(({ authorId }) => ({ authorId })) } }, socialMediaAuthors: { createMany: { data: socialMediaAuthors.map(({ authorId }) => ({ authorId })) } } }) }, draft: { delete: true } }, include: fullArticleInclude }) ]); return updatedArticle; }); exports.publishArticle = publishArticle; const updateArticle = (id, _r, authenticate, articleClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _s, _t, _u, _v, _w, _x, _y, _z, _0, _1; var { properties, authorIDs, socialMediaAuthorIDs, shared, hidden } = _r, input = tslib_1.__rest(_r, ["properties", "authorIDs", "socialMediaAuthorIDs", "shared", "hidden"]); const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanCreateArticle, roles); const article = yield articleClient.findUnique({ where: { id }, include: fullArticleInclude }); if (!article) { throw new error_1.NotFound('article', id); } return articleClient.update({ where: { id }, data: { shared, hidden, draft: { upsert: { update: Object.assign(Object.assign({}, input), { revision: article.pending ? article.pending.revision + 1 : article.published ? article.published.revision + 1 : 0, updatedAt: null, createdAt: (_t = (_s = article.draft) === null || _s === void 0 ? void 0 : _s.createdAt) !== null && _t !== void 0 ? _t : new Date(), properties: { deleteMany: { articleRevisionId: (_v = (_u = article.draft) === null || _u === void 0 ? void 0 : _u.id) !== null && _v !== void 0 ? _v : '' }, createMany: { data: properties.map(property => ({ key: property.key, value: property.value, public: property.public })) } }, authors: { deleteMany: { revisionId: (_x = (_w = article.draft) === null || _w === void 0 ? void 0 : _w.id) !== null && _x !== void 0 ? _x : '' }, createMany: { data: authorIDs.map(authorId => ({ authorId })) } }, socialMediaAuthors: { deleteMany: { revisionId: (_z = (_y = article.draft) === null || _y === void 0 ? void 0 : _y.id) !== null && _z !== void 0 ? _z : '' }, createMany: { data: socialMediaAuthorIDs.map(authorId => ({ authorId })) } } }), create: Object.assign(Object.assign({}, input), { revision: article.pending ? article.pending.revision + 1 : article.published ? article.published.revision + 1 : 0, updatedAt: null, createdAt: (_1 = (_0 = article.draft) === null || _0 === void 0 ? void 0 : _0.createdAt) !== null && _1 !== void 0 ? _1 : new Date(), properties: { createMany: { data: properties.map(property => ({ key: property.key, value: property.value, public: property.public })) } }, authors: { createMany: { data: authorIDs.map(authorId => ({ authorId })) } }, socialMediaAuthors: { createMany: { data: socialMediaAuthorIDs.map(authorId => ({ authorId })) } } }) } } }, include: fullArticleInclude }); }); exports.updateArticle = updateArticle; //# sourceMappingURL=article.private-mutation.js.map