Files
setrip/server/services/trust.service.ts
T

40 lines
1.1 KiB
TypeScript

import { prisma } from "@/lib/prisma";
import { TRIP_LEADER_MIN_TRIPS } from "@/lib/trust";
export type OrganizerTrust = {
isVerified: boolean;
tripsCreated: number;
avgRating: number | null;
reviewCount: number;
isTripLeader: boolean;
};
export const trustService = {
async getOrganizerTrust(organizerId: string): Promise<OrganizerTrust> {
const [tripsCreated, reviewAgg, organizerVerification] = await Promise.all([
prisma.trip.count({ where: { organizerId } }),
prisma.tripReview.aggregate({
where: {
trip: { organizerId },
},
_avg: { rating: true },
_count: { _all: true },
}),
prisma.organizerVerification.findUnique({
where: { userId: organizerId },
select: { status: true },
}),
]);
const avg = reviewAgg._avg.rating;
return {
isVerified: organizerVerification?.status === "APPROVED",
tripsCreated,
avgRating:
avg != null ? Math.round(Number(avg) * 10) / 10 : null,
reviewCount: reviewAgg._count._all,
isTripLeader: tripsCreated >= TRIP_LEADER_MIN_TRIPS,
};
},
};