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
181 changes: 158 additions & 23 deletions src/api/scorecard/scorecard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import {
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import {
Prisma,
scorecardGroup,
scorecardQuestion,
scorecardSection,
} from '@prisma/client';
import {
mapScorecardRequestForCreate,
mapScorecardRequestToDto,
ScorecardGroupBaseDto,
ScorecardPaginatedResponseDto,
ScorecardQueryDto,
Expand All @@ -18,10 +22,36 @@ import {
} from 'src/dto/scorecard.dto';
import { JwtUser } from 'src/shared/modules/global/jwt.service';
import { PrismaService } from 'src/shared/modules/global/prisma.service';
import { omit } from 'lodash';
import { LoggerService } from 'src/shared/modules/global/logger.service';

type ChildEntity = scorecardGroup | scorecardSection | scorecardQuestion;

interface SyncChildrenParams<T extends ChildEntity> {
parentId: string;
parentField: keyof T; // "scorecardId" | "groupId" | "sectionId"
model: {
create: (args: { data: any }) => Promise<T>;
update: (args: { where: { id: string }; data: any }) => Promise<T>;
deleteMany: (args: { where: { id: { in: string[] } } }) => Promise<any>;
findMany: (args: { where: any }) => Promise<T[]>;
};
incoming: (T & {
sections?: scorecardSection[];
questions?: scorecardQuestion[];
})[];
existing: T[];
cascade?: (child: any) => Promise<void>;
userId: string;
}

@Injectable()
export class ScoreCardService {
constructor(private readonly prisma: PrismaService) {}
private readonly logger: LoggerService;

constructor(private readonly prisma: PrismaService) {
this.logger = LoggerService.forRoot('ScorecardService');
}

/**
* Adds score card
Expand Down Expand Up @@ -62,26 +92,80 @@ export class ScoreCardService {
* @returns ScorecardWithGroupResponseDto
*/
async editScorecard(
id: string,
body: ScorecardRequestDto,
scorecardId: string,
scorecardInput: ScorecardRequestDto,
user: JwtUser,
): Promise<ScorecardWithGroupResponseDto> {
const original = await this.prisma.scorecard.findUnique({
where: { id },
where: { id: scorecardId },
});

if (!original) {
throw new NotFoundException({ message: `Scorecard not found.` });
}

const data = await this.prisma.scorecard
.update({
where: { id },
data: mapScorecardRequestToDto({
...body,
createdBy: user.isMachine ? 'System' : (user.userId as string),
updatedBy: user.isMachine ? 'System' : (user.userId as string),
}) as any,
const userId = user.isMachine ? 'System' : (user.userId as string);

this.logger.log(
`[updateScorecard] Updating scorecard with id: ${scorecardId}`,
);

return this.prisma.$transaction(async (tx) => {
// Update scorecard basic info
const updatedScorecard = await tx.scorecard.update({
where: { id: scorecardId },
data: {
...omit(scorecardInput, 'scorecardGroups'),
},
});
this.logger.log(
`[updateScorecard] Updated scorecard basic info: ${JSON.stringify(updatedScorecard)}`,
);

// Sync groups
await this.syncChildren<scorecardGroup>({
parentId: scorecardId,
parentField: 'scorecardId',
model: tx.scorecardGroup,
incoming: scorecardInput.scorecardGroups as any,
existing: await tx.scorecardGroup.findMany({
where: { scorecardId: scorecardId },
}),
userId,
cascade: async (groupInput) => {
// Sync sections
await this.syncChildren<scorecardSection>({
parentId: groupInput.id,
parentField: 'scorecardGroupId',
model: tx.scorecardSection,
incoming: groupInput.sections ?? [],
existing: await tx.scorecardSection.findMany({
where: { scorecardGroupId: groupInput.id },
}),
userId,
cascade: async (sectionInput) => {
// Sync questions
await this.syncChildren<scorecardQuestion>({
parentId: sectionInput.id,
parentField: 'scorecardSectionId',
model: tx.scorecardQuestion,
incoming: sectionInput.questions ?? [],
existing: await tx.scorecardQuestion.findMany({
where: { scorecardSectionId: sectionInput.id },
}),
userId,
});
},
});
},
});

this.logger.log(
`[updateScorecard] Finished syncing groups, sections, and questions for scorecard ${scorecardId}`,
);

return this.prisma.scorecard.findUnique({
where: { id: scorecardId },
include: {
scorecardGroups: {
include: {
Expand All @@ -93,17 +177,68 @@ export class ScoreCardService {
},
},
},
})
.catch((error) => {
if (error.code !== 'P2025') {
throw new NotFoundException({ message: `Scorecard not found.` });
}
throw new InternalServerErrorException({
message: `Error: ${error.code}`,
}) as Promise<ScorecardWithGroupResponseDto>;
});
}

async syncChildren<T extends ChildEntity>({
parentId,
parentField,
model,
incoming,
existing,
cascade,
userId,
}: SyncChildrenParams<T>): Promise<void> {
const incomingIds = incoming.filter((c) => c.id).map((c) => c.id);
const existingIds = existing.map((e) => e.id);

// Delete removed children
const toDelete = existingIds.filter((id) => !incomingIds.includes(id));
if (toDelete.length > 0) {
this.logger.log(
`[syncChildren] Deleting children with ids: ${toDelete.join(',')}`,
);
await model.deleteMany({ where: { id: { in: toDelete } } });
}

// Upsert incoming
for (const child of incoming) {
if (child.id) {
this.logger.log(`[syncChildren] Updating child with id: ${child.id}`);
await model.update({
where: { id: child.id },
data: {
...omit(child, ['sections', 'questions']),
updatedBy: userId,
},
});
});
} else {
this.logger.log(
`[syncChildren] Creating new child for parentId: ${parentId}`,
);
const created = await model.create({
data: {
...omit(child, ['sections', 'questions']),
[parentField]: parentId,
createdBy: userId,
updatedBy: userId,
},
});
child.id = created.id; // assign id back to input
this.logger.log(
`[syncChildren] Created child with new id: ${created.id}`,
);
}

return data as unknown as ScorecardWithGroupResponseDto;
// Cascade to nested children
if (cascade) {
await cascade(child);
}
}
this.logger.log(
`[syncChildren] Finished syncing children for parentId: ${parentId}`,
);
}

/**
Expand Down
78 changes: 5 additions & 73 deletions src/dto/scorecard.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class ScorecardQuestionBaseDto {
example: 'Provide detailed information.',
})
@IsString()
@IsNotEmpty()
guidelines: string;

@ApiProperty({ description: 'The weight of the question', example: 10 })
Expand Down Expand Up @@ -105,6 +106,10 @@ export class ScorecardQuestionBaseDto {
@IsOptional()
@IsNumber()
scaleMax?: number;

@ApiProperty({ description: 'Sort order of the question', example: 1 })
@IsNumber()
sortOrder: number;
}

export class ScorecardQuestionRequestDto extends ScorecardQuestionBaseDto {}
Expand Down Expand Up @@ -374,76 +379,3 @@ export function mapScorecardRequestForCreate(request: ScorecardRequestDto) {
},
};
}

export function mapScorecardRequestToDto(request: ScorecardRequestDto) {
const userFields = {
...(request.createdBy ? { createdBy: request.createdBy } : {}),
updatedBy: request.updatedBy,
};

return {
...request,
...userFields,
scorecardGroups: {
upsert: request.scorecardGroups.map((group) => ({
where: { id: (group as any).id as string },
update: {
...group,
updatedBy: request.updatedBy,
sections: {
upsert: group.sections.map((section) => ({
where: { id: (section as any).id as string },
update: {
...section,
updatedBy: request.updatedBy,
questions: {
upsert: section.questions.map((question) => ({
where: { id: (question as any).id as string },
update: {
...question,
sortOrder: 1,
updatedBy: request.updatedBy,
},
create: {
...question,
sortOrder: 1,
...userFields,
},
})),
},
},
create: {
...section,
...userFields,
questions: {
create: section.questions.map((question) => ({
...question,
sortOrder: 1,
...userFields,
})),
},
},
})),
},
},
create: {
...group,
...userFields,
sections: {
create: group.sections.map((section) => ({
...section,
...userFields,
questions: {
create: section.questions.map((question) => ({
...question,
sortOrder: 1,
...userFields,
})),
},
})),
},
},
})),
},
};
}