archetecture security fix
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.findMany = findMany;
|
||||
exports.findById = findById;
|
||||
exports.updateById = updateById;
|
||||
exports.getStats = getStats;
|
||||
exports.sendReminder = sendReminder;
|
||||
const prisma_1 = require("../../lib/prisma");
|
||||
const REVIEW_INCLUDE = {
|
||||
reservation: {
|
||||
select: {
|
||||
id: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
reviewToken: true,
|
||||
reviewRequestSentAt: true,
|
||||
reviewReminderSentAt: true,
|
||||
reviewFinalReminderSentAt: true,
|
||||
reviewPaused: true,
|
||||
customer: {
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
email: true,
|
||||
reviewOptOut: true,
|
||||
},
|
||||
},
|
||||
vehicle: {
|
||||
select: {
|
||||
id: true,
|
||||
make: true,
|
||||
model: true,
|
||||
year: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
async function findMany(companyId, where, skip, take) {
|
||||
const baseWhere = { companyId, ...where };
|
||||
return Promise.all([
|
||||
prisma_1.prisma.review.findMany({
|
||||
where: baseWhere,
|
||||
include: REVIEW_INCLUDE,
|
||||
skip,
|
||||
take,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma_1.prisma.review.count({ where: baseWhere }),
|
||||
]);
|
||||
}
|
||||
async function findById(id, companyId) {
|
||||
return prisma_1.prisma.review.findFirst({
|
||||
where: { id, companyId },
|
||||
include: REVIEW_INCLUDE,
|
||||
});
|
||||
}
|
||||
async function updateById(id, data) {
|
||||
return prisma_1.prisma.review.update({
|
||||
where: { id },
|
||||
data,
|
||||
include: REVIEW_INCLUDE,
|
||||
});
|
||||
}
|
||||
async function getStats(companyId) {
|
||||
const reviews = await prisma_1.prisma.review.findMany({
|
||||
where: { companyId },
|
||||
select: { overallRating: true, vehicleRating: true, serviceRating: true },
|
||||
});
|
||||
const total = reviews.length;
|
||||
const byRating = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
|
||||
let sumOverall = 0;
|
||||
let sumVehicle = 0;
|
||||
let countVehicle = 0;
|
||||
let sumService = 0;
|
||||
let countService = 0;
|
||||
for (const r of reviews) {
|
||||
sumOverall += r.overallRating;
|
||||
byRating[r.overallRating] = (byRating[r.overallRating] ?? 0) + 1;
|
||||
if (r.vehicleRating != null) {
|
||||
sumVehicle += r.vehicleRating;
|
||||
countVehicle++;
|
||||
}
|
||||
if (r.serviceRating != null) {
|
||||
sumService += r.serviceRating;
|
||||
countService++;
|
||||
}
|
||||
}
|
||||
return {
|
||||
total,
|
||||
averageOverall: total > 0 ? Math.round((sumOverall / total) * 10) / 10 : 0,
|
||||
averageVehicle: countVehicle > 0 ? Math.round((sumVehicle / countVehicle) * 10) / 10 : 0,
|
||||
averageService: countService > 0 ? Math.round((sumService / countService) * 10) / 10 : 0,
|
||||
byRating,
|
||||
};
|
||||
}
|
||||
async function sendReminder(reservationId, sentAt, field) {
|
||||
return prisma_1.prisma.reservation.update({
|
||||
where: { id: reservationId },
|
||||
data: { [field]: sentAt },
|
||||
});
|
||||
}
|
||||
//# sourceMappingURL=review.repo.js.map
|
||||
Reference in New Issue
Block a user