UNPKG

@wepublish/api

Version:
150 lines 5.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteComment = exports.createAdminComment = exports.updateComment = exports.takeActionOnComment = void 0; const tslib_1 = require("tslib"); const client_1 = require("@prisma/client"); const error_1 = require("../../error"); const comment_rating_public_mutation_1 = require("../comment-rating/comment-rating.public-mutation"); const permissions_1 = require("../permissions"); const api_1 = require("../../../../permissions-api/src"); const takeActionOnComment = (id, input, authenticate, comment) => { const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanTakeActionOnComment, roles); return comment.update({ where: { id }, data: input, include: { revisions: { orderBy: { createdAt: 'asc' } } } }); }; exports.takeActionOnComment = takeActionOnComment; const updateComment = (commentId, revision, userID, guestUsername, guestUserImageID, source, tagIds, ratingOverrides, authenticate, commentRatingAnswerClient, commentClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanUpdateComments, roles); const canSkipApproval = (0, api_1.hasPermission)(api_1.CanCreateApprovedComment, roles); if (ratingOverrides === null || ratingOverrides === void 0 ? void 0 : ratingOverrides.length) { const answerIds = ratingOverrides.map(override => override.answerId); const answers = yield commentRatingAnswerClient.findMany({ where: { id: { in: answerIds } } }); ratingOverrides.forEach(override => { var _a; const answer = answers.find(a => a.id === override.answerId); if (!answer) { throw new error_1.NotFound('CommentRatingSystemAnswer', override.answerId); } (0, comment_rating_public_mutation_1.validateCommentRatingValue)(answer.type, (_a = override.value) !== null && _a !== void 0 ? _a : 0); }); } return commentClient.update({ where: { id: commentId }, data: { userID, guestUsername, state: canSkipApproval ? client_1.CommentState.approved : client_1.CommentState.pendingApproval, guestUserImageID, source, revisions: revision ? { create: { text: revision.text, title: revision.title, lead: revision.lead } } : undefined, tags: tagIds ? { connectOrCreate: tagIds.map(tagId => ({ where: { commentId_tagId: { commentId, tagId } }, create: { tagId } })), deleteMany: { commentId, tagId: { notIn: tagIds } } } : undefined, overriddenRatings: { upsert: ratingOverrides === null || ratingOverrides === void 0 ? void 0 : ratingOverrides.map(override => { var _a; return ({ where: { answerId_commentId: { answerId: override.answerId, commentId } }, create: { answerId: override.answerId, value: override.value }, update: { value: (_a = override.value) !== null && _a !== void 0 ? _a : null } }); }) } }, include: { revisions: { orderBy: { createdAt: 'asc' } }, overriddenRatings: true } }); }); exports.updateComment = updateComment; const createAdminComment = (itemId, itemType, parentID, text, tagIds, authenticate, commentClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanUpdateComments, roles); const canSkipApproval = (0, api_1.hasPermission)(api_1.CanCreateApprovedComment, roles); return commentClient.create({ data: { state: canSkipApproval ? client_1.CommentState.approved : client_1.CommentState.pendingApproval, authorType: client_1.CommentAuthorType.team, itemID: itemId, itemType, parentID, revisions: text ? { create: { text } } : undefined, tags: { create: tagIds === null || tagIds === void 0 ? void 0 : tagIds.map(tagId => ({ tagId })) } }, include: { revisions: { orderBy: { createdAt: 'asc' } } } }); }); exports.createAdminComment = createAdminComment; const deleteComment = (id, authenticate, commentClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_1.CanDeleteComments, roles); return commentClient.delete({ where: { id } }); }); exports.deleteComment = deleteComment; //# sourceMappingURL=comment.private-mutation.js.map