Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/api/review/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,43 @@ export class ReviewService {
): Promise<ReviewItemResponseDto> {
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,
},
Expand Down
16 changes: 14 additions & 2 deletions src/dto/review.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MappedReviewItem> = {
...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;
}

Expand Down