-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3503 from incubateur-ademe/backend-domain-archite…
…cture Refacto de la partie indicateurs du backend pour éviter les duplicats dans le package api.
- Loading branch information
Showing
97 changed files
with
811 additions
and
913 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...ns.react/src/app/pages/collectivite/EtatDesLieux/Accueil/EtatDesLieux/IndicateursCard.tsx
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
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
105 changes: 0 additions & 105 deletions
105
backend/src/indicateurs/controllers/indicateurs.controller.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ltre/indicateur-filtre.router.e2e-spec.ts → ...ions/indicateur-filtre.router.e2e-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.
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
112 changes: 112 additions & 0 deletions
112
backend/src/indicateurs/definitions/list-definitions.service.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,112 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { | ||
aliasedTable, | ||
and, | ||
eq, | ||
getTableColumns, | ||
inArray, | ||
isNull, | ||
or, | ||
sql, | ||
} from 'drizzle-orm'; | ||
import { objectToCamel } from 'ts-case-convert'; | ||
import { groupementCollectiviteTable } from '../../collectivites/shared/models/groupement-collectivite.table'; | ||
import { groupementTable } from '../../collectivites/shared/models/groupement.table'; | ||
import { DatabaseService } from '../../utils/database/database.service'; | ||
import { | ||
IndicateurDefinition, | ||
IndicateurDefinitionAvecEnfantsType, | ||
indicateurDefinitionTable, | ||
} from '../shared/models/indicateur-definition.table'; | ||
import { indicateurGroupeTable } from '../shared/models/indicateur-groupe.table'; | ||
|
||
@Injectable() | ||
export default class ListDefinitionsService { | ||
private readonly logger = new Logger(ListDefinitionsService.name); | ||
|
||
constructor(private readonly databaseService: DatabaseService) {} | ||
|
||
async getReferentielIndicateurDefinitions(identifiantsReferentiel: string[]) { | ||
this.logger.log( | ||
`Récupération des définitions des indicateurs ${identifiantsReferentiel.join( | ||
',' | ||
)}` | ||
); | ||
const definitions = await this.databaseService.db | ||
.select() | ||
.from(indicateurDefinitionTable) | ||
.where( | ||
inArray( | ||
indicateurDefinitionTable.identifiantReferentiel, | ||
identifiantsReferentiel | ||
) | ||
); | ||
this.logger.log(`${definitions.length} définitions trouvées`); | ||
return definitions; | ||
} | ||
|
||
/** | ||
* Charge la définition des indicateurs à partir de leur id | ||
* ainsi que les définitions des indicateurs "enfant" associés. | ||
*/ | ||
async getIndicateurDefinitions( | ||
collectiviteId: number, | ||
indicateurIds: number[] | ||
): Promise<IndicateurDefinitionAvecEnfantsType[]> { | ||
this.logger.log( | ||
`Charge la définition des indicateurs ${indicateurIds.join(',')}` | ||
); | ||
|
||
const definitionEnfantsTable = aliasedTable( | ||
indicateurDefinitionTable, | ||
'enfants' | ||
); | ||
|
||
const definitions = await this.databaseService.db | ||
.select({ | ||
...getTableColumns(indicateurDefinitionTable), | ||
enfants: sql`json_agg(${definitionEnfantsTable})`, | ||
}) | ||
.from(indicateurDefinitionTable) | ||
.leftJoin( | ||
indicateurGroupeTable, | ||
eq(indicateurGroupeTable.parent, indicateurDefinitionTable.id) | ||
) | ||
.leftJoin( | ||
definitionEnfantsTable, | ||
eq(definitionEnfantsTable.id, indicateurGroupeTable.enfant) | ||
) | ||
.leftJoin( | ||
groupementTable, | ||
eq(groupementTable.id, definitionEnfantsTable.groupementId) | ||
) | ||
.leftJoin( | ||
groupementCollectiviteTable, | ||
eq(groupementCollectiviteTable.groupementId, groupementTable.id) | ||
) | ||
.where( | ||
and( | ||
inArray(indicateurDefinitionTable.id, indicateurIds), | ||
or( | ||
isNull(definitionEnfantsTable.groupementId), | ||
eq(groupementCollectiviteTable.collectiviteId, collectiviteId) | ||
) | ||
) | ||
) | ||
.groupBy(indicateurDefinitionTable.id); | ||
|
||
this.logger.log(`${definitions.length} définitions trouvées`); | ||
|
||
return definitions.map( | ||
(def: IndicateurDefinition & { enfants: unknown[] }) => { | ||
const enfants = def.enfants?.filter(Boolean); | ||
return { | ||
...def, | ||
enfants: enfants?.length | ||
? (objectToCamel(enfants) as IndicateurDefinition[]) | ||
: null, | ||
}; | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.