-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADMINISTRATION] Ajoute un service de recherche des comptes utilisateurs
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 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
99 changes: 99 additions & 0 deletions
99
mon-aide-cyber-api/src/administration/utilisateurs/recherche/commande.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,99 @@ | ||
import { program } from 'commander'; | ||
import { adaptateurServiceChiffrement } from '../../../infrastructure/adaptateurs/adaptateurServiceChiffrement'; | ||
import { | ||
DTO, | ||
EntrepotPostgres, | ||
} from '../../../infrastructure/entrepots/postgres/EntrepotPostgres'; | ||
import { Aggregat } from '../../../domaine/Aggregat'; | ||
import { ServiceDeChiffrement } from '../../../securite/ServiceDeChiffrement'; | ||
|
||
type CompteUtilisateur = Aggregat & { | ||
nomPrenom: string; | ||
email: string; | ||
}; | ||
|
||
type CompteUtilisateurDTO = DTO & { | ||
nom_prenom: string; | ||
mail: string; | ||
}; | ||
|
||
class EntrepotCompteUtilisateur extends EntrepotPostgres< | ||
CompteUtilisateur, | ||
CompteUtilisateurDTO | ||
> { | ||
constructor(private readonly serviceChiffrement: ServiceDeChiffrement) { | ||
super(); | ||
} | ||
|
||
async tous(): Promise<CompteUtilisateur[]> { | ||
return this.knex | ||
.raw( | ||
` | ||
SELECT id, donnees ->> 'nomPrenom' as nom_prenom, donnees ->> 'identifiantConnexion' as email | ||
FROM utilisateurs | ||
` | ||
) | ||
.then(({ rows }: { rows: CompteUtilisateurDTO[] }) => { | ||
return rows; | ||
}) | ||
.then((dtos) => { | ||
return dtos.map((dto) => this.deDTOAEntite(dto)); | ||
}); | ||
} | ||
|
||
protected champsAMettreAJour(_entiteDTO: DTO): Partial<DTO> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
protected nomTable(): string { | ||
return 'utilisateurs'; | ||
} | ||
|
||
protected deEntiteADTO(_entite: Aggregat): CompteUtilisateurDTO { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
protected deDTOAEntite(dto: CompteUtilisateurDTO): CompteUtilisateur { | ||
return { | ||
identifiant: dto.id, | ||
email: this.serviceChiffrement.dechiffre(dto.mail), | ||
nomPrenom: this.serviceChiffrement.dechiffre(dto.nom_prenom), | ||
}; | ||
} | ||
} | ||
|
||
program | ||
.description('Recherche les comptes utilisateurs') | ||
.option('-n, --nom <nom>', 'le nom ou partie du nom recherché') | ||
.option('-m, --mail <mail>', 'le mail ou partie du mail recherché') | ||
.action(async (options) => { | ||
console.log('Recherche des aidants en cours'); | ||
const utilisateurs = await new EntrepotCompteUtilisateur( | ||
adaptateurServiceChiffrement() | ||
).tous(); | ||
|
||
const utilisateursTrouves = utilisateurs.filter((utilisateur) => { | ||
if (options.nom) { | ||
return utilisateur.nomPrenom | ||
.toLowerCase() | ||
.trim() | ||
.includes(options.nom.toLowerCase().trim()); | ||
} | ||
if (options.mail) { | ||
return utilisateur.email | ||
.toLowerCase() | ||
.trim() | ||
.includes(options.mail.toLowerCase().trim()); | ||
} | ||
return true; | ||
}); | ||
console.log( | ||
`Il y a %s utilisateur(s) et %s trouvé(s)`, | ||
utilisateurs.length, | ||
utilisateursTrouves.length | ||
); | ||
console.log(JSON.stringify(utilisateursTrouves, undefined, 2)); | ||
process.exit(0); | ||
}); | ||
|
||
program.parse(); |