Skip to content

Commit

Permalink
fix(hebergement): refacto getters
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM committed Nov 19, 2024
1 parent 078537f commit ad40a56
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/demandeSejour/depose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const dayjs = require("dayjs");
const yup = require("yup");

const DemandeSejour = require("../../services/DemandeSejour");
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const Send = require("../../services/mail").mailService.send;
const PdfDeclaration2Mois = require("../../services/pdf/declaration2mois/generate");
const PdfDeclaration8jours = require("../../services/pdf/declaration8jours/generate");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");

const logger = require("../../utils/logger");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");

const logger = require("../../utils/logger");
const Sentry = require("@sentry/node");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/getById.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const AppError = require("../../utils/error");

const logger = require("../../utils/logger");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/getExtract.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const logger = require("../../utils/logger");
const dayjs = require("dayjs");

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const yup = require("yup");
const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const HebergementSchema = require("../../schemas/hebergement");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const yup = require("yup");

const Hebergement = require("../../services/Hebergement");
const Hebergement = require("../../services/hebergement/Hebergement");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
const HebergementSchema = require("../../schemas/hebergement");
Expand Down
27 changes: 27 additions & 0 deletions packages/backend/src/services/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ const query = {
WHERE uuid = $1;`,
[uuid],
],
getNameByUuid: (uuid) => [
`
SELECT
uuid,
filename as "name"
FROM doc.documents
WHERE uuid = $1;`,
[uuid],
],
};

module.exports.download = async (uuid) => {
Expand All @@ -63,6 +72,24 @@ module.exports.download = async (uuid) => {
}
};

module.exports.getNameByUuid = async (uuid) => {
log.i("IN");
try {
const { rows, rowCount } = await poolDoc.query(
...query.getNameByUuid(uuid),
);
if (rowCount > 0) {
log.i("DONE", rows[0]);
return rows[0];
}
log.i("DONE");
return null;
} catch (err) {
log.w(err);
throw new AppError("query.getNameByUuid failed", { cause: err });
}
};

module.exports.downloadNext = async (uuid) => {
log.i("IN");
try {
Expand Down
19 changes: 19 additions & 0 deletions packages/backend/src/services/adresse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const pool = require("../utils/pgpool").getPool();

const query = {
editCleInsee: `
UPDATE FRONT.ADRESSE
Expand Down Expand Up @@ -31,6 +33,18 @@ const query = {
($1, $2, $3, $4, $5, $6, $7)
RETURNING id
`,
getById: `
SELECT
LABEL as "label",
CODE_INSEE as "codeInsee",
CODE_POSTAL as "codePostal",
LONG as "long",
LAT as "lat",
DEPARTEMENT as "departement"
FROM
FRONT.ADRESSE
WHERE id = $1
`,
};

const getByCleInseeOrLabel = async (client, { cleInsee, label }) => {
Expand Down Expand Up @@ -71,3 +85,8 @@ module.exports.saveAdresse = async (client, adresse) => {

return existingAdresse.id;
};

module.exports.getById = async (id) => {
const { rows } = await pool.query(query.getById, [id]);
return rows?.[0] ?? null;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable no-param-reassign */
const logger = require("../utils/logger");
const { saveAdresse } = require("./adresse");
const pool = require("../utils/pgpool").getPool();
const logger = require("../../utils/logger");
const { saveAdresse, getById: getAdressById } = require("../adresse");
const { queryGetFields, mapDBHebergement } = require("./helpers");
const pool = require("../../utils/pgpool").getPool();

const log = logger(module.filename);

Expand Down Expand Up @@ -158,14 +159,12 @@ ${new Array(nbRows)
`
SELECT
id,
supprime,
nom,
coordonnees,
informations_locaux as "informationsLocaux",
informations_transport as "informationsTransport",
created_at as "createdAt",
edited_at as "editedAt"
FROM front.hebergement
${queryGetFields}
FROM front.hebergement h
WHERE id = $1
`,
[id],
Expand All @@ -174,12 +173,12 @@ ${new Array(nbRows)
SELECT
h.id as "id",
nom as "nom",
a.label,
a.departement
a.label as "adresse",
a.departement as "departement"
FROM front.hebergement h
LEFT JOIN front.user_organisme uo ON uo.org_id = h.organisme_id
LEFT JOIN front.adresse a ON a.id = h.adresse_id
WHERE uo.use_id = 1
WHERE uo.use_id = $1
AND CURRENT IS TRUE
`,
getPreviousValueForHistory: `
Expand Down Expand Up @@ -373,6 +372,14 @@ module.exports.getById = async (id) => {
const { rows: hebergements, rowCount } = await pool.query(
...query.getById(id),
);

if (rowCount === 0) {
return 0;
}
const hebergement = hebergements[0];

const adresse = await getAdressById(hebergement.adresseId);

log.d("getById - DONE");
return rowCount > 0 ? hebergements[0] : null;
return await mapDBHebergement(hebergement, adresse);
};
135 changes: 135 additions & 0 deletions packages/backend/src/services/hebergement/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const { adresse } = require("../../controllers/geo");

Check failure on line 1 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

'adresse' is assigned a value but never used
const { getNameByUuid } = require("../Document");
module.exports.queryGetFields = `
H.NOM AS "nom",
H.EMAIL AS "email",
H.ADRESSE_ID AS "adresseId",
H.TELEPHONE_1 AS "numTelephone1",
H.TELEPHONE_2 AS "numTelephone2",
H.NOM_GESTIONNAIRE AS "nomGestionnaire",
(
SELECT
VALUE
FROM
FRONT.HEBERGEMENT_TYPE
WHERE
ID = H.TYPE_ID
) AS "type",
(
SELECT
VALUE
FROM
FRONT.HEBERGEMENT_TYPE_PENSION
WHERE
ID = H.TYPE_PENSION_ID
) AS "typePension",
H.NOMBRE_LITS AS "nombreLits",
H.LIT_DESSUS AS "litDessus",
H.NOMBRE_LITS_SUPERPOSES AS "nombreLitsSuperposes",
H.NOMBRE_MAX_PERSONNES_COUCHAGE AS "nombreMaxPersonnesCouchage",
H.VISITE_LOCAUX AS "visiteLocaux",
H.VISITE_LOCAUX_AT AS "visiteLocauxAt",
H.ACCESSIBILITE_ID AS "accessibiliteId",
(
SELECT
VALUE
FROM
FRONT.HEBERGEMENT_ACCESSIBILITE
WHERE
ID = H.ACCESSIBILITE_ID
) AS "accessibilite",
H.ACCESSIBILITE_PRECISION AS "accessibilitePrecision",
H.CHAMBRES_DOUBLES AS "chambresDoubles",
H.CHAMBRES_UNISEXES AS "chambresUnisexes",
H.REGLEMENTATION_ERP AS "reglementationErp",
H.COUCHAGE_INDIVIDUEL AS "couchageIndividuel",
H.RANGEMENT_INDIVIDUEL AS "rangementIndividuel",
H.AMENAGEMENTS_SPECIFIQUES AS "amenagementsSpecifiques",
H.AMENAGEMENTS_SPECIFIQUES_PRECISION AS "amenagementsSpecifiquesPrecision",
H.DESCRIPTION_LIEU_HEBERGEMENT AS "descriptionLieuHebergement",
H.EXCURSION_DESCRIPTION AS "excursionDescription",
H.DEPLACEMENT_PROXIMITE_DESCRIPTION AS "deplacementProximiteDescription",
H.VEHICULES_ADAPTES AS "vehiculesAdaptes",
H.FILE_REPONSE_EXPLOITANT_OU_PROPRIETAIRE AS "fileReponseExploitantOuProprietaire",
H.FILE_DERNIER_ARRETE_AUTORISATION_MAIRE AS "fileDernierArreteAutorisationMaire",
H.FILE_DERNIERE_ATTESTATION_SECURITE AS "fileDerniereAttestationSecurite",
(
SELECT
ARRAY_AGG(HPH.VALUE)
FROM
FRONT.HEBERGEMENT_TO_PRESTATIONS_HOTELIERES HTPH
LEFT JOIN FRONT.HEBERGEMENT_PRESTATIONS_HOTELIERES HPH ON HPH.ID = HTPH.PRESTATION_ID
WHERE
HEBERGEMENT_ID = H.ID
) AS "prestationsHoteliere"
`;

const mapHebergementToCoordonnees = (hebergement, adresse) => {
return {
adresse: {
codeInsee: adresse.codeInsee,
codePostal: adresse.codePostal,
coordinates: [adresse.long, adresse.lat],
departement: adresse.departement,
label: adresse.label,
},
email: hebergement.email,
nomGestionnaire: hebergement.nomGestionnaire,
numTelephone1: hebergement.numTelephone1,
numTelephone2: hebergement.numTelephone2,
};
};

const mapHebergementToInformationsLocaux = async (hebergement) => {
return {
type: hebergement.type,
pension: hebergement.typePension,
litsDessus: hebergement.litDessus,
nombreLits: hebergement.nombreLits,
nombreLitsSuperposes: hebergement.nombreLitsSuperposes,
visiteLocaux: hebergement.visiteLocaux,
accessibilite: hebergement.accessibilite,

Check warning on line 91 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

Expected object keys to be in ascending order. 'couchageIndividuel' should be before 'litsDessus'
accessibilitePrecision: hebergement.accessibilitePrecision,
visiteLocauxAt: hebergement.visiteLocauxAt,
chambresDoubles: hebergement.chambresDoubles,
chambresUnisexes: hebergement.chambresUnisexes,

Check warning on line 95 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

Expected object keys to be in ascending order. 'amenagementsSpecifiques' should be before 'pension'
reglementationErp: hebergement.reglementationErp,
couchageIndividuel: hebergement.couchageIndividuel,

Check warning on line 97 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

Expected object keys to be in ascending order. 'descriptionLieuHebergement' should be before 'type'
rangementIndividuel: hebergement.rangementIndividuel,
prestationsHotelieres: hebergement.prestationsHoteliere,
amenagementsSpecifiques: hebergement.amenagementsSpecifiques,
descriptionLieuHebergement: hebergement.descriptionLieuHebergement,
nombreMaxPersonnesCouchage: hebergement.nombreMaxPersonnesCouchage,

Check warning on line 102 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

Expected object keys to be in ascending order. 'fileDernierArreteAutorisationMaire' should be before 'prestationsHotelieres'
precisionAmenagementsSpecifiques:
hebergement.amenagementsSpecifiquesPrecision,
fileDerniereAttestationSecurite: await getNameByUuid(
hebergement.fileDerniereAttestationSecurite,

Check warning on line 106 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

Expected object keys to be in ascending order. 'fileReponseExploitantOuProprietaire' should be before 'visiteLocaux'
),
fileDernierArreteAutorisationMaire: await getNameByUuid(
hebergement.fileDernierArreteAutorisationMaire,
),
fileReponseExploitantOuProprietaire: await getNameByUuid(

Check warning on line 111 in packages/backend/src/services/hebergement/helpers.js

View workflow job for this annotation

GitHub Actions / lint-format-test

Expected object keys to be in ascending order. 'precisionAmenagementsSpecifiques' should be before 'visiteLocauxAt'
hebergement.fileReponseExploitantOuProprietaire,
),
};
};

const mapHebergementToInformationsTransport = (hebergement) => {
return {
excursion: hebergement.excursionDescription,
vehiculesAdaptes: hebergement.vehiculesAdaptes,
deplacementProximite: hebergement.deplacementProximiteDescription,
};
};

const mapDBHebergement = async (hebergement, adresse) => {
return {
id: hebergement.id,
nom: hebergement.nom,
coordonnees: mapHebergementToCoordonnees(hebergement, adresse),
informationsLocaux: await mapHebergementToInformationsLocaux(hebergement),
informationsTransport: mapHebergementToInformationsTransport(hebergement),
};
};

module.exports.mapDBHebergement = mapDBHebergement;
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,11 @@ async function updateOrCreate(hebergement) {
// Sauvegarde de l'hébergement
try {
const id = await hebergementStore.updateOrCreate(
hebergement,
hebergementId.value,
);
await hebergementStore.updateOrCreate(hebergement, hebergementId.value);
log.d("hebergement sauvegardé");
toaster.success({ titleTag: "h2", description: "Hébergement sauvegardé" });
if (!hebergementId.value && id) {
return await navigateTo("/hebergements/liste");
}
await navigateTo("/hebergements/liste");
} catch (error) {
toaster.error({
titleTag: "h2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.raw(
`
ALTER TABLE FRONT.HEBERGEMENT
RENAME COLUMN VEHICULES_ADAPTES TO VEHICULES_ADAPTES_DEPRECATED;
ALTER TABLE FRONT.HEBERGEMENT
ADD COLUMN VEHICULES_ADAPTES BOOLEAN;
UPDATE FRONT.HEBERGEMENT
SET
VEHICULES_ADAPTES = CASE
WHEN VEHICULES_ADAPTES_DEPRECATED = 'true' THEN TRUE
ELSE FALSE
END;
`,
);
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.raw(
`
ALTER TABLE FRONT.HEBERGEMENT
DROP COLUMN VEHICULES_ADAPTES;
ALTER TABLE FRONT.HEBERGEMENT
RENAME COLUMN VEHICULES_ADAPTES_DEPRECATED TO VEHICULES_ADAPTES;
`,
);
};

0 comments on commit ad40a56

Please sign in to comment.