-
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.
feat(indicateurs): calcul de la trajectoire SNBC
- Loading branch information
Showing
17 changed files
with
550 additions
and
124 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { SheetModule } from './spreadsheets/sheet.module'; | ||
import { CommonModule } from './common/common.module'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { CollectivitesModule } from './collectivites/collectivites.module'; | ||
import { CommonModule } from './common/common.module'; | ||
import { IndicateursModule } from './indicateurs/indicateurs.module'; | ||
import { SheetModule } from './spreadsheets/sheet.module'; | ||
|
||
@Module({ | ||
imports: [CommonModule, SheetModule, CollectivitesModule], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
ignoreEnvFile: process.env.NODE_ENV === 'production', // In production, environment variables are set by the deployment | ||
}), | ||
CommonModule, | ||
SheetModule, | ||
CollectivitesModule, | ||
IndicateursModule, | ||
], | ||
controllers: [], | ||
providers: [], | ||
}) | ||
export class AppModule {} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const optionalBooleanMapper = new Map([ | ||
['undefined', undefined], | ||
['true', true], | ||
['false', false], | ||
]); | ||
|
||
export default optionalBooleanMapper; |
18 changes: 18 additions & 0 deletions
18
backend/src/indicateurs/controllers/trajectoires.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,18 @@ | ||
import { Controller, Get, Logger, Query } from '@nestjs/common'; | ||
import CalculTrajectoireRequest from '../models/calcultrajectoire.request'; | ||
import TrajectoiresService from '../service/trajectoires.service'; | ||
|
||
@Controller('trajectoires') | ||
export class TrajectoiresController { | ||
private readonly logger = new Logger(TrajectoiresController.name); | ||
|
||
constructor(private readonly trajectoiresService: TrajectoiresService) {} | ||
|
||
@Get('snbc') // TODO: plutôt un post | ||
calculeTrajectoireSnbc(@Query() request: CalculTrajectoireRequest) { | ||
this.logger.log( | ||
`Calcul de la trajectoire SNBC pour la collectivité ${request.collectivite_id}`, | ||
); | ||
return this.trajectoiresService.calculeTrajectoireSnbc(request); | ||
} | ||
} |
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,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CollectivitesModule } from '../collectivites/collectivites.module'; | ||
import { CommonModule } from '../common/common.module'; | ||
import { SheetModule } from '../spreadsheets/sheet.module'; | ||
import { TrajectoiresController } from './controllers/trajectoires.controller'; | ||
import IndicateursService from './service/indicateurs.service'; | ||
import TrajectoiresService from './service/trajectoires.service'; | ||
|
||
@Module({ | ||
imports: [CommonModule, CollectivitesModule, SheetModule], | ||
providers: [IndicateursService, TrajectoiresService], | ||
exports: [IndicateursService, TrajectoiresService], | ||
controllers: [TrajectoiresController], | ||
}) | ||
export class IndicateursModule {} |
14 changes: 14 additions & 0 deletions
14
backend/src/indicateurs/models/calcultrajectoire.request.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,14 @@ | ||
import { Transform, Type } from 'class-transformer'; | ||
import { IsBoolean, IsInt, IsOptional } from 'class-validator'; | ||
import optionalBooleanMapper from '../../common/services/optionalBooleanMapper'; | ||
|
||
export default class CalculTrajectoireRequest { | ||
@IsInt() | ||
@Type(() => Number) | ||
collectivite_id: number; | ||
|
||
@IsBoolean() | ||
@Transform(({ value }) => optionalBooleanMapper.get(value)) // Useful for query param | ||
@IsOptional() | ||
conserve_fichier_temporaire?: boolean; | ||
} |
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,125 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { and, eq, gte, inArray, lte, sql, SQLWrapper } from 'drizzle-orm'; | ||
import { | ||
boolean, | ||
date, | ||
doublePrecision, | ||
integer, | ||
pgTable, | ||
serial, | ||
text, | ||
timestamp, | ||
uuid, | ||
} from 'drizzle-orm/pg-core'; | ||
import CollectivitesService from '../../collectivites/services/collectivites.service'; | ||
import DatabaseService from '../../common/services/database.service'; | ||
|
||
@Injectable() | ||
export default class IndicateursService { | ||
private readonly logger = new Logger(IndicateursService.name); | ||
public readonly indicateurDefinitionTable; | ||
public readonly indicateurValeurTable; | ||
|
||
constructor( | ||
private readonly databaseService: DatabaseService, | ||
private readonly collectivitesService: CollectivitesService, | ||
) { | ||
this.indicateurDefinitionTable = pgTable('indicateur_definition', { | ||
id: serial('id').primaryKey(), | ||
groupement_id: integer('groupement_id'), // TODO: references | ||
collectivite_id: integer('collectivite_id') | ||
.notNull() | ||
.references(() => this.collectivitesService.collectiviteTable.id, { | ||
onDelete: 'cascade', | ||
}), | ||
identifiant_referentiel: text('identifiant_referentiel').unique(), | ||
titre: text('titre').notNull(), | ||
titre_long: text('titre_long'), | ||
description: text('description'), | ||
unite: text('unite').notNull(), | ||
borne_min: doublePrecision('borne_min'), | ||
borne_max: doublePrecision('borne_max'), | ||
participation_score: boolean('participation_score') | ||
.default(false) | ||
.notNull(), | ||
sans_valeur_utilisateur: boolean('sans_valeur_utilisateur') | ||
.default(false) | ||
.notNull(), | ||
valeur_calcule: text('valeur_calcule'), | ||
modified_at: timestamp('modified_at', { withTimezone: true }) | ||
.default(sql.raw(`CURRENT_TIMESTAMP`)) | ||
.notNull(), // with time zone default CURRENT_TIMESTAMP | ||
created_at: timestamp('created_at', { withTimezone: true }) | ||
.default(sql.raw(`CURRENT_TIMESTAMP`)) | ||
.notNull(), // with time zone default CURRENT_TIMESTAMP | ||
modified_by: uuid('modified_by'), // TODO: default auth.uid() references auth.users | ||
created_by: uuid('created_by'), // TODO: default auth.uid() references auth.users | ||
}); | ||
|
||
this.indicateurValeurTable = pgTable('indicateur_valeur', { | ||
id: serial('id').primaryKey(), | ||
collectivite_id: integer('collectivite_id') | ||
.notNull() | ||
.references(() => this.collectivitesService.collectiviteTable.id, { | ||
onDelete: 'cascade', | ||
}), | ||
indicateur_id: integer('indicateur_id') | ||
.notNull() | ||
.references(() => this.indicateurDefinitionTable.id, { | ||
onDelete: 'cascade', | ||
}), | ||
date_valeur: date('date_valeur').notNull(), | ||
metadonnee_id: integer('metadonnee_id'), // TODO: references | ||
resultat: doublePrecision('resultat'), | ||
resultat_commentaire: text('resultat_commentaire'), | ||
objectif: doublePrecision('objectif'), | ||
objectif_commentaire: text('objectif_commentaire'), | ||
estimation: doublePrecision('estimation'), | ||
modified_at: timestamp('modified_at', { withTimezone: true }) | ||
.default(sql.raw(`CURRENT_TIMESTAMP`)) | ||
.notNull(), // with time zone default CURRENT_TIMESTAMP | ||
created_at: timestamp('created_at', { withTimezone: true }) | ||
.default(sql.raw(`CURRENT_TIMESTAMP`)) | ||
.notNull(), // with time zone default CURRENT_TIMESTAMP | ||
modified_by: uuid('modified_by'), // TODO: default auth.uid() references auth.users | ||
created_by: uuid('created_by'), // TODO: default auth.uid() references auth.users | ||
}); | ||
} | ||
|
||
async getReferentielIndicateursValeurs( | ||
collectiviteId: number, | ||
identifiantsReferentiel: string[], | ||
dateDebut?: string, | ||
dateFin?: string, | ||
) { | ||
this.logger.log( | ||
`Récupération des valeurs des indicateurs ${identifiantsReferentiel.join(',')} pour la collectivite ${collectiviteId} et la plage de date ${dateDebut} - ${dateFin}`, | ||
); | ||
const conditions: SQLWrapper[] = [ | ||
eq(this.indicateurValeurTable.collectivite_id, collectiviteId), | ||
inArray( | ||
this.indicateurDefinitionTable.identifiant_referentiel, | ||
identifiantsReferentiel, | ||
), | ||
]; | ||
|
||
if (dateDebut) { | ||
conditions.push(gte(this.indicateurValeurTable.date_valeur, dateDebut)); | ||
} | ||
if (dateFin) { | ||
conditions.push(lte(this.indicateurValeurTable.date_valeur, dateFin)); | ||
} | ||
|
||
return this.databaseService.db | ||
.select() | ||
.from(this.indicateurValeurTable) | ||
.leftJoin( | ||
this.indicateurDefinitionTable, | ||
eq( | ||
this.indicateurValeurTable.indicateur_id, | ||
this.indicateurDefinitionTable.id, | ||
), | ||
) | ||
.where(and(...conditions)); | ||
} | ||
} |
Oops, something went wrong.