-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fo-organismes): new tab for etablissements secondaires
- Loading branch information
1 parent
3c1821d
commit e199435
Showing
18 changed files
with
634 additions
and
326 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
85 changes: 85 additions & 0 deletions
85
packages/backend/src/services/organisme/EtablissementsSecondaires.js
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,85 @@ | ||
const logger = require("../../utils/logger"); | ||
|
||
const pool = require("../../utils/pgpool").getPool(); | ||
|
||
const log = logger(module.filename); | ||
|
||
const query = { | ||
associateEtablissement: (valueParams) => ` | ||
INSERT INTO front.opm_etablissements ( | ||
personne_morale_id, | ||
nic, | ||
siret, | ||
adresse, | ||
commune, | ||
enabled, | ||
code_postal, | ||
denomination, | ||
etat_administratif | ||
) | ||
VALUES ${valueParams} | ||
`, | ||
getPersonneMoraleId: ` | ||
SELECT | ||
id AS "personneMoraleId" | ||
FROM | ||
front.personne_morale | ||
WHERE | ||
organisme_id = $1; | ||
`, | ||
removeEtablissements: ` | ||
DELETE FROM front.opm_etablissements | ||
WHERE | ||
personne_morale_id = $1; | ||
`, | ||
}; | ||
|
||
module.exports.createOrUpdate = async (organismeId, parametre) => { | ||
log.i("createOrUpdate - IN"); | ||
|
||
const client = await pool.connect(); | ||
try { | ||
await client.query("BEGIN"); | ||
|
||
const { rows } = await client.query(query.getPersonneMoraleId, [ | ||
organismeId, | ||
]); | ||
|
||
const personneMoraleId = rows[0].personneMoraleId; | ||
|
||
await client.query(query.removeEtablissements, [personneMoraleId]); | ||
const etablissements = parametre.etablissements; | ||
|
||
const valuesEtablissement = etablissements.flatMap((etablissement) => [ | ||
personneMoraleId, | ||
etablissement?.nic ?? null, | ||
etablissement?.siret ?? null, | ||
etablissement?.adresse ?? null, | ||
etablissement?.commune ?? null, | ||
etablissement?.enabled ?? null, | ||
etablissement?.codePostal ?? null, | ||
etablissement?.denomination ?? null, | ||
etablissement?.etatAdministratif ?? null, | ||
]); | ||
|
||
const valuesParamsEtab = etablissements | ||
.map( | ||
(_, index) => | ||
`($${index * 9 + 1}, $${index * 9 + 2}, $${index * 9 + 3}, $${index * 9 + 4}, $${index * 9 + 5}, $${index * 9 + 6}, $${index * 9 + 7}, $${index * 9 + 8}, $${index * 9 + 9})`, | ||
) | ||
.join(", "); | ||
|
||
await client.query( | ||
query.associateEtablissement(valuesParamsEtab), | ||
valuesEtablissement, | ||
); | ||
await client.query("COMMIT"); | ||
} catch (error) { | ||
await client.query("ROLLBACK"); | ||
throw error; | ||
} finally { | ||
client.release(); | ||
} | ||
|
||
log.i("createOrUpdate - DONE"); | ||
}; |
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
Oops, something went wrong.