@wepublish/api
Version:
API core for we.publish.
479 lines • 17.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePage = exports.publishPage = exports.unpublishPage = exports.duplicatePage = exports.createPage = exports.deletePageById = 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 deletePageById = (id, authenticate, prisma) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanDeletePage, roles);
const page = yield prisma.page.findUnique({
where: {
id
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
if (!page) {
throw new error_1.NotFound('page', id);
}
yield prisma.$transaction([
prisma.page.delete({
where: {
id
}
}),
prisma.pageRevision.deleteMany({
where: {
id: {
in: [page.draftId, page.pendingId, page.publishedId].filter(Boolean)
}
}
})
]);
return page;
});
exports.deletePageById = deletePageById;
const createPage = (input, authenticate, page) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanCreatePage, roles);
const { properties } = input, data = tslib_1.__rest(input, ["properties"]);
return page.create({
data: {
draft: {
create: Object.assign(Object.assign({}, data), { properties: {
createMany: {
data: properties
}
}, revision: 0 })
}
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
});
exports.createPage = createPage;
const duplicatePage = (id, authenticate, pages, pageClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanCreatePage, roles);
const page = yield pages.load(id);
if (!page) {
throw new error_1.NotFound('page', id);
}
const _c = ((_b = (_a = page.draft) !== null && _a !== void 0 ? _a : page.pending) !== null && _b !== void 0 ? _b : page.published), { id: _id, updatedAt: _updatedAt, createdAt: _createdAt, publishedAt: _publishedAt, slug: _slug, properties } = _c, pageRevision = tslib_1.__rest(_c, ["id", "updatedAt", "createdAt", "publishedAt", "slug", "properties"]);
const duplicatedProperties = properties.map(property => ({
key: property.key,
value: property.value,
public: property.public
}));
const input = Object.assign(Object.assign({}, pageRevision), { blocks: pageRevision.blocks || client_1.Prisma.JsonNull, properties: {
createMany: {
data: duplicatedProperties
}
} });
return pageClient.create({
data: {
draft: {
create: input
}
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
});
exports.duplicatePage = duplicatePage;
const unpublishPage = (id, authenticate, pageClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _d, _e;
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanPublishPage, roles);
const page = yield pageClient.findUnique({
where: { id },
include: {
draft: {
include: {
properties: {
select: {
key: true,
value: true,
public: true
}
}
}
},
pending: {
include: {
properties: {
select: {
key: true,
value: true,
public: true
}
}
}
},
published: {
include: {
properties: {
select: {
key: true,
value: true,
public: true
}
}
}
}
}
});
if (!page || !(page.pending || page.published)) {
throw new error_1.NotFound('page', id);
}
const _f = ((_e = (_d = page.draft) !== null && _d !== void 0 ? _d : page.pending) !== null && _e !== void 0 ? _e : page.published), { id: revisionId, properties } = _f, revision = tslib_1.__rest(_f, ["id", "properties"]);
return pageClient.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: properties
}
} }),
update: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishAt: null, publishedAt: null, updatedAt: null, properties: {
deleteMany: {
pageRevisionId: revisionId
},
createMany: {
data: properties
}
} })
}
},
pending: {
delete: Boolean(page.pendingId)
},
published: {
delete: Boolean(page.publishedId)
}
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
});
exports.unpublishPage = unpublishPage;
const publishPage = (id, input, authenticate, pageClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _g, _h, _j, _k, _l, _m, _o, _p, _q;
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanPublishPage, roles);
const publishAt = (_g = input.publishAt) !== null && _g !== void 0 ? _g : new Date();
const publishedAt = input.publishedAt;
const updatedAt = input.updatedAt;
const page = yield pageClient.findUnique({
where: { id },
include: {
draft: {
include: {
properties: {
select: {
key: true,
value: true,
public: true
}
}
}
},
pending: {
include: {
properties: {
select: {
key: true,
value: true,
public: true
}
}
}
},
published: {
include: {
properties: {
select: {
key: true,
value: true,
public: true
}
}
}
}
}
});
if (!page)
throw new error_1.NotFound('page', id);
if (!page.draft)
return null;
const _r = page.draft, { id: revisionId, properties } = _r, revision = tslib_1.__rest(_r, ["id", "properties"]);
const publishedPage = yield pageClient.findFirst({
where: {
OR: [
{
published: {
is: {
slug: revision.slug
}
}
},
{
pending: {
is: {
slug: revision.slug
}
}
}
]
},
include: {
draft: true,
pending: true,
published: true
}
});
if (publishedPage && publishedPage.id !== id) {
throw new error_1.DuplicatePageSlugError(publishedPage.id,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(publishedPage.published || publishedPage.pending).slug);
}
if (publishAt > new Date()) {
return pageClient.update({
where: { id },
data: {
pending: {
upsert: {
create: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishAt, publishedAt: (_j = publishedAt !== null && publishedAt !== void 0 ? publishedAt : (_h = page === null || page === void 0 ? void 0 : page.published) === null || _h === void 0 ? void 0 : _h.publishedAt) !== null && _j !== void 0 ? _j : publishAt, updatedAt: updatedAt !== null && updatedAt !== void 0 ? updatedAt : publishAt, properties: {
createMany: {
data: properties
}
} }),
update: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishAt, publishedAt: (_l = publishedAt !== null && publishedAt !== void 0 ? publishedAt : (_k = page === null || page === void 0 ? void 0 : page.published) === null || _k === void 0 ? void 0 : _k.publishedAt) !== null && _l !== void 0 ? _l : publishAt, updatedAt: updatedAt !== null && updatedAt !== void 0 ? updatedAt : publishAt, properties: {
deleteMany: {
pageRevisionId: revisionId
},
createMany: {
data: properties
}
} })
}
},
draft: {
delete: true
}
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
}
return pageClient.update({
where: { id },
data: {
published: {
upsert: {
create: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishedAt: (_o = publishedAt !== null && publishedAt !== void 0 ? publishedAt : (_m = page.published) === null || _m === void 0 ? void 0 : _m.publishAt) !== null && _o !== void 0 ? _o : publishAt, updatedAt: updatedAt !== null && updatedAt !== void 0 ? updatedAt : publishAt, publishAt: null, properties: {
createMany: {
data: properties
}
} }),
update: Object.assign(Object.assign({}, revision), { blocks: revision.blocks || client_1.Prisma.JsonNull, publishedAt: (_q = publishedAt !== null && publishedAt !== void 0 ? publishedAt : (_p = page.published) === null || _p === void 0 ? void 0 : _p.publishAt) !== null && _q !== void 0 ? _q : publishAt, updatedAt: updatedAt !== null && updatedAt !== void 0 ? updatedAt : publishAt, publishAt: null, properties: {
createMany: {
data: properties
}
} })
}
},
pending: {
delete: Boolean(page.pendingId)
},
draft: {
delete: true
}
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
});
exports.publishPage = publishPage;
const updatePage = (id, _s, authenticate, pageClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _t, _u, _v, _w, _x, _y;
var { properties } = _s, input = tslib_1.__rest(_s, ["properties"]);
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanCreatePage, roles);
const page = yield pageClient.findUnique({
where: { id },
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
if (!page) {
throw new error_1.NotFound('page', id);
}
return pageClient.update({
where: { id },
data: {
draft: {
upsert: {
update: Object.assign(Object.assign({}, input), { revision: page.pending
? page.pending.revision + 1
: page.published
? page.published.revision + 1
: 0, updatedAt: null, createdAt: (_u = (_t = page.draft) === null || _t === void 0 ? void 0 : _t.createdAt) !== null && _u !== void 0 ? _u : new Date(), properties: {
deleteMany: {
pageRevisionId: (_w = (_v = page.draft) === null || _v === void 0 ? void 0 : _v.id) !== null && _w !== void 0 ? _w : ''
},
createMany: {
data: properties.map(property => ({
key: property.key,
value: property.value,
public: property.public
}))
}
} }),
create: Object.assign(Object.assign({}, input), { revision: page.pending
? page.pending.revision + 1
: page.published
? page.published.revision + 1
: 0, updatedAt: null, createdAt: (_y = (_x = page.draft) === null || _x === void 0 ? void 0 : _x.createdAt) !== null && _y !== void 0 ? _y : new Date(), properties: {
createMany: {
data: properties.map(property => ({
key: property.key,
value: property.value,
public: property.public
}))
}
} })
}
}
},
include: {
draft: {
include: {
properties: true
}
},
pending: {
include: {
properties: true
}
},
published: {
include: {
properties: true
}
}
}
});
});
exports.updatePage = updatePage;
//# sourceMappingURL=page.private-mutation.js.map