Skip to content

Commit

Permalink
fix(backend): fix search for birth date & phone
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Jan 28, 2025
1 parent 640e798 commit 6e683e3
Show file tree
Hide file tree
Showing 30 changed files with 223 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
margin-left: 5px;
"
>
+33 1 02 03 04 05
01 02 03 04 05
</li>
</ul>
<p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
margin-left: 5px;
"
>
+33 1 02 03 04 05
01 02 03 04 05
</li>
</ul>
<p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("usagersImportValidator parse invalid data", () => {
rowNumber: 10,
value: {
countryCode: "fr",
numero: "0102030405 et des lettres",
numero: "0102030405",
},
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export class SearchUsagersController {
structureId: user.structureId,
});

console.log(search);
if (search.searchString?.length > 0) {
if (search.searchStringField === CriteriaSearchField.DEFAULT) {
query.andWhere("nom_prenom_surnom_ref ILIKE :str", {
Expand All @@ -137,7 +136,6 @@ export class SearchUsagersController {
} else if (
search.searchStringField === CriteriaSearchField.PHONE_NUMBER
) {
console.log(search.searchString);
query.andWhere(`telephone->>'numero' ILIKE :phone`, {
phone: `%${search.searchString}%`,
});
Expand Down Expand Up @@ -199,14 +197,6 @@ export class SearchUsagersController {
}
}

console.log({
searchString: !search.searchString,
echeance: !search?.echeance,
entretien: !search?.entretien,
referrerId: typeof search?.referrerId === "undefined",
lastInteractionDate: !search?.lastInteractionDate,
});

if (
!search.searchString &&
!search?.echeance &&
Expand Down
10 changes: 4 additions & 6 deletions packages/backend/src/usagers/dto/search-usager.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IsIn, IsNumber, IsOptional, ValidateIf } from "class-validator";
import {
CriteriaSearchField,
normalizeString,
parseBirthDate,
UsagersFilterCriteriaDernierPassage,
UsagersFilterCriteriaEcheance,
UsagersFilterCriteriaEntretien,
Expand All @@ -20,13 +21,10 @@ export class SearchUsagerDto {
return null;
}

if (
[
CriteriaSearchField.PHONE_NUMBER,
CriteriaSearchField.BIRTH_DATE,
].includes(obj.searchStringField)
) {
if (CriteriaSearchField.PHONE_NUMBER === obj.searchStringField) {
return value.replace(/\D/g, "");
} else if (CriteriaSearchField.BIRTH_DATE === obj.searchStringField) {
return parseBirthDate(value);
}

return normalizeString(value).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const CERFA_MOCK_USAGER_ACTIF: UsagerCerfaFields = {
signature1A: "ASNIERES-SUR-SEINE",
signature1B: "ASNIERES-SUR-SEINE",
signature2: "ASNIERES-SUR-SEINE",
telephone: "+33 6 06 06 06 06",
telephoneOrga: "+33 6 02 03 04 05",
telephone: "06 06 06 06 06",
telephoneOrga: "06 02 03 04 05",
typeDemande: "2",
};

Expand Down Expand Up @@ -124,6 +124,6 @@ export const CERFA_MOCK_USAGER_REFUS: UsagerCerfaFields = {
signature1B: "ASNIERES-SUR-SEINE",
signature2: "ASNIERES-SUR-SEINE",
telephone: "",
telephoneOrga: "+33 6 02 03 04 05",
telephoneOrga: "06 02 03 04 05",
typeDemande: "1",
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const CUSTOM_DOC_ATTESTATION_POSTALE: StructureCustomDocTags = {
USAGER_SURNOM: "TEST",
USAGER_DATE_NAISSANCE: "02/11/1988",
USAGER_LIEU_NAISSANCE: "Paris",
USAGER_PHONE: "+33 6 06 06 06 06",
USAGER_PHONE: "06 06 06 06 06",
USAGER_EMAIL: "[email protected]",
USAGER_NUMERO_DISTRIBUTION_SPECIALE: "",
STATUT_DOM: "Actif",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const CUSTOM_DOC_COURRIER_REFUS: StructureCustomDocTags = {
USAGER_SURNOM: "",
USAGER_DATE_NAISSANCE: "07/08/1998",
USAGER_LIEU_NAISSANCE: "Bouaké, Côte d'Ivoire",
USAGER_PHONE: "+33 6 06 06 06 06",
USAGER_PHONE: "06 06 06 06 06",
USAGER_EMAIL: "[email protected]",
AYANTS_DROITS_LISTE: "Karamoko Mauricette né(e) le 20/12/1978",
STATUT_DOM: "Refusé",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const FIRST_SHEET_USAGERS = [
USAGER_NATIONALITE: "",
USAGER_NOM: "NOUVEAU",
USAGER_NUMERO_DISTRIBUTION_SPECIALE: "",
USAGER_PHONE: "+33 6 06 06 06 06",
USAGER_PHONE: "06 06 06 06 06",
USAGER_PRENOM: "DOSSIER",
USAGER_CUSTOM_REF: "6",
USAGER_SURNOM: "TEST",
Expand Down Expand Up @@ -62,7 +62,7 @@ export const FIRST_SHEET_USAGERS = [
USAGER_NATIONALITE: "",
USAGER_NOM: "Martine",
USAGER_NUMERO_DISTRIBUTION_SPECIALE: "",
USAGER_PHONE: "+33 6 06 06 06 06",
USAGER_PHONE: "06 06 06 06 06",
USAGER_PRENOM: "Sembat",
USAGER_CUSTOM_REF: "5",
USAGER_SURNOM: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function ValidateSearchField(validationOptions?: ValidationOptions) {
options: validationOptions,
validator: {
validate(value: string, args: ValidationArguments) {
console.log({ value });
const searchStringField = args.object["searchStringField"];
if (!searchStringField) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/util/phone/phoneUtils.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Telephone utils", () => {

it("getPhoneString shoud return string with countryCode and numero", () => {
expect(getPhoneString({ countryCode: "fr", numero: "0622062206" })).toEqual(
"0622062206"
"06 22 06 22 06"
);
});
});
5 changes: 5 additions & 0 deletions packages/backend/src/util/test/AppTestHelper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ async function bootstrapTestApp(
context.app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
stopAtFirstError: true,
transform: true,
transformOptions: {
enableImplicitConversion: false,
},
})
);
await context.app.init();
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/search/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@index('./*.ts', f => `export * from '${f.path}'`)
export * from "./getUsagerDeadlines";
export * from "./normalize-string";
export * from "./parseBirthDate";
15 changes: 15 additions & 0 deletions packages/common/src/search/functions/parseBirthDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { format, isValid, parse } from "date-fns";

export const parseBirthDate = (text: string): string | null => {
const parsedDate = parse(text.trim(), "dd/MM/yyyy", new Date());
if (!isValid(parsedDate)) {
return null;
}
const minDate = new Date(1900, 0, 1);
const today = new Date();

if (parsedDate < minDate || parsedDate > today) {
return null;
}
return format(parsedDate, "ddMMyyyy");
};
71 changes: 71 additions & 0 deletions packages/common/src/search/functions/tests/parseBirthDate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { parseBirthDate } from "../parseBirthDate";

describe("parseBirthDate", () => {
describe("Valid Date Formats", () => {
it("should parse a valid date", () => {
const result = parseBirthDate("15/08/1990");
expect(result).toBe("15081990");
});

it("should handle initial zeros", () => {
const result = parseBirthDate("01/02/2000");
expect(result).toBe("01022000");
});
});

describe("Date Range Validation: 1900 < date > now", () => {
it("should return null for a date before 1900", () => {
const result = parseBirthDate("01/01/1899");
expect(result).toBeNull();
});

it("should return null for a future date", () => {
const result = parseBirthDate("01/01/2045");
expect(result).toBeNull();
});
});

describe("Invalid Date Formats", () => {
it("should return null for incorrect date format", () => {
const result = parseBirthDate("2023-01-15");
expect(result).toBeNull();
});

it("should return null for a non-existent date", () => {
const result = parseBirthDate("31/02/2000");
expect(result).toBeNull();
});

it("should return null for a non-numeric string", () => {
const result = parseBirthDate("abc/def/ghij");
expect(result).toBeNull();
});
});

// Spaces handling tests
describe("Spaces Handling", () => {
it("should remove spaces before and after the date", () => {
const result = parseBirthDate(" 15/08/1990 ");
expect(result).toBe("15081990");
});
});

// Boundary cases tests
describe("Boundary Cases", () => {
it("should accept the minimum allowed date", () => {
const result = parseBirthDate("01/01/1900");
expect(result).toBe("01011900");
});

it("should accept the maximum allowed date (today)", () => {
const today = new Date();
const formattedToday = `${today.getDate().toString().padStart(2, "0")}/${(
today.getMonth() + 1
)
.toString()
.padStart(2, "0")}/${today.getFullYear()}`;
const result = parseBirthDate(formattedToday);
expect(result).not.toBeNull();
});
});
});
Loading

0 comments on commit 6e683e3

Please sign in to comment.