Skip to content

Commit

Permalink
feat: [Contracts] Validate legal guardian only for under 16 volunteers
Browse files Browse the repository at this point in the history
  • Loading branch information
radulescuandrew committed Sep 19, 2024
1 parent a2423b3 commit 7015b06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
17 changes: 17 additions & 0 deletions backend/src/common/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { differenceInYears } from 'date-fns';
import * as XLSX from 'xlsx';

export function JSONStringifyError(value: Error): string {
Expand Down Expand Up @@ -25,3 +26,19 @@ export function jsonToExcelBuffer<T>(

return XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' });
}

export const isOver16FromCNP = (cnp: string): boolean => {
// we don't need to perform the calculation before the user has entered all the necessary digits to calculate
if (cnp.length !== 13) {
throw new Error('CNP must be 13 digits long');
}
// if first digit is above 5, then the birth year is 2000+
const yearPrefix = parseInt(cnp[0], 10) < 5 ? '19' : '20';
const year = (yearPrefix + cnp.substring(1, 3)).toString();
const month = cnp.substring(3, 5);
const day = cnp.substring(5, 7);
const birthday = new Date(`${year}-${month}-${day}`);

const age = differenceInYears(new Date(), birthday);
return age >= 16;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import { isOver16FromCNP } from 'src/common/helpers/utils';
import { IUseCaseService } from 'src/common/interfaces/use-case-service.interface';
import { ExceptionsService } from 'src/infrastructure/exceptions/exceptions.service';
import { DocumentContractStatus } from 'src/modules/documents/enums/contract-status.enum';
Expand Down Expand Up @@ -44,10 +45,7 @@ export class CreateDocumentContractUsecase implements IUseCaseService<string> {
>,
): Promise<string> {
// 1. check if the organization exists
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const organization = await this.getOrganizationUsecase.execute(
newContract.organizationId,
);
await this.getOrganizationUsecase.execute(newContract.organizationId);

// 2. check if the volunteer exists
const volunteer = await this.checkVolunteerExists(
Expand All @@ -68,10 +66,18 @@ export class CreateDocumentContractUsecase implements IUseCaseService<string> {
// 6. Extract volunteerData and volunteerTutorData from the user
const volunteerPersonalData = volunteer.user.userPersonalData;

// console.log(volunteerPersonalData);

await this.validateVolunteerPersonalData(volunteerPersonalData);
await this.validateLegalGuardianData(volunteerPersonalData.legalGuardian);

if (!isOver16FromCNP(volunteerPersonalData.cnp)) {
if (!volunteerPersonalData.legalGuardian) {
this.exceptionsService.badRequestException({
message: 'Legal guardian data is required for under 16 volunteers',
code_error: 'LEGAL_GUARDIAN_DATA_REQUIRED',
});
}

await this.validateLegalGuardianData(volunteerPersonalData.legalGuardian);
}

const newContractOptions: CreateDocumentContractOptions = {
...newContract,
Expand Down

0 comments on commit 7015b06

Please sign in to comment.