generated from kir-dev/next-nest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from kir-dev/feat/4-application-period-crud
added basic crud for application periods
- Loading branch information
Showing
11 changed files
with
312 additions
and
9 deletions.
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)); | ||
} | ||
} |
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.