diff --git a/src/api/review/review.service.ts b/src/api/review/review.service.ts index 1ae1726..c397528 100644 --- a/src/api/review/review.service.ts +++ b/src/api/review/review.service.ts @@ -652,9 +652,43 @@ export class ReviewService { ): Promise { this.logger.log(`Updating review item with ID: ${itemId}`); try { + // First get the existing review item to find the associated review's resourceId + const existingItem = await this.prisma.reviewItem.findUnique({ + where: { id: itemId }, + include: { + review: { + select: { + resourceId: true, + }, + }, + }, + }); + + if (!existingItem) { + throw new NotFoundException({ + message: `Review item with ID ${itemId} was not found. Please check the ID and try again.`, + code: 'RECORD_NOT_FOUND', + details: { itemId }, + }); + } + + // Get the mapped data for update + const mappedData = mapReviewItemRequestForUpdate(body); + + // If there are review item comments to update, set the resourceId + if (mappedData.reviewItemComments?.create) { + mappedData.reviewItemComments.create = + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + mappedData.reviewItemComments.create.map((comment: any) => ({ + ...comment, + // Use the review's resourceId as the commenter + resourceId: comment.resourceId || existingItem.review.resourceId, + })); + } + const data = await this.prisma.reviewItem.update({ where: { id: itemId }, - data: mapReviewItemRequestForUpdate(body), + data: mappedData, include: { reviewItemComments: true, }, diff --git a/src/dto/review.dto.ts b/src/dto/review.dto.ts index 55a9995..658f9bc 100644 --- a/src/dto/review.dto.ts +++ b/src/dto/review.dto.ts @@ -496,12 +496,24 @@ export function mapReviewItemRequestForUpdate( reviewItemComments?: any[]; } & ReviewItemRequestDto; - // For updates, we only include the core review item fields - // Comments should be handled separately via dedicated comment endpoints const payload: Partial = { ...rest, }; + // Handle review item comments update if provided + // Strategy: Delete all existing comments and create new ones + if (reviewItemComments !== undefined) { + payload.reviewItemComments = { + deleteMany: {}, // Delete all existing comments for this review item + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + create: reviewItemComments?.map((comment) => ({ + ...comment, + // resourceId is required on reviewItemComment; leave population to controller/service + resourceId: '', + })), + } as any; + } + return payload; }