-
Notifications
You must be signed in to change notification settings - Fork 6
fic(PM-1585): Search scorecard modifications #14
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 3 commits
d598e22
7db8787
278a90c
dd01d79
ccefae6
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 |
---|---|---|
|
@@ -7,8 +7,7 @@ import { | |
Body, | ||
Param, | ||
Query, | ||
NotFoundException, | ||
InternalServerErrorException, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiTags, | ||
|
@@ -24,18 +23,22 @@ import { UserRole } from 'src/shared/enums/userRole.enum'; | |
import { Scopes } from 'src/shared/decorators/scopes.decorator'; | ||
import { Scope } from 'src/shared/enums/scopes.enum'; | ||
import { | ||
ScorecardPaginatedResponseDto, | ||
ScorecardRequestDto, | ||
ScorecardResponseDto, | ||
ScorecardWithGroupResponseDto, | ||
mapScorecardRequestToDto, | ||
} from 'src/dto/scorecard.dto'; | ||
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 import |
||
import { ChallengeTrack } from 'src/shared/enums/challengeTrack.enum'; | ||
import { PrismaService } from '../../shared/modules/global/prisma.service'; | ||
import { ScoreCardService } from './scorecard.service'; | ||
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 import statement for |
||
import { PaginationHeaderInterceptor } from 'src/interceptors/PaginationHeaderInterceptor'; | ||
|
||
@ApiTags('Scorecard') | ||
@ApiBearerAuth() | ||
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 |
||
@Controller('/scorecards') | ||
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 |
||
export class ScorecardController { | ||
constructor(private readonly prisma: PrismaService) {} | ||
constructor(private readonly prisma: PrismaService, private readonly scorecardService: ScoreCardService) {} | ||
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 constructor parameter |
||
|
||
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 |
||
@Post() | ||
@Roles(UserRole.Admin) | ||
|
@@ -48,27 +51,13 @@ export class ScorecardController { | |
@ApiResponse({ | ||
status: 201, | ||
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. Are really returning 201 here? Probably change to 200... 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. @kkartunov This is for adding scorecard, normally for adding we return 201 so this looks like its returning correct, for search api it returns 200. |
||
description: 'Scorecard added successfully.', | ||
type: ScorecardResponseDto, | ||
type: ScorecardWithGroupResponseDto, | ||
}) | ||
@ApiResponse({ status: 403, description: 'Forbidden.' }) | ||
async addScorecard( | ||
@Body() body: ScorecardRequestDto, | ||
): Promise<ScorecardResponseDto> { | ||
const data = await this.prisma.scorecard.create({ | ||
data: mapScorecardRequestToDto(body), | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
sections: { | ||
include: { | ||
questions: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return data as ScorecardResponseDto; | ||
): Promise<ScorecardWithGroupResponseDto> { | ||
return await this.scorecardService.addScorecard(body); | ||
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 |
||
} | ||
|
||
@Put('/:id') | ||
|
@@ -87,41 +76,15 @@ export class ScorecardController { | |
@ApiResponse({ | ||
status: 200, | ||
description: 'Scorecard updated successfully.', | ||
type: ScorecardResponseDto, | ||
type: ScorecardWithGroupResponseDto, | ||
}) | ||
@ApiResponse({ status: 403, description: 'Forbidden.' }) | ||
@ApiResponse({ status: 404, description: 'Scorecard not found.' }) | ||
async editScorecard( | ||
@Param('id') id: string, | ||
@Body() body: ScorecardRequestDto, | ||
): Promise<ScorecardResponseDto> { | ||
console.log(JSON.stringify(body)); | ||
|
||
const data = await this.prisma.scorecard | ||
.update({ | ||
where: { id }, | ||
data: mapScorecardRequestToDto(body), | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
sections: { | ||
include: { | ||
questions: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
.catch((error) => { | ||
if (error.code !== 'P2025') { | ||
throw new NotFoundException({ message: `Scorecard not found.` }); | ||
} | ||
throw new InternalServerErrorException({ | ||
message: `Error: ${error.code}`, | ||
}); | ||
}); | ||
return data as ScorecardResponseDto; | ||
@Body() body: ScorecardWithGroupResponseDto, | ||
): Promise<ScorecardWithGroupResponseDto> { | ||
return await this.scorecardService.editScorecard(id, body); | ||
} | ||
|
||
@Delete(':id') | ||
|
@@ -139,24 +102,12 @@ export class ScorecardController { | |
@ApiResponse({ | ||
status: 200, | ||
description: 'Scorecard deleted successfully.', | ||
type: ScorecardResponseDto, | ||
type: 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. The type for the API response has been changed from |
||
}) | ||
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 |
||
@ApiResponse({ status: 403, description: 'Forbidden.' }) | ||
@ApiResponse({ status: 404, description: 'Scorecard not found.' }) | ||
async deleteScorecard(@Param('id') id: string) { | ||
await this.prisma.scorecard | ||
.delete({ | ||
where: { id }, | ||
}) | ||
.catch((error) => { | ||
if (error.code !== 'P2025') { | ||
throw new NotFoundException({ message: `Scorecard not found.` }); | ||
} | ||
throw new InternalServerErrorException({ | ||
message: `Error: ${error.code}`, | ||
}); | ||
}); | ||
return { message: `Scorecard ${id} deleted successfully.` }; | ||
return await this.scorecardService.deleteScorecard(id); | ||
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 error handling logic has been removed from this method. Ensure that |
||
} | ||
|
||
@Get('/:id') | ||
|
@@ -173,38 +124,15 @@ export class ScorecardController { | |
@ApiResponse({ | ||
status: 200, | ||
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 error handling logic for |
||
description: 'Scorecard retrieved successfully.', | ||
type: ScorecardResponseDto, | ||
type: ScorecardWithGroupResponseDto, | ||
}) | ||
@ApiResponse({ status: 404, description: 'Scorecard not found.' }) | ||
async viewScorecard(@Param('id') id: string): Promise<ScorecardResponseDto> { | ||
const data = await this.prisma.scorecard | ||
.findUniqueOrThrow({ | ||
where: { id }, | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
sections: { | ||
include: { | ||
questions: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
.catch((error) => { | ||
if (error.code !== 'P2025') { | ||
throw new NotFoundException({ message: `Scorecard not found.` }); | ||
} | ||
throw new InternalServerErrorException({ | ||
message: `Error: ${error.code}`, | ||
}); | ||
}); | ||
return data as ScorecardResponseDto; | ||
async viewScorecard(@Param('id') id: string): Promise<ScorecardWithGroupResponseDto> { | ||
return await this.scorecardService.viewScorecard(id); | ||
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 error handling logic for |
||
} | ||
|
||
@Get() | ||
@Roles(UserRole.Admin, UserRole.Copilot) | ||
@Roles(UserRole.Admin) | ||
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. SuggestionConsider the implications of removing the |
||
@Scopes(Scope.ReadScorecard) | ||
@ApiOperation({ | ||
summary: 'Search scorecards', | ||
|
@@ -249,34 +177,31 @@ export class ScorecardController { | |
description: 'List of matching scorecards', | ||
type: [ScorecardResponseDto], | ||
}) | ||
@UseInterceptors(PaginationHeaderInterceptor) | ||
async searchScorecards( | ||
@Query('challengeTrack') challengeTrack?: ChallengeTrack, | ||
@Query('challengeType') challengeType?: string, | ||
@Query('challengeTrack') challengeTrack?: ChallengeTrack | ChallengeTrack[], | ||
@Query('challengeType') challengeType?: string | string[], | ||
@Query('name') name?: string, | ||
@Query('page') page: number = 1, | ||
@Query('perPage') perPage: number = 10, | ||
) { | ||
const skip = (page - 1) * perPage; | ||
const data = await this.prisma.scorecard.findMany({ | ||
where: { | ||
...(challengeTrack && { challengeTrack }), | ||
...(challengeType && { challengeType }), | ||
...(name && { name: { contains: name, mode: 'insensitive' } }), | ||
}, | ||
include: { | ||
scorecardGroups: { | ||
include: { | ||
sections: { | ||
include: { | ||
questions: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
skip, | ||
take: perPage, | ||
): Promise<ScorecardPaginatedResponseDto> { | ||
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 return type of the |
||
const challengeTrackArray = Array.isArray(challengeTrack) | ||
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 logic for converting |
||
? challengeTrack | ||
: challengeTrack | ||
? [challengeTrack] | ||
: []; | ||
const challengeTypeArray = Array.isArray(challengeType) | ||
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 logic for converting |
||
? challengeType | ||
: challengeType | ||
? [challengeType] | ||
: []; | ||
const result = await this.scorecardService.getScoreCards({ | ||
challengeTrack: challengeTrackArray, | ||
challengeType: challengeTypeArray, | ||
name, | ||
page, | ||
perPage, | ||
}); | ||
return data as ScorecardResponseDto[]; | ||
return result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScoreCardService
is added to the providers array, but ensure that it is properly imported at the top of the file. If it's already imported, verify that the import path is correct and matches the file structure.