-
Notifications
You must be signed in to change notification settings - Fork 6
PM-1504 create edit scorecard #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,18 @@ import { | |
} from '@nestjs/common'; | ||
import { Prisma } from '@prisma/client'; | ||
import { | ||
mapScorecardRequestForCreate, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mapScorecardRequestToDto, | ||
// ScorecardGroupBaseDto, | ||
ScorecardGroupBaseDto, | ||
ScorecardPaginatedResponseDto, | ||
ScorecardQueryDto, | ||
// ScorecardQuestionBaseDto, | ||
ScorecardQuestionBaseDto, | ||
ScorecardRequestDto, | ||
ScorecardResponseDto, | ||
// ScorecardSectionBaseDto, | ||
ScorecardSectionBaseDto, | ||
ScorecardWithGroupResponseDto, | ||
} from 'src/dto/scorecard.dto'; | ||
import { JwtUser } from 'src/shared/modules/global/jwt.service'; | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { PrismaService } from 'src/shared/modules/global/prisma.service'; | ||
|
||
@Injectable() | ||
|
@@ -28,9 +30,16 @@ export class ScoreCardService { | |
*/ | ||
async addScorecard( | ||
body: ScorecardRequestDto, | ||
user: JwtUser, | ||
): Promise<ScorecardWithGroupResponseDto> { | ||
const data = await this.prisma.scorecard.create({ | ||
data: mapScorecardRequestToDto(body), | ||
data: { | ||
...(mapScorecardRequestForCreate({ | ||
...body, | ||
createdBy: user.isMachine ? 'System' : (user.userId as string), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a type guard or type assertion to ensure |
||
updatedBy: user.isMachine ? 'System' : (user.userId as string), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous comment, ensure |
||
}) as any), | ||
}, | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
|
@@ -44,7 +53,7 @@ export class ScoreCardService { | |
}, | ||
}); | ||
|
||
return data as ScorecardWithGroupResponseDto; | ||
return data as unknown as ScorecardWithGroupResponseDto; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting |
||
} | ||
|
||
/** | ||
|
@@ -54,12 +63,17 @@ export class ScoreCardService { | |
*/ | ||
async editScorecard( | ||
id: string, | ||
body: ScorecardWithGroupResponseDto, | ||
body: ScorecardRequestDto, | ||
user: JwtUser, | ||
): Promise<ScorecardWithGroupResponseDto> { | ||
const data = await this.prisma.scorecard | ||
.update({ | ||
where: { id }, | ||
data: mapScorecardRequestToDto(body), | ||
data: mapScorecardRequestToDto({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type assertion |
||
...body, | ||
createdBy: user.isMachine ? 'System' : (user.userId as string), | ||
updatedBy: user.isMachine ? 'System' : (user.userId as string), | ||
}) as any, | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
|
@@ -81,7 +95,7 @@ export class ScoreCardService { | |
}); | ||
}); | ||
|
||
return data as ScorecardWithGroupResponseDto; | ||
return data as unknown as ScorecardWithGroupResponseDto; | ||
kkartunov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
@@ -202,79 +216,87 @@ export class ScoreCardService { | |
}; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await | ||
async cloneScorecard(id: string): Promise<ScorecardResponseDto> { | ||
// const original = await this.prisma.scorecard.findUnique({ | ||
// where: { id }, | ||
// include: { | ||
// scorecardGroups: { | ||
// include: { | ||
// sections: { | ||
// include: { | ||
// questions: true, | ||
// }, | ||
// }, | ||
// }, | ||
// }, | ||
// }, | ||
// }); | ||
async cloneScorecard( | ||
id: string, | ||
user: { userId?: string; isMachine: boolean }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
): Promise<ScorecardResponseDto> { | ||
const original = await this.prisma.scorecard.findUnique({ | ||
where: { id }, | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
sections: { | ||
include: { | ||
questions: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// if (!original) { | ||
// throw new NotFoundException({ message: `Scorecard not found.` }); | ||
// } | ||
if (!original) { | ||
throw new NotFoundException({ message: `Scorecard not found.` }); | ||
} | ||
|
||
// // Remove id fields from nested objects for cloning | ||
// const cloneGroups = original.scorecardGroups.map( | ||
// (group: ScorecardGroupBaseDto) => ({ | ||
// ...group, | ||
// id: undefined, | ||
// createdAt: undefined, | ||
// updatedAt: undefined, | ||
// scorecardId: undefined, | ||
// sections: group.sections.map((section: ScorecardSectionBaseDto) => ({ | ||
// ...section, | ||
// id: undefined, | ||
// createdAt: undefined, | ||
// updatedAt: undefined, | ||
// scorecardGroupId: undefined, | ||
// questions: section.questions.map( | ||
// (question: ScorecardQuestionBaseDto) => ({ | ||
// ...question, | ||
// id: undefined, | ||
// createdAt: undefined, | ||
// updatedAt: undefined, | ||
// sectionId: undefined, | ||
// scorecardSectionId: undefined, | ||
// }), | ||
// ), | ||
// })), | ||
// }), | ||
// ); | ||
const auditFields = { | ||
createdBy: user.isMachine ? 'System' : (user.userId as string), | ||
updatedBy: user.isMachine ? 'System' : (user.userId as string), | ||
createdAt: undefined, | ||
updatedAt: undefined, | ||
}; | ||
|
||
// const clonedScorecard = await this.prisma.scorecard.create({ | ||
// data: { | ||
// ...original, | ||
// id: undefined, | ||
// name: `${original.name} (Clone)`, | ||
// createdAt: undefined, | ||
// updatedAt: undefined, | ||
// scorecardGroups: { | ||
// create: cloneGroups, | ||
// }, | ||
// }, | ||
// include: { | ||
// scorecardGroups: { | ||
// include: { | ||
// sections: { | ||
// include: { | ||
// questions: true, | ||
// }, | ||
// }, | ||
// }, | ||
// }, | ||
// }, | ||
// }); | ||
const clonedScorecard = {}; | ||
// Remove id fields from nested objects for cloning | ||
const cloneGroups = original.scorecardGroups.map( | ||
(group: ScorecardGroupBaseDto) => ({ | ||
...group, | ||
id: undefined, | ||
...auditFields, | ||
scorecardId: undefined, | ||
sections: { | ||
create: group.sections.map((section: ScorecardSectionBaseDto) => ({ | ||
...section, | ||
id: undefined, | ||
...auditFields, | ||
scorecardGroupId: undefined, | ||
questions: { | ||
create: section.questions.map( | ||
(question: ScorecardQuestionBaseDto) => ({ | ||
...question, | ||
id: undefined, | ||
...auditFields, | ||
sectionId: undefined, | ||
scorecardSectionId: undefined, | ||
}), | ||
), | ||
}, | ||
})), | ||
}, | ||
}), | ||
) as any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
const clonedScorecard = await this.prisma.scorecard.create({ | ||
data: { | ||
...original, | ||
id: undefined, | ||
name: `${original.name} (Clone)`, | ||
...auditFields, | ||
scorecardGroups: { | ||
create: cloneGroups, | ||
}, | ||
}, | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
sections: { | ||
include: { | ||
questions: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return clonedScorecard as ScorecardResponseDto; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.