Skip to content
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

Add validations for event_date #15

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
abarghoud marked this conversation as resolved.
Show resolved Hide resolved
}
}
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