Skip to content

Commit

Permalink
Add validations for event_date
Browse files Browse the repository at this point in the history
  • Loading branch information
abarghoud committed Nov 10, 2024
1 parent e236eb4 commit 9cd98ea
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions apps/api/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default {
displayName: 'api',
preset: '../../jest.preset.js',
testEnvironment: 'node',
setupFiles: ['./jest.setup.ts'],
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
Expand Down
1 change: 1 addition & 0 deletions apps/api/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'reflect-metadata';
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 14 additions & 3 deletions apps/api/src/app/pps/domain/pps-profile-dto.model.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';
ppsDto.gender = Gender.male;
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/pps/third-party/pps-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/pps/usecase/pps-generator-usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9cd98ea

Please sign in to comment.