-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): add validators to search
- Loading branch information
Showing
33 changed files
with
551 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,5 @@ | ||
//@index('./*.ts', f => `export * from '${f.path}'`) | ||
export * from "./IsValidPasswordDecorator"; | ||
export * from "./IsValidPhoneDecorator"; | ||
export * from "./LowerCaseDecorator"; | ||
export * from "./parse-hard-reset-token.pipe"; | ||
export * from "./parse-token.pipe"; | ||
export * from "./ParseRegion.pipe"; | ||
export * from "./ParseString.pipe"; | ||
export * from "./PhoneTransformDecorator"; | ||
export * from "./StripTagsDecorator"; | ||
export * from "./TrimDecorator"; | ||
export * from "./TrimOrNullDecorator"; | ||
export * from "./UpperCaseDecorator"; | ||
export * from "./transformers"; | ||
export * from "./parse-pipes"; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//@index('./*.ts', f => `export * from '${f.path}'`) | ||
export * from "./parse-hard-reset-token.pipe"; | ||
export * from "./parse-token.pipe"; | ||
export * from "./ParseRegion.pipe"; | ||
export * from "./ParseString.pipe"; |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/backend/src/_common/decorators/tests/parse-hard-reset-token-pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/backend/src/_common/decorators/tests/parse-token-pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
packages/backend/src/_common/decorators/transformers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//@index('./*.ts', f => `export * from '${f.path}'`) | ||
export * from "./LowerCaseDecorator"; | ||
export * from "./PhoneTransformDecorator"; | ||
export * from "./StripTagsDecorator"; | ||
export * from "./TrimDecorator"; | ||
export * from "./TrimOrNullDecorator"; | ||
export * from "./UpperCaseDecorator"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
packages/backend/src/usagers/controllers/search-usagers.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import { | ||
Usager, | ||
UsagerDecision, | ||
CriteriaSearchField, | ||
getUsagerDeadlines, | ||
ETAPE_ENTRETIEN, | ||
} from "@domifa/common"; | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
ParseBoolPipe, | ||
Post, | ||
Query, | ||
UseGuards, | ||
} from "@nestjs/common"; | ||
import { AuthGuard } from "@nestjs/passport"; | ||
import { ApiBearerAuth } from "@nestjs/swagger"; | ||
import { format, parse, subMinutes } from "date-fns"; | ||
import { Not } from "typeorm"; | ||
import { | ||
USER_STRUCTURE_ROLE_ALL, | ||
UserStructureAuthenticated, | ||
} from "../../_common/model"; | ||
import { AllowUserStructureRoles, CurrentUser } from "../../auth/decorators"; | ||
import { AppUserGuard } from "../../auth/guards"; | ||
import { | ||
usagerRepository, | ||
USAGER_LIGHT_ATTRIBUTES, | ||
joinSelectFields, | ||
} from "../../database"; | ||
|
||
import { SearchUsagerDto } from "../dto"; | ||
|
||
@Controller("search-usagers") | ||
@UseGuards(AuthGuard("jwt"), AppUserGuard) | ||
@ApiBearerAuth() | ||
export class SearchUsagersController { | ||
@Get() | ||
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL) | ||
public async findAllByStructure( | ||
@Query("chargerTousRadies", new ParseBoolPipe()) | ||
chargerTousRadies: boolean, | ||
@CurrentUser() user: UserStructureAuthenticated | ||
) { | ||
const usagersNonRadies = await usagerRepository.find({ | ||
where: { | ||
statut: Not("RADIE"), | ||
structureId: user.structureId, | ||
}, | ||
select: USAGER_LIGHT_ATTRIBUTES, | ||
}); | ||
|
||
const usagersRadiesFirsts = await usagerRepository.find({ | ||
where: { | ||
statut: "RADIE", | ||
structureId: user.structureId, | ||
}, | ||
select: USAGER_LIGHT_ATTRIBUTES, | ||
take: chargerTousRadies ? undefined : 1600, | ||
}); | ||
|
||
const usagersRadiesTotalCount = chargerTousRadies | ||
? usagersRadiesFirsts.length | ||
: await usagerRepository.count({ | ||
where: { | ||
statut: "RADIE", | ||
structureId: user.structureId, | ||
}, | ||
}); | ||
|
||
const filterHistorique = (usager: Usager) => { | ||
if (usager.historique && Array.isArray(usager.historique)) { | ||
usager.historique = usager.historique.map((item: UsagerDecision) => ({ | ||
statut: item.statut, | ||
dateDecision: item.dateDecision, | ||
dateDebut: item.dateDebut, | ||
dateFin: item.dateFin, | ||
})) as UsagerDecision[]; | ||
} | ||
return usager; | ||
}; | ||
|
||
const usagersMerges = [...usagersNonRadies, ...usagersRadiesFirsts].map( | ||
filterHistorique | ||
); | ||
|
||
return { | ||
usagersRadiesTotalCount, | ||
usagers: usagersMerges, | ||
}; | ||
} | ||
|
||
@Get("update-manage") | ||
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL) | ||
public async updateManage(@CurrentUser() user: UserStructureAuthenticated) { | ||
return await usagerRepository | ||
.createQueryBuilder() | ||
.select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES)) | ||
.where( | ||
`"structureId" = :structureId AND "updatedAt" >= :fiveMinutesAgo`, | ||
{ | ||
structureId: user.structureId, | ||
fiveMinutesAgo: subMinutes(new Date(), 5), | ||
} | ||
) | ||
.getRawMany(); | ||
} | ||
|
||
@Post("search-radies") | ||
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL) | ||
public async searchInRadies( | ||
@Body() search: SearchUsagerDto, | ||
@CurrentUser() user: UserStructureAuthenticated | ||
) { | ||
const query = usagerRepository | ||
.createQueryBuilder("usager") | ||
.select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES)) | ||
.where(`"structureId" = :structureId and statut = 'RADIE'`, { | ||
structureId: user.structureId, | ||
}); | ||
|
||
if (search.searchString) { | ||
if (search.searchStringField === CriteriaSearchField.DEFAULT) { | ||
query.andWhere("nom_prenom_surnom_ref ILIKE :str", { | ||
str: `%${search.searchString}%`, | ||
}); | ||
} else if (search.searchStringField === CriteriaSearchField.BIRTH_DATE) { | ||
const formattedDate = format( | ||
parse(search.searchString, "ddMMyyyy", new Date()), | ||
"yyyy-MM-dd" | ||
); | ||
query.andWhere(`DATE("dateNaissance") = DATE(:date)`, { | ||
date: formattedDate, | ||
}); | ||
} else if ( | ||
search.searchStringField === CriteriaSearchField.PHONE_NUMBER | ||
) { | ||
query.andWhere(`telephone->>'numero' ILIKE :phone`, { | ||
phone: `%${search.searchString}%`, | ||
}); | ||
} | ||
} | ||
|
||
if (search?.lastInteractionDate) { | ||
const deadlines = getUsagerDeadlines(); | ||
const date = deadlines[search.lastInteractionDate].value; | ||
|
||
query.andWhere( | ||
`("lastInteraction"->>'dateInteraction')::timestamp >= :dateRef::timestamp`, | ||
{ | ||
dateRef: date, | ||
} | ||
); | ||
} | ||
|
||
if (typeof search?.referrerId !== "undefined") { | ||
query.andWhere( | ||
search.referrerId === null | ||
? `"referrerId" IS NULL` | ||
: `"referrerId" = :referrerId`, | ||
{ referrerId: search.referrerId } | ||
); | ||
} | ||
|
||
if (search?.entretien) { | ||
query.andWhere( | ||
`rdv->>'dateRdv' IS NOT NULL AND "etapeDemande" <= :step AND (rdv->>'dateRdv')::date ${ | ||
search.entretien === "COMING" ? ">" : "<" | ||
} CURRENT_DATE`, | ||
{ step: ETAPE_ENTRETIEN } | ||
); | ||
} | ||
|
||
if (search?.echeance) { | ||
const deadlines = getUsagerDeadlines(); | ||
const now = new Date(); | ||
const deadline = deadlines[search.echeance]; | ||
|
||
if (search.echeance === "EXCEEDED") { | ||
query.andWhere(`(decision->>'dateDecision')::timestamp < :now`, { | ||
now, | ||
}); | ||
} else if (search.echeance.startsWith("NEXT_")) { | ||
query.andWhere( | ||
`(decision->>'dateDecision')::timestamp <= :deadline AND (decision->>'dateDecision')::timestamp > :now`, | ||
{ | ||
deadline: deadline.value, | ||
now, | ||
} | ||
); | ||
} else if (search?.echeance.startsWith("PREVIOUS_")) { | ||
query.andWhere(`(decision->>'dateDecision')::timestamp < :deadline`, { | ||
deadline: deadline.value, | ||
now, | ||
}); | ||
} | ||
} | ||
|
||
if ( | ||
!search.searchString && | ||
!search?.echeance && | ||
!search?.entretien && | ||
typeof search?.referrerId !== undefined && | ||
!search?.lastInteractionDate | ||
) { | ||
query.take(100); | ||
} | ||
|
||
return await query.getRawMany(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ackend/src/usagers/controllers/security-tests/search-usagers.controller.security-tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { AppTestContext, AppTestHttpClient } from "../../../util/test"; | ||
import { USER_STRUCTURE_ROLE_ALL } from "../../../_common/model"; | ||
import { | ||
AppTestHttpClientSecurityTestDef, | ||
expectedResponseStatusBuilder, | ||
} from "../../../_tests"; | ||
|
||
////////////////// IMPORTANT ////////////////// | ||
// | ||
// Ce fichier doit être importé dans : | ||
// - API_SECURITY_STRUCTURE_CONTROLLER_TEST_DEFS | ||
// | ||
|
||
const CONTROLLER = "SearchUsagersController"; | ||
|
||
export const UsagersControllerSecurityTests: AppTestHttpClientSecurityTestDef[] = | ||
[ | ||
{ | ||
label: `${CONTROLLER}.findAllByStructure`, | ||
query: async (context: AppTestContext) => ({ | ||
response: await AppTestHttpClient.get( | ||
"/usagers?chargerTousRadies=false", | ||
{ | ||
context, | ||
} | ||
), | ||
expectedStatus: expectedResponseStatusBuilder.allowStructureOnly( | ||
context.user, | ||
{ | ||
roles: USER_STRUCTURE_ROLE_ALL, | ||
} | ||
), | ||
}), | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.