Skip to content

Commit

Permalink
feat(fo-organismes): new tab for etablissements secondaires
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM committed Jan 23, 2025
1 parent 3c1821d commit e199435
Show file tree
Hide file tree
Showing 18 changed files with 634 additions and 326 deletions.
1 change: 1 addition & 0 deletions packages/backend/src/helpers/org-part.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports.partOrganisme = {
ETABLISSEMENTS_SECONDAIRES: "etablissements_secondaires",
PERSONNE_MORALE: "personne_morale",
PERSONNE_PHYSIQUE: "personne_physique",
PROTOCOLE_SANITAIRE: "protocole_sanitaire",
Expand Down
56 changes: 54 additions & 2 deletions packages/backend/src/services/Organisme.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const PersonnePhysique = require("./organisme/PersonnePhysique");
const PersonneMorale = require("./organisme/PersonneMorale");
const ProtocoleSanitaire = require("./organisme/ProtocoleSanitaire");
const ProtocoleTransport = require("./organisme/ProtocoleTransport");
const EtablissementsSecondaires = require("./organisme/EtablissementsSecondaires");

const log = logger(module.filename);

Expand Down Expand Up @@ -433,14 +434,39 @@ FROM back.organisme_non_agree ona
complet = complet AND $3,
edited_at = NOW()
WHERE id = $1
`,
// TODO : A SUPPRIMER APRES lE REFACTO ORGANNISME :
// Utilisé uniquement pour mettre a jour les json personnes_morale
// lors de l'ajout des etablissement secondaire.
getPersonneMorale: `
SELECT
personne_morale AS "personneMorale"
FROM
front.organismes
Where id = $1
`,
// TODO : A SUPPRIMER APRES lE REFACTO ORGANNISME :
// Utilisé uniquement pour mettre a jour les json personnes_morale
// lors de l'ajout des etablissement secondaire.
updatePersonneMorale: `
UPDATE front.organismes
SET
personne_morale = $2,
complet = complet AND $3,
edited_at = NOW()
WHERE id = $1
`,
};

module.exports.create = async (type, parametre) => {
log.i("create - IN", { type });
const response =
type === partOrganisme.PERSONNE_MORALE
? await pool.query(query.create, [type, parametre, {}])
? await pool.query(query.create, [
type,
{ ...parametre, etablissements: [] },
{},
])
: await pool.query(query.create, [type, {}, parametre]);
const { organismeId } = response && response.rows[0];
const responseNew =
Expand Down Expand Up @@ -475,12 +501,20 @@ module.exports.update = async (type, parametre, organismeId) => {

switch (type) {
case partOrganisme.PERSONNE_MORALE: {
const { rows } = await pool.query(query.getPersonneMorale, [organismeId]);
const etablissementsSecondaires =
rows[0].personneMorale.etablissements ?? [];
const parametreWithPersonneMorale = {
...parametre,
etablissements: etablissementsSecondaires,
};

const complet =
await Organisme.schema(regions).personneMorale.isValid(parametre);
response = await pool.query(query.updatePersonne, [
organismeId,
type,
parametre,
parametreWithPersonneMorale,
{},
complet,
]);
Expand All @@ -500,6 +534,24 @@ module.exports.update = async (type, parametre, organismeId) => {
await PersonnePhysique.createOrUpdate(organismeId, parametre);
break;
}
case partOrganisme.ETABLISSEMENTS_SECONDAIRES: {
const { rows } = await pool.query(query.getPersonneMorale, [organismeId]);
const personneMorale = rows[0].personneMorale ?? {};
const parametreWithPersonneMorale = {
...personneMorale,
etablissements: parametre.etablissements,
};
const complet =
await Organisme.schema(regions).personneMorale.isValid(parametre);

response = await pool.query(query.updatePersonneMorale, [
organismeId,
parametreWithPersonneMorale,
complet,
]);
await EtablissementsSecondaires.createOrUpdate(organismeId, parametre);
break;
}
case partOrganisme.PROTOCOLE_TRANSPORT: {
const complet =
await Organisme.schema(regions).protocoleTransport.isValid(parametre);
Expand Down
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");
};
48 changes: 1 addition & 47 deletions packages/backend/src/services/organisme/PersonneMorale.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,9 @@ 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}
`,
associateRepresentantsLegaux: (valueParams) => `
INSERT INTO front.opm_representants_legaux (
personne_morale_id,
personne_morale_id,
prenom,
nom,
fonction
Expand Down Expand Up @@ -72,11 +58,6 @@ const query = {
FROM front.personne_morale
WHERE organisme_id = $1
`,
removeEtablissements: `
DELETE FROM front.opm_etablissements
WHERE
personne_morale_id = $1;
`,
removeRepresentantsLegaux: `
DELETE FROM front.opm_representants_legaux
WHERE
Expand Down Expand Up @@ -210,33 +191,6 @@ module.exports.createOrUpdate = async (organismeId, parametre) => {
parametre?.etablissementPrincipal?.email ?? null,
]);

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(query.removeRepresentantsLegaux, [personneMoraleId]);
const representantsLegaux = parametre.representantsLegaux;

Expand Down
2 changes: 0 additions & 2 deletions packages/frontend-bo/src/components/demandes-sejour/eigs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ const openModal = async (index) => {
return;
}
console.log(eig);
if (utilsEig.mustMarkAsRead(eig, userStore.user)) {
expandedIndex.value = -1;
modalDetails.value = { eigId: eig.id, index };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
:show-buttons="false"
:modifiable="false"
></OrganismePersonneMorale>
<OrganismeEtablissementsSecondaires
v-if="organismeStore.isSiegeSocial"
:modifiable="false"
:show-buttons="false"
/>
</div>

<div v-if="props.showButtons" class="fr-fieldset">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

<script setup>
const props = defineProps({
activeId: { type: String, default: organismeMenus[0].id, required: false },
activeId: {
type: String,
default: demandeSejourMenus[0].id,
required: false,
},
demande: { type: Object, default: () => ({}) },
});
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-usagers/src/components/DS/stepper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</template>
<script setup>
const props = defineProps({
step: { type: String, default: organismeMenus[0].id, required: false },
step: { type: String, default: demandeSejourMenus[0].id, required: false },
});
const demandeSejourStore = useDemandeSejourStore();
Expand Down
Loading

0 comments on commit e199435

Please sign in to comment.