152 lines
4.6 KiB
TypeScript
152 lines
4.6 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { AccessStore } from '../identity/access.store';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import type { PageInput } from '../identity/identity.schemas';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import { commandHash } from '../common/idempotency';
|
|
import { recordAudit } from '../identity/audit';
|
|
import {
|
|
ownReviewView,
|
|
reviewInclude,
|
|
publicReviewSelect,
|
|
publicReviewWhere,
|
|
} from './review-view';
|
|
import type { ReviewSubmissionInput } from './review.schemas';
|
|
|
|
@Injectable()
|
|
export class ReviewStore {
|
|
constructor(
|
|
private readonly db: DatabaseService,
|
|
private readonly access: AccessStore,
|
|
) {}
|
|
|
|
async own(actor: Principal, page: PageInput) {
|
|
const reviews = await this.db.productReview.findMany({
|
|
where: { organizationId: actor.organizationId, userId: actor.userId },
|
|
include: reviewInclude,
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: page.limit,
|
|
skip: page.offset,
|
|
});
|
|
return reviews.map(ownReviewView);
|
|
}
|
|
|
|
eligibility(actor: Principal, page: PageInput) {
|
|
return this.db.reviewPurchaseVerification.findMany({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
userId: actor.userId,
|
|
revocation: { is: null },
|
|
variant: {
|
|
active: true,
|
|
product: { status: 'PUBLISHED' },
|
|
reviews: {
|
|
none: {
|
|
userId: actor.userId,
|
|
organizationId: actor.organizationId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
select: { id: true, variantId: true, createdAt: true },
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: page.limit,
|
|
skip: page.offset,
|
|
});
|
|
}
|
|
|
|
async publicList(organizationId: string, productId: string, page: PageInput) {
|
|
if (
|
|
!(await this.db.product.findFirst({
|
|
where: { id: productId, organizationId, status: 'PUBLISHED' },
|
|
select: { id: true },
|
|
}))
|
|
)
|
|
throw new AppError('PRODUCT_NOT_FOUND');
|
|
const reviews = await this.db.productReview.findMany({
|
|
where: publicReviewWhere(organizationId, productId),
|
|
select: publicReviewSelect,
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: page.limit,
|
|
skip: page.offset,
|
|
});
|
|
return reviews.map((review) => ({
|
|
...review,
|
|
verifiedPurchase: true,
|
|
verificationMethod: 'MANUAL' as const,
|
|
}));
|
|
}
|
|
|
|
submit(actor: Principal, input: ReviewSubmissionInput) {
|
|
const hash = commandHash(
|
|
input.verificationId,
|
|
input.rating,
|
|
input.displayName,
|
|
input.title,
|
|
input.body,
|
|
);
|
|
return this.access.mutate(actor, null, async (tx) => {
|
|
const replay = await tx.productReview.findUnique({
|
|
where: {
|
|
userId_organizationId_idempotencyKey: {
|
|
userId: actor.userId,
|
|
organizationId: actor.organizationId,
|
|
idempotencyKey: input.idempotencyKey,
|
|
},
|
|
},
|
|
include: reviewInclude,
|
|
});
|
|
if (replay) {
|
|
if (replay.requestHash !== hash)
|
|
throw new AppError('REVIEW_REPLAY_CONFLICT');
|
|
return ownReviewView(replay);
|
|
}
|
|
const verification = await tx.reviewPurchaseVerification.findFirst({
|
|
where: {
|
|
id: input.verificationId,
|
|
userId: actor.userId,
|
|
organizationId: actor.organizationId,
|
|
},
|
|
include: { revocation: true, variant: { include: { product: true } } },
|
|
});
|
|
if (!verification) throw new AppError('REVIEW_VERIFICATION_NOT_FOUND');
|
|
if (verification.revocation)
|
|
throw new AppError('REVIEW_VERIFICATION_REVOKED');
|
|
if (
|
|
!verification.variant.active ||
|
|
verification.variant.product.status !== 'PUBLISHED'
|
|
)
|
|
throw new AppError('REVIEW_VARIANT_UNAVAILABLE');
|
|
if (
|
|
await tx.productReview.findFirst({
|
|
where: {
|
|
userId: actor.userId,
|
|
organizationId: actor.organizationId,
|
|
variantId: verification.variantId,
|
|
},
|
|
})
|
|
)
|
|
throw new AppError('REVIEW_ALREADY_EXISTS');
|
|
const review = await tx.productReview.create({
|
|
data: {
|
|
...input,
|
|
organizationId: actor.organizationId,
|
|
userId: actor.userId,
|
|
variantId: verification.variantId,
|
|
requestHash: hash,
|
|
},
|
|
include: reviewInclude,
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'review.submitted',
|
|
review.id,
|
|
);
|
|
return ownReviewView(review);
|
|
});
|
|
}
|
|
}
|