From 9cd98ea37427a8add47135c4496fbace776b1752 Mon Sep 17 00:00:00 2001 From: Anas Barghoud Date: Mon, 11 Nov 2024 00:05:17 +0100 Subject: [PATCH] Add validations for event_date --- apps/api/jest.config.ts | 1 + apps/api/jest.setup.ts | 1 + .../pps-profile-dto-to-runner-personal-infos.ts | 2 +- .../src/app/pps/domain/pps-profile-dto.model.ts | 17 ++++++++++++++--- .../pps-api-form-data-generator.spec.ts | 2 +- .../api/src/app/pps/third-party/pps-api.spec.ts | 2 +- .../pps/usecase/pps-generator-usecase.spec.ts | 2 +- 7 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 apps/api/jest.setup.ts diff --git a/apps/api/jest.config.ts b/apps/api/jest.config.ts index b2c2496..66da57a 100644 --- a/apps/api/jest.config.ts +++ b/apps/api/jest.config.ts @@ -2,6 +2,7 @@ export default { displayName: 'api', preset: '../../jest.preset.js', testEnvironment: 'node', + setupFiles: ['./jest.setup.ts'], transform: { '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }] }, diff --git a/apps/api/jest.setup.ts b/apps/api/jest.setup.ts new file mode 100644 index 0000000..d2c9bc6 --- /dev/null +++ b/apps/api/jest.setup.ts @@ -0,0 +1 @@ +import 'reflect-metadata'; diff --git a/apps/api/src/app/pps/domain/pps-profile-dto-to-runner-personal-infos.ts b/apps/api/src/app/pps/domain/pps-profile-dto-to-runner-personal-infos.ts index b87a5ad..9b898be 100644 --- a/apps/api/src/app/pps/domain/pps-profile-dto-to-runner-personal-infos.ts +++ b/apps/api/src/app/pps/domain/pps-profile-dto-to-runner-personal-infos.ts @@ -21,6 +21,6 @@ export class PPSProfileDTOToRunnerPersonalInfos { this.firstname = ppsDto.firstname; this.gender = ppsDto.gender; this.lastname = ppsDto.lastname; - this.eventDate = DateTime.fromISO(ppsDto.event_date); + this.eventDate = DateTime.fromJSDate(ppsDto.event_date); } } diff --git a/apps/api/src/app/pps/domain/pps-profile-dto.model.ts b/apps/api/src/app/pps/domain/pps-profile-dto.model.ts index 3216c35..331dc3d 100644 --- a/apps/api/src/app/pps/domain/pps-profile-dto.model.ts +++ b/apps/api/src/app/pps/domain/pps-profile-dto.model.ts @@ -1,6 +1,8 @@ -import { IsDateString, IsEmail, IsEnum, IsString } from 'class-validator'; +import { IsDate, IsDateString, IsEmail, IsEnum, IsString, MaxDate, MinDate } from 'class-validator'; +import { Type } from 'class-transformer'; import { Gender } from '../../gender.enum'; +import { DateTime } from 'luxon'; export class PPSProfileDto { @IsDateString() @@ -9,8 +11,17 @@ export class PPSProfileDto { @IsEmail() public email: string; - @IsDateString() - public event_date: string; + @Type(() => Date) + @IsDate() + @MinDate( + () => DateTime.now().toJSDate(), + { message: 'event_date cannot be in the past' } + ) + @MaxDate( + () => DateTime.now().plus({ month: 3 }).toJSDate(), + { message: 'event_date should be within 3 months' } + ) + public event_date: Date; @IsString() public firstname: string; diff --git a/apps/api/src/app/pps/third-party/pps-api-form-data-generator.spec.ts b/apps/api/src/app/pps/third-party/pps-api-form-data-generator.spec.ts index 0e9b693..12a6904 100644 --- a/apps/api/src/app/pps/third-party/pps-api-form-data-generator.spec.ts +++ b/apps/api/src/app/pps/third-party/pps-api-form-data-generator.spec.ts @@ -6,7 +6,7 @@ import { Gender } from '../../gender.enum'; const getFakePPSDto = (): PPSProfileDto => { const ppsDto = new PPSProfileDto(); - ppsDto.event_date = '2024-12-31'; + ppsDto.event_date = new Date('2024-12-31'); ppsDto.birthday = '1990-01-01'; ppsDto.email = 'test@test.com'; ppsDto.gender = Gender.male; diff --git a/apps/api/src/app/pps/third-party/pps-api.spec.ts b/apps/api/src/app/pps/third-party/pps-api.spec.ts index 917c8c1..6231e33 100644 --- a/apps/api/src/app/pps/third-party/pps-api.spec.ts +++ b/apps/api/src/app/pps/third-party/pps-api.spec.ts @@ -21,7 +21,7 @@ import { PPSId } from '../domain/pps-id.type'; const getFakePPSDto = (): PPSProfileDto => { const ppsDto = new PPSProfileDto(); - ppsDto.event_date = '2024-12-31'; + ppsDto.event_date = new Date('2024-12-31'); return ppsDto; }; diff --git a/apps/api/src/app/pps/usecase/pps-generator-usecase.spec.ts b/apps/api/src/app/pps/usecase/pps-generator-usecase.spec.ts index 11e9e84..1070f71 100644 --- a/apps/api/src/app/pps/usecase/pps-generator-usecase.spec.ts +++ b/apps/api/src/app/pps/usecase/pps-generator-usecase.spec.ts @@ -29,7 +29,7 @@ describe('The PPSGeneratorUseCase class', () => { let result: PPSId; beforeAll(async () => { - ppsProfileDto.event_date = '2024-11-01'; + ppsProfileDto.event_date = new Date('2024-11-01'); mockPpsApi.finalize.mockResolvedValue(fakePPSId); result = await app.get(PPSGeneratorUseCase).generate(ppsProfileDto);