UNPKG

@wepublish/api

Version:
86 lines 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPublicCommentsForItemById = exports.getCalculatedRatingsForComment = exports.getPublicChildrenCommentsByParentId = exports.mapCommentToPublicComment = void 0; const tslib_1 = require("tslib"); const client_1 = require("@prisma/client"); const ramda_1 = require("ramda"); const comment_1 = require("../../db/comment"); const api_1 = require("../../../../utils-api/src"); const mapCommentToPublicComment = (comment) => { const { revisions } = comment; return Object.assign({ title: revisions.length ? revisions[revisions.length - 1].title : null, lead: revisions.length ? revisions[revisions.length - 1].lead : null, text: revisions.length ? revisions[revisions.length - 1].text : null, revisions }, comment); }; exports.mapCommentToPublicComment = mapCommentToPublicComment; const getPublicChildrenCommentsByParentId = (parentId, userId, comment) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const comments = yield comment.findMany({ where: { AND: [ { parentID: parentId }, { OR: [userId ? { userID: userId } : {}, { state: client_1.CommentState.approved }] } ] }, orderBy: { modifiedAt: 'desc' }, include: { revisions: { orderBy: { createdAt: 'asc' } }, overriddenRatings: true } }); return comments.map(exports.mapCommentToPublicComment); }); exports.getPublicChildrenCommentsByParentId = getPublicChildrenCommentsByParentId; const getCalculatedRatingsForComment = (answers, ratings) => answers.map(answer => { const sortedRatings = ratings .filter(rating => rating.answerId === answer.id) .map(rating => rating.value) .sort((a, b) => a - b); const total = sortedRatings.reduce((value, rating) => value + rating, 0); const mean = total / Math.max(sortedRatings.length, 1); return { answer, count: sortedRatings.length, mean, total }; }); exports.getCalculatedRatingsForComment = getCalculatedRatingsForComment; const sortCommentsByRating = (orderFn) => (0, ramda_1.sortWith)([ orderFn(({ calculatedRatings }) => calculatedRatings.reduce((ratingsTotal, calculatedRating) => ratingsTotal + calculatedRating.mean, 0)), (0, ramda_1.ascend)(({ createdAt }) => createdAt) ]); const getPublicCommentsForItemById = (itemId, userId, sort, order, commentRatingSystemAnswers, comment) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const [answers, comments] = yield Promise.all([ commentRatingSystemAnswers.load(1), comment.findMany({ where: { OR: [ { itemID: itemId, state: client_1.CommentState.approved, parentID: null }, userId ? { itemID: itemId, userID: userId, parentID: null } : {} ] }, include: { revisions: { orderBy: { createdAt: 'asc' } }, ratings: true, overriddenRatings: true }, orderBy: { createdAt: 'asc' } }) ]); const commentsWithRating = comments.map((_a) => { var { ratings } = _a, comment = tslib_1.__rest(_a, ["ratings"]); return (Object.assign(Object.assign({}, (0, exports.mapCommentToPublicComment)(comment)), { calculatedRatings: (0, exports.getCalculatedRatingsForComment)(answers, ratings) })); }); if (sort === comment_1.PublicCommentSort.Rating) { if (order === api_1.SortOrder.Ascending) { return sortCommentsByRating(ramda_1.ascend)(commentsWithRating); } return sortCommentsByRating(ramda_1.descend)(commentsWithRating); } // no sorting needed as comments already come sorted by creation return commentsWithRating; }); exports.getPublicCommentsForItemById = getPublicCommentsForItemById; //# sourceMappingURL=comment.public-queries.js.map