@wepublish/api
Version:
API core for we.publish.
72 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rateComment = exports.validateCommentRatingValue = void 0;
const tslib_1 = require("tslib");
const client_1 = require("@prisma/client");
const error_1 = require("../../error");
const api_1 = require("../../../../settings-api/src");
const comment_public_queries_1 = require("../comment/comment.public-queries");
const validateCommentRatingValue = (type, value) => {
switch (type) {
case client_1.RatingSystemType.star: {
if (value <= 0 || value > 5) {
throw new error_1.InvalidStarRatingValueError();
}
}
}
};
exports.validateCommentRatingValue = validateCommentRatingValue;
const rateComment = (commentId, answerId, value, fingerprint, optionalAuthenticateUser, commentRatingSystemAnswer, commentRating, commentClient, settingsClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a;
const session = optionalAuthenticateUser();
// check if anonymous rating is allowed
const guestRatingSetting = yield settingsClient.findUnique({
where: {
name: api_1.SettingName.ALLOW_GUEST_COMMENT_RATING
}
});
if (!session && (guestRatingSetting === null || guestRatingSetting === void 0 ? void 0 : guestRatingSetting.value) !== true) {
throw new error_1.AnonymousCommentRatingDisabledError();
}
const answer = yield commentRatingSystemAnswer.findUnique({
where: {
id: answerId
}
});
if (!answer) {
throw new error_1.NotFound('CommentRatingSystemAnswer', answerId);
}
(0, exports.validateCommentRatingValue)(answer.type, value);
yield commentRating.upsert({
where: {
answerId_commentId_userId: {
answerId,
commentId,
userId: (_a = session === null || session === void 0 ? void 0 : session.user.id) !== null && _a !== void 0 ? _a : ''
}
},
update: {
fingerprint,
value
},
create: {
answerId,
commentId,
userId: session === null || session === void 0 ? void 0 : session.user.id,
value,
fingerprint
}
});
const comment = yield commentClient.findFirst({
where: {
id: commentId
},
include: {
revisions: { orderBy: { createdAt: 'asc' } },
overriddenRatings: true
}
});
return (0, comment_public_queries_1.mapCommentToPublicComment)(comment);
});
exports.rateComment = rateComment;
//# sourceMappingURL=comment-rating.public-mutation.js.map