Skip to content
Merged
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
43 changes: 33 additions & 10 deletions src/api/scorecard/scorecard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,45 @@ export class ScoreCardService {
*/
async viewScorecard(id: string): Promise<ScorecardWithGroupResponseDto> {
try {
const data = await this.prisma.scorecard.findUniqueOrThrow({
where: { id },
include: {
scorecardGroups: {
include: {
sections: {
include: {
questions: true,
},
const include = {
scorecardGroups: {
include: {
sections: {
include: {
questions: true,
},
},
},
},
};

const scorecardById = await this.prisma.scorecard.findUnique({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ maintainability]
Consider using findUniqueOrThrow instead of findUnique for scorecardById to automatically throw an error if the scorecard is not found. This would simplify the logic by removing the need for the subsequent if (scorecardById) check.

where: { id },
include,
});

if (scorecardById) {
return scorecardById as ScorecardWithGroupResponseDto;
}

const scorecardByLegacyId = await this.prisma.scorecard.findFirst({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[❗❗ correctness]
The use of findFirst for scorecardByLegacyId could potentially return an incorrect scorecard if multiple scorecards have the same legacyId. Ensure that legacyId is unique or adjust the logic to handle multiple matches appropriately.

where: { legacyId: id },
include,
});
return data as ScorecardWithGroupResponseDto;

if (!scorecardByLegacyId) {
throw new NotFoundException({
message: `Scorecard with ID ${id} not found. Please check the ID and try again.`,
details: { scorecardId: id },
});
}

return scorecardByLegacyId as ScorecardWithGroupResponseDto;
} catch (error) {
if (error instanceof NotFoundException) {
throw error;
}

const errorResponse = this.prismaErrorService.handleError(
error,
`viewing scorecard with ID: ${id}`,
Expand Down