add payment, trust badge, handle race condition, fix booking schema
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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 [user, tripsCreated, reviewAgg] = await Promise.all([
|
||||
prisma.user.findUnique({
|
||||
where: { id: organizerId },
|
||||
select: { isVerified: true },
|
||||
}),
|
||||
prisma.trip.count({ where: { organizerId } }),
|
||||
prisma.tripReview.aggregate({
|
||||
where: {
|
||||
trip: { organizerId },
|
||||
},
|
||||
_avg: { rating: true },
|
||||
_count: { _all: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const avg = reviewAgg._avg.rating;
|
||||
return {
|
||||
isVerified: user?.isVerified ?? false,
|
||||
tripsCreated,
|
||||
avgRating:
|
||||
avg != null ? Math.round(Number(avg) * 10) / 10 : null,
|
||||
reviewCount: reviewAgg._count._all,
|
||||
isTripLeader: tripsCreated >= TRIP_LEADER_MIN_TRIPS,
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user