Skip to content

Commit

Permalink
chore(be): delete unneeded exception logics on assignment module
Browse files Browse the repository at this point in the history
  • Loading branch information
jimin9038 committed Jan 12, 2025
1 parent 045561c commit c990c8b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 142 deletions.
144 changes: 36 additions & 108 deletions apps/backend/apps/client/src/assignment/assignment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import {
Controller,
InternalServerErrorException,
NotFoundException,
Param,
Post,
Req,
Get,
Query,
Logger,
DefaultValuePipe,
Delete
} from '@nestjs/common'
import { Prisma } from '@prisma/client'
import {
AuthNotNeededIfOpenSpace,
AuthenticatedRequest,
UserNullWhenAuthFailedIfOpenSpace
} from '@libs/auth'
import {
ConflictFoundException,
EntityNotExistException,
ForbiddenAccessException
} from '@libs/exception'
import {
CursorValidationPipe,
GroupIDPipe,
Expand All @@ -32,37 +23,25 @@ import { AssignmentService } from './assignment.service'

@Controller('assignment')
export class AssignmentController {
private readonly logger = new Logger(AssignmentController.name)

constructor(private readonly assignmentService: AssignmentService) {}

@Get('ongoing-upcoming')
@AuthNotNeededIfOpenSpace()
async getOngoingUpcomingAssignments(
@Query('groupId', GroupIDPipe) groupId: number
) {
try {
return await this.assignmentService.getAssignmentsByGroupId(groupId)
} catch (error) {
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.assignmentService.getAssignmentsByGroupId(groupId)
}

@Get('ongoing-upcoming-with-registered')
async getOngoingUpcomingAssignmentsWithRegistered(
@Req() req: AuthenticatedRequest,
@Query('groupId', GroupIDPipe) groupId: number
) {
try {
return await this.assignmentService.getAssignmentsByGroupId(
groupId,
req.user.id
)
} catch (error) {
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.assignmentService.getAssignmentsByGroupId(
groupId,
req.user.id
)
}

@Get('finished')
Expand All @@ -75,18 +54,13 @@ export class AssignmentController {
take: number,
@Query('search') search?: string
) {
try {
return await this.assignmentService.getFinishedAssignmentsByGroupId(
req.user?.id,
cursor,
take,
groupId,
search
)
} catch (error) {
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.assignmentService.getFinishedAssignmentsByGroupId(
req.user?.id,
cursor,
take,
groupId,
search
)
}

@Get('registered-finished')
Expand All @@ -98,18 +72,13 @@ export class AssignmentController {
take: number,
@Query('search') search?: string
) {
try {
return await this.assignmentService.getRegisteredFinishedAssignments(
cursor,
take,
groupId,
req.user.id,
search
)
} catch (error) {
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.assignmentService.getRegisteredFinishedAssignments(
cursor,
take,
groupId,
req.user.id,
search
)
}

@Get('registered-ongoing-upcoming')
Expand All @@ -118,16 +87,11 @@ export class AssignmentController {
@Query('groupId', GroupIDPipe) groupId: number,
@Query('search') search?: string
) {
try {
return await this.assignmentService.getRegisteredOngoingUpcomingAssignments(
groupId,
req.user.id,
search
)
} catch (error) {
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.assignmentService.getRegisteredOngoingUpcomingAssignments(
groupId,
req.user.id,
search
)
}

@Get(':id')
Expand All @@ -137,19 +101,7 @@ export class AssignmentController {
@Query('groupId', GroupIDPipe) groupId: number,
@Param('id', new RequiredIntPipe('id')) id: number
) {
try {
return await this.assignmentService.getAssignment(
id,
groupId,
req.user?.id
)
} catch (error) {
if (error instanceof EntityNotExistException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.assignmentService.getAssignment(id, groupId, req.user?.id)
}

@Post(':id/participation')
Expand All @@ -159,25 +111,12 @@ export class AssignmentController {
@Param('id', IDValidationPipe) assignmentId: number,
@Query('invitationCode') invitationCode?: string
) {
try {
return await this.assignmentService.createAssignmentRecord(
assignmentId,
req.user.id,
invitationCode,
groupId
)
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.name === 'NotFoundError'
) {
throw new NotFoundException(error.message)
} else if (error instanceof ConflictFoundException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException(error.message)
}
return await this.assignmentService.createAssignmentRecord(
assignmentId,
req.user.id,
invitationCode,
groupId
)
}

// unregister only for upcoming Assignment
Expand All @@ -187,21 +126,10 @@ export class AssignmentController {
@Query('groupId', GroupIDPipe) groupId: number,
@Param('id', IDValidationPipe) assignmentId: number
) {
try {
return await this.assignmentService.deleteAssignmentRecord(
assignmentId,
req.user.id,
groupId
)
} catch (error) {
if (
error instanceof ForbiddenAccessException ||
error instanceof EntityNotExistException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException(error.message)
}
return await this.assignmentService.deleteAssignmentRecord(
assignmentId,
req.user.id,
groupId
)
}
}
51 changes: 17 additions & 34 deletions apps/backend/apps/client/src/assignment/assignment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,50 +467,33 @@ export class AssignmentService {
userId: number,
groupId = OPEN_SPACE_ID
) {
let assignment
try {
assignment = await this.prisma.assignment.findUniqueOrThrow({
const [assignment, assignmentRecord] = await Promise.all([
this.prisma.contest.findUnique({
where: { id: assignmentId, groupId }
})
} catch (err) {
if (
err instanceof Prisma.PrismaClientKnownRequestError &&
err.code === 'P2025'
) {
throw new EntityNotExistException('Assignment')
}
}
try {
await this.prisma.assignmentRecord.findFirstOrThrow({
}),
this.prisma.assignmentRecord.findFirst({
where: { userId, assignmentId }
})
} catch (err) {
if (
err instanceof Prisma.PrismaClientKnownRequestError &&
err.code === 'P2025'
) {
throw new EntityNotExistException('AssignmentRecord')
}
])

if (!assignment) {
throw new EntityNotExistException('Contest')
}

if (!assignmentRecord) {
throw new EntityNotExistException('ContestRecord')
}

const now = new Date()
if (now >= assignment.startTime) {
throw new ForbiddenAccessException(
'Cannot unregister ongoing or ended assignment'
)
}

try {
return await this.prisma.assignmentRecord.delete({
// eslint-disable-next-line @typescript-eslint/naming-convention
where: { assignmentId_userId: { assignmentId, userId } }
})
} catch (err) {
if (
err instanceof Prisma.PrismaClientKnownRequestError &&
err.code === 'P2025'
) {
throw new EntityNotExistException('AssignmentRecord')
}
}
return await this.prisma.assignmentRecord.delete({
// eslint-disable-next-line @typescript-eslint/naming-convention
where: { assignmentId_userId: { assignmentId, userId } }
})
}
}

0 comments on commit c990c8b

Please sign in to comment.