generated from kir-dev/next-nest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
added basic crud for application periods #17
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fdb816f
added basic crud for application periods
FearsomeRover f121b24
fixed overlap checks, error types
FearsomeRover 94c42a0
Merge branch 'main' into feat/4-application-period-crud
FearsomeRover 3d67a0e
added shady validation for dates
FearsomeRover File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
apps/backend/src/application-period/application-period.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { CurrentUser } from '@kir-dev/passport-authsch'; | ||
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiBearerAuth } from '@nestjs/swagger'; | ||
import { ApplicationPeriod, Role, User } from '@prisma/client'; | ||
import { Roles } from 'src/auth/decorators/Roles.decorator'; | ||
import { RolesGuard } from 'src/auth/roles.guard'; | ||
|
||
import { ApplicationPeriodService } from './application-period.service'; | ||
import { CreateApplicationPeriodDto } from './dto/create-application-period.dto'; | ||
import { GetApplicationPeriodsDto } from './dto/get-application-periods.dto'; | ||
import { SimpleApplicationPeriodDto } from './dto/simple-application-period.dto'; | ||
import { UpdateApplicationPeriodDto } from './dto/update-application-period.dto'; | ||
|
||
@Controller('application-periods') | ||
export class ApplicationPeriodController { | ||
constructor(private readonly applicationPeriodService: ApplicationPeriodService) {} | ||
|
||
@Get() | ||
async findAll(@Query() getApplicationPeriodsDto: GetApplicationPeriodsDto): Promise<SimpleApplicationPeriodDto[]> { | ||
return this.applicationPeriodService.findAll(getApplicationPeriodsDto); | ||
} | ||
|
||
@Get('current') | ||
async getCurrentPeriod(): Promise<ApplicationPeriod> { | ||
return this.applicationPeriodService.getCurrentPeriod(); | ||
} | ||
|
||
@Get(':id') | ||
async findOne(@Param('id') id: string): Promise<ApplicationPeriod> { | ||
return this.applicationPeriodService.findOne(Number(id)); | ||
} | ||
|
||
@UseGuards(AuthGuard('jwt'), RolesGuard) | ||
@ApiBearerAuth() | ||
@Roles(Role.BODY_ADMIN, Role.BODY_MEMBER) | ||
@Post() | ||
async create( | ||
@Body() createApplicationPeriodDto: CreateApplicationPeriodDto, | ||
@CurrentUser() user: User | ||
): Promise<ApplicationPeriod> { | ||
return this.applicationPeriodService.create(createApplicationPeriodDto, user); | ||
} | ||
|
||
@UseGuards(AuthGuard('jwt'), RolesGuard) | ||
@ApiBearerAuth() | ||
@Roles(Role.BODY_ADMIN, Role.BODY_MEMBER) | ||
@Delete(':id') | ||
async delete(@Param('id') id: string): Promise<ApplicationPeriod> { | ||
return this.applicationPeriodService.delete(Number(id)); | ||
} | ||
|
||
@UseGuards(AuthGuard('jwt'), RolesGuard) | ||
@ApiBearerAuth() | ||
@Roles(Role.BODY_ADMIN, Role.BODY_MEMBER) | ||
@Patch(':id') | ||
async update( | ||
@Body() updateApplicationPeriodDto: UpdateApplicationPeriodDto, | ||
@Param() id: string | ||
): Promise<ApplicationPeriod> { | ||
return this.applicationPeriodService.update(updateApplicationPeriodDto, Number(id)); | ||
} | ||
Comment on lines
+34
to
+62
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. not sure if this was discussed with them, but maybe it'd make sense if only the body admin could do these |
||
} |
10 changes: 10 additions & 0 deletions
10
apps/backend/src/application-period/application-period.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { ApplicationPeriodController } from './application-period.controller'; | ||
import { ApplicationPeriodService } from './application-period.service'; | ||
|
||
@Module({ | ||
controllers: [ApplicationPeriodController], | ||
providers: [ApplicationPeriodService], | ||
}) | ||
export class ApplicationPeriodModule {} |
144 changes: 144 additions & 0 deletions
144
apps/backend/src/application-period/application-period.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; | ||
import { ApplicationPeriod, Prisma } from '@prisma/client'; | ||
import { PrismaService } from 'nestjs-prisma'; | ||
|
||
import { CreateApplicationPeriodDto } from './dto/create-application-period.dto'; | ||
import { GetApplicationPeriodsDto } from './dto/get-application-periods.dto'; | ||
import { UpdateApplicationPeriodDto } from './dto/update-application-period.dto'; | ||
|
||
@Injectable() | ||
export class ApplicationPeriodService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
findAll(getApplicationPeriodsDto: GetApplicationPeriodsDto) { | ||
const skip = getApplicationPeriodsDto.page * getApplicationPeriodsDto.page_size; | ||
return this.prisma.applicationPeriod.findMany({ | ||
skip, | ||
take: Number(getApplicationPeriodsDto.page_size), | ||
}); | ||
} | ||
async getCurrentPeriod(): Promise<ApplicationPeriod> { | ||
const period = await this.prisma.applicationPeriod.findFirst({ | ||
where: { | ||
applicationPeriodStartAt: { | ||
lte: new Date(), | ||
}, | ||
applicationPeriodEndAt: { | ||
gte: new Date(), | ||
}, | ||
}, | ||
}); | ||
if (!period) { | ||
throw new NotFoundException('No current application period found'); | ||
} | ||
return period; | ||
} | ||
|
||
async findOne(id: number): Promise<ApplicationPeriod> { | ||
const period = await this.prisma.applicationPeriod.findUnique({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
if (!period) { | ||
throw new NotFoundException('Application period not found'); | ||
} | ||
return period; | ||
} | ||
|
||
async create(createApplicationPeriodDto: CreateApplicationPeriodDto, user): Promise<ApplicationPeriod> { | ||
try { | ||
const conflictingPeriod = await this.prisma.applicationPeriod.findFirst({ | ||
where: { | ||
AND: [ | ||
{ | ||
applicationPeriodStartAt: { | ||
lte: createApplicationPeriodDto.applicationPeriodEndAt, | ||
}, | ||
}, | ||
{ | ||
applicationPeriodEndAt: { | ||
gte: createApplicationPeriodDto.applicationPeriodStartAt, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
if (conflictingPeriod) { | ||
throw new BadRequestException('Application period overlaps with another period'); | ||
} | ||
} catch (e: any) { | ||
if (e instanceof Prisma.PrismaClientValidationError) { | ||
throw new BadRequestException('Invalid date format'); | ||
} | ||
} | ||
return this.prisma.applicationPeriod.create({ | ||
data: { | ||
...createApplicationPeriodDto, | ||
author: { | ||
connect: { | ||
authSchId: user.authSchId, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async delete(id: number): Promise<ApplicationPeriod> { | ||
try { | ||
return await this.prisma.applicationPeriod.delete({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
} catch (e) { | ||
throw new NotFoundException('Application period not found'); | ||
} | ||
} | ||
|
||
async update(updateApplicationPeriodDto: UpdateApplicationPeriodDto, id: number): Promise<ApplicationPeriod> { | ||
if ( | ||
Boolean(updateApplicationPeriodDto.applicationPeriodStartAt) || | ||
Boolean(updateApplicationPeriodDto.applicationPeriodEndAt) | ||
) { | ||
try { | ||
const conflictingPeriod = await this.prisma.applicationPeriod.findFirst({ | ||
where: { | ||
AND: [ | ||
{ | ||
applicationPeriodStartAt: { | ||
lte: updateApplicationPeriodDto.applicationPeriodEndAt, | ||
}, | ||
}, | ||
{ | ||
applicationPeriodEndAt: { | ||
gte: updateApplicationPeriodDto.applicationPeriodStartAt, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
if (conflictingPeriod) { | ||
throw new BadRequestException('Application period overlaps with another period'); | ||
} | ||
} catch (e: any) { | ||
if (e instanceof Prisma.PrismaClientValidationError) { | ||
throw new BadRequestException('Invalid date format'); | ||
} | ||
} | ||
} | ||
|
||
try { | ||
return this.prisma.applicationPeriod.update({ | ||
where: { | ||
id, | ||
}, | ||
data: updateApplicationPeriodDto, | ||
}); | ||
} catch (e) { | ||
if (e instanceof Prisma.PrismaClientKnownRequestError) { | ||
throw new BadRequestException('Invalid date format'); | ||
} | ||
throw new BadRequestException('Application period not found'); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/backend/src/application-period/dto/create-application-period.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IsBoolean, IsDateString, IsISO8601, IsOptional } from 'class-validator'; | ||
|
||
export class CreateApplicationPeriodDto { | ||
name: string; | ||
@IsDateString() | ||
@IsISO8601() | ||
applicationPeriodStartAt: Date; | ||
@IsDateString() | ||
@IsISO8601() | ||
applicationPeriodEndAt: Date; | ||
@IsOptional() | ||
@IsBoolean() | ||
ticketsAreValid?: boolean; | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/backend/src/application-period/dto/get-application-periods.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { IsInt, IsOptional, Min } from 'class-validator'; | ||
export class GetApplicationPeriodsDto { | ||
@IsOptional() | ||
@IsInt() | ||
@Min(0) | ||
page: number; | ||
@IsOptional() | ||
@IsInt() | ||
@Min(1) | ||
page_size: number; | ||
} |
10 changes: 10 additions & 0 deletions
10
apps/backend/src/application-period/dto/simple-application-period.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class SimpleApplicationPeriodDto { | ||
id: number; | ||
name: string; | ||
applicationPeriodStartAt: Date; | ||
applicationPeriodEndAt: Date; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
ticketsAreValid: boolean; | ||
authorId: string; | ||
} |
5 changes: 5 additions & 0 deletions
5
apps/backend/src/application-period/dto/update-application-period.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PartialType } from '@nestjs/swagger'; | ||
|
||
import { CreateApplicationPeriodDto } from './create-application-period.dto'; | ||
|
||
export class UpdateApplicationPeriodDto extends PartialType(CreateApplicationPeriodDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do regular users need access to the findAll and findOne methods?
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.
I don't really see the point of them not seeing it. But we should discuss the permissions regardless