55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { AppError } from '../common/errors/app-error';
|
|
import type { DatabaseService } from '../database/database.service';
|
|
import type { Prisma } from '../generated/prisma/client';
|
|
|
|
export const reviewInclude = {
|
|
verification: {
|
|
select: { revocation: { select: { verificationId: true } } },
|
|
},
|
|
events: { orderBy: { sequence: 'asc' as const } },
|
|
};
|
|
type Review = Prisma.ProductReviewGetPayload<{ include: typeof reviewInclude }>;
|
|
export function ownReviewView(review: Review) {
|
|
return {
|
|
id: review.id,
|
|
variantId: review.variantId,
|
|
rating: review.rating,
|
|
displayName: review.displayName,
|
|
title: review.title,
|
|
body: review.body,
|
|
createdAt: review.createdAt,
|
|
moderationStatus: review.events.at(-1)?.kind ?? 'PENDING',
|
|
purchaseVerified: !review.verification.revocation,
|
|
};
|
|
}
|
|
export const publicReviewSelect = {
|
|
id: true,
|
|
variantId: true,
|
|
rating: true,
|
|
displayName: true,
|
|
title: true,
|
|
body: true,
|
|
createdAt: true,
|
|
} as const;
|
|
export const publicReviewWhere = (
|
|
organizationId: string,
|
|
productId: string,
|
|
): Prisma.ProductReviewWhereInput => ({
|
|
organizationId,
|
|
variant: { active: true, productId, product: { status: 'PUBLISHED' } },
|
|
verification: { revocation: { is: null } },
|
|
events: { some: { kind: 'APPROVED' }, none: { kind: 'HIDDEN' } },
|
|
});
|
|
export async function requireReview(
|
|
db: DatabaseService | Prisma.TransactionClient,
|
|
organizationId: string,
|
|
id: string,
|
|
) {
|
|
const review = await db.productReview.findFirst({
|
|
where: { id, organizationId },
|
|
include: reviewInclude,
|
|
});
|
|
if (!review) throw new AppError('REVIEW_NOT_FOUND');
|
|
return review;
|
|
}
|