-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: recupere liste beneficiaires a archiver (#1642)
- Loading branch information
1 parent
087be90
commit d89626e
Showing
15 changed files
with
434 additions
and
26 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
44 changes: 44 additions & 0 deletions
44
src/application/queries/get-beneficiaires-a-archiver.query.handler.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,44 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { ConseillerAuthorizer } from 'src/application/authorizers/conseiller-authorizer' | ||
import { GetBeneficiairesAArchiverQueryGetter } from 'src/application/queries/query-getters/get-beneficiaires-a-archiver.query.getter.db' | ||
import { IdentiteJeuneQueryModel } from 'src/application/queries/query-models/jeunes.query-model' | ||
import { QueryHandler } from 'src/building-blocks/types/query-handler' | ||
import { Result } from 'src/building-blocks/types/result' | ||
import { Authentification } from 'src/domain/authentification' | ||
|
||
type GetBeneficiairesAArchiverQuery = { | ||
idConseiller: string | ||
} | ||
|
||
@Injectable() | ||
export class GetBeneficiairesAArchiverQueryHandler extends QueryHandler< | ||
GetBeneficiairesAArchiverQuery, | ||
Result<IdentiteJeuneQueryModel[]> | ||
> { | ||
constructor( | ||
private readonly conseillerAuthorizer: ConseillerAuthorizer, | ||
private readonly queryGetter: GetBeneficiairesAArchiverQueryGetter | ||
) { | ||
super('GetBeneficiairesAArchiverQueryHandler') | ||
} | ||
|
||
async authorize( | ||
query: GetBeneficiairesAArchiverQuery, | ||
utilisateur: Authentification.Utilisateur | ||
): Promise<Result> { | ||
return this.conseillerAuthorizer.autoriserLeConseiller( | ||
query.idConseiller, | ||
utilisateur | ||
) | ||
} | ||
|
||
async handle( | ||
query: GetBeneficiairesAArchiverQuery | ||
): Promise<Result<IdentiteJeuneQueryModel[]>> { | ||
return this.queryGetter.handle(query.idConseiller) | ||
} | ||
|
||
async monitor(): Promise<void> { | ||
return | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/application/queries/query-getters/get-beneficiaires-a-archiver.query.getter.db.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,57 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { QueryTypes, Sequelize } from 'sequelize' | ||
import { IdentiteJeuneQueryModel } from 'src/application/queries/query-models/jeunes.query-model' | ||
import { Result, success } from 'src/building-blocks/types/result' | ||
import { SequelizeInjectionToken } from 'src/infrastructure/sequelize/providers' | ||
import { DateService } from 'src/utils/date-service' | ||
|
||
@Injectable() | ||
export class GetBeneficiairesAArchiverQueryGetter { | ||
constructor( | ||
private readonly dateService: DateService, | ||
@Inject(SequelizeInjectionToken) private readonly sequelize: Sequelize | ||
) {} | ||
|
||
async handle( | ||
idConseiller: string | ||
): Promise<Result<IdentiteJeuneQueryModel[]>> { | ||
const models = | ||
await this.queryBeneficiairesAArchiver<IdentiteJeuneQueryModel>( | ||
'id, nom, prenom', | ||
idConseiller | ||
) | ||
return success(models) | ||
} | ||
|
||
async count(idConseiller: string): Promise<number> { | ||
const rows = await this.queryBeneficiairesAArchiver<{ count: string }>( | ||
'COUNT(id)', | ||
idConseiller | ||
) | ||
|
||
return Number.parseInt(rows[0].count) | ||
} | ||
|
||
private async queryBeneficiairesAArchiver<T extends object>( | ||
query: string, | ||
idConseiller: string | ||
): Promise<T[]> { | ||
const ilYa6mois = this.dateService.now().minus({ month: 6 }).startOf('day') | ||
|
||
return this.sequelize.query<T>( | ||
` | ||
SELECT ${query} FROM jeune | ||
WHERE id_conseiller = :idConseiller | ||
AND ( | ||
date_fin_cej < :date | ||
OR date_derniere_actualisation_token < :date | ||
OR (EXISTS (SELECT 1 FROM jeune_milo_a_archiver WHERE jeune_milo_a_archiver.id_jeune = jeune.id)) IS true | ||
) | ||
`, | ||
{ | ||
type: QueryTypes.SELECT, | ||
replacements: { idConseiller, date: ilYa6mois.toJSDate() } | ||
} | ||
) | ||
} | ||
} |
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
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
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
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
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
60 changes: 60 additions & 0 deletions
60
test/application/queries/get-beneficiaires-a-archiver.query.handler.test.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,60 @@ | ||
import { ConseillerAuthorizer } from 'src/application/authorizers/conseiller-authorizer' | ||
import { GetBeneficiairesAArchiverQueryHandler } from 'src/application/queries/get-beneficiaires-a-archiver.query.handler' | ||
import { GetBeneficiairesAArchiverQueryGetter } from 'src/application/queries/query-getters/get-beneficiaires-a-archiver.query.getter.db' | ||
import { success } from 'src/building-blocks/types/result' | ||
import { unUtilisateurConseiller } from 'test/fixtures/authentification.fixture' | ||
import { expect, StubbedClass, stubClass } from 'test/utils' | ||
|
||
describe('GetBeneficiaireAArchiverQueryHandler', () => { | ||
let conseillerAuthorizer: ConseillerAuthorizer | ||
let queryGetter: StubbedClass<GetBeneficiairesAArchiverQueryGetter> | ||
let queryHandler: GetBeneficiairesAArchiverQueryHandler | ||
beforeEach(async () => { | ||
conseillerAuthorizer = stubClass(ConseillerAuthorizer) | ||
queryGetter = stubClass(GetBeneficiairesAArchiverQueryGetter) | ||
queryHandler = new GetBeneficiairesAArchiverQueryHandler( | ||
conseillerAuthorizer, | ||
queryGetter | ||
) | ||
}) | ||
|
||
describe('handle', () => { | ||
it('recupère les bénéficiaires à archiver', async () => { | ||
// Given | ||
queryGetter.handle.resolves( | ||
success([{ id: 'id-jeune-3', nom: 'Liskov', prenom: 'Barbara' }]) | ||
) | ||
|
||
// When | ||
const actual = await queryHandler.handle({ | ||
idConseiller: 'id-conseiller' | ||
}) | ||
|
||
// Then | ||
expect(queryGetter.handle).to.have.been.calledOnceWithExactly( | ||
'id-conseiller' | ||
) | ||
expect(actual).to.deep.equal( | ||
success([{ id: 'id-jeune-3', nom: 'Liskov', prenom: 'Barbara' }]) | ||
) | ||
}) | ||
}) | ||
|
||
describe('authorize', () => { | ||
it('appelle l’authorizer pour le conseiller', async () => { | ||
// Given | ||
const utilisateur = unUtilisateurConseiller() | ||
|
||
// When | ||
await queryHandler.authorize( | ||
{ idConseiller: 'id-conseiller' }, | ||
utilisateur | ||
) | ||
|
||
// Then | ||
expect( | ||
conseillerAuthorizer.autoriserLeConseiller | ||
).to.have.been.calledWithExactly('id-conseiller', utilisateur) | ||
}) | ||
}) | ||
}) |
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.