diff --git a/packages/backend/src/controllers/demandeSejour/depose.js b/packages/backend/src/controllers/demandeSejour/depose.js index af9f7d864..c6dbc755e 100644 --- a/packages/backend/src/controllers/demandeSejour/depose.js +++ b/packages/backend/src/controllers/demandeSejour/depose.js @@ -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"); diff --git a/packages/backend/src/controllers/hebergement/get.js b/packages/backend/src/controllers/hebergement/get.js index 5ec9b788c..d198f9bd6 100644 --- a/packages/backend/src/controllers/hebergement/get.js +++ b/packages/backend/src/controllers/hebergement/get.js @@ -1,4 +1,4 @@ -const Hebergement = require("../../services/Hebergement"); +const Hebergement = require("../../services/hebergement/Hebergement"); const logger = require("../../utils/logger"); diff --git a/packages/backend/src/controllers/hebergement/getByDepartements.js b/packages/backend/src/controllers/hebergement/getByDepartements.js index 29c1244ae..6db586038 100644 --- a/packages/backend/src/controllers/hebergement/getByDepartements.js +++ b/packages/backend/src/controllers/hebergement/getByDepartements.js @@ -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"); diff --git a/packages/backend/src/controllers/hebergement/getById.js b/packages/backend/src/controllers/hebergement/getById.js index aa7aa2ae4..7a902ce22 100644 --- a/packages/backend/src/controllers/hebergement/getById.js +++ b/packages/backend/src/controllers/hebergement/getById.js @@ -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"); diff --git a/packages/backend/src/controllers/hebergement/getExtract.js b/packages/backend/src/controllers/hebergement/getExtract.js index 38f425f15..8306bb8ba 100644 --- a/packages/backend/src/controllers/hebergement/getExtract.js +++ b/packages/backend/src/controllers/hebergement/getExtract.js @@ -1,4 +1,4 @@ -const Hebergement = require("../../services/Hebergement"); +const Hebergement = require("../../services/hebergement/Hebergement"); const logger = require("../../utils/logger"); const dayjs = require("dayjs"); diff --git a/packages/backend/src/controllers/hebergement/post.js b/packages/backend/src/controllers/hebergement/post.js index 37ae6c5bf..ea1d0aa57 100644 --- a/packages/backend/src/controllers/hebergement/post.js +++ b/packages/backend/src/controllers/hebergement/post.js @@ -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"); diff --git a/packages/backend/src/controllers/hebergement/update.js b/packages/backend/src/controllers/hebergement/update.js index 359c0ac2f..c8f7ad392 100644 --- a/packages/backend/src/controllers/hebergement/update.js +++ b/packages/backend/src/controllers/hebergement/update.js @@ -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"); diff --git a/packages/backend/src/services/Document.js b/packages/backend/src/services/Document.js index 3b881e329..085e1e9fb 100644 --- a/packages/backend/src/services/Document.js +++ b/packages/backend/src/services/Document.js @@ -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) => { @@ -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 { diff --git a/packages/backend/src/services/adresse.js b/packages/backend/src/services/adresse.js index 7ef8af2c6..ca0e94748 100644 --- a/packages/backend/src/services/adresse.js +++ b/packages/backend/src/services/adresse.js @@ -1,3 +1,5 @@ +const pool = require("../utils/pgpool").getPool(); + const query = { editCleInsee: ` UPDATE FRONT.ADRESSE @@ -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 }) => { @@ -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; +}; diff --git a/packages/backend/src/services/Hebergement.js b/packages/backend/src/services/hebergement/Hebergement.js similarity index 95% rename from packages/backend/src/services/Hebergement.js rename to packages/backend/src/services/hebergement/Hebergement.js index 017a414f3..bc622d2ad 100644 --- a/packages/backend/src/services/Hebergement.js +++ b/packages/backend/src/services/hebergement/Hebergement.js @@ -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); @@ -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], @@ -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: ` @@ -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); }; diff --git a/packages/backend/src/services/hebergement/helpers.js b/packages/backend/src/services/hebergement/helpers.js new file mode 100644 index 000000000..69ae79c49 --- /dev/null +++ b/packages/backend/src/services/hebergement/helpers.js @@ -0,0 +1,135 @@ +const { adresse } = require("../../controllers/geo"); +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, + accessibilitePrecision: hebergement.accessibilitePrecision, + visiteLocauxAt: hebergement.visiteLocauxAt, + chambresDoubles: hebergement.chambresDoubles, + chambresUnisexes: hebergement.chambresUnisexes, + reglementationErp: hebergement.reglementationErp, + couchageIndividuel: hebergement.couchageIndividuel, + rangementIndividuel: hebergement.rangementIndividuel, + prestationsHotelieres: hebergement.prestationsHoteliere, + amenagementsSpecifiques: hebergement.amenagementsSpecifiques, + descriptionLieuHebergement: hebergement.descriptionLieuHebergement, + nombreMaxPersonnesCouchage: hebergement.nombreMaxPersonnesCouchage, + precisionAmenagementsSpecifiques: + hebergement.amenagementsSpecifiquesPrecision, + fileDerniereAttestationSecurite: await getNameByUuid( + hebergement.fileDerniereAttestationSecurite, + ), + fileDernierArreteAutorisationMaire: await getNameByUuid( + hebergement.fileDernierArreteAutorisationMaire, + ), + fileReponseExploitantOuProprietaire: await getNameByUuid( + 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; diff --git a/packages/frontend-usagers/src/pages/hebergements/[[hebergementId]].vue b/packages/frontend-usagers/src/pages/hebergements/[[hebergementId]].vue index b59906a91..9f7336e39 100644 --- a/packages/frontend-usagers/src/pages/hebergements/[[hebergementId]].vue +++ b/packages/frontend-usagers/src/pages/hebergements/[[hebergementId]].vue @@ -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", diff --git a/packages/migrations/src/migrations/20241119135647_vehiculesAdaptes-type.js b/packages/migrations/src/migrations/20241119135647_vehiculesAdaptes-type.js new file mode 100644 index 000000000..9ce2e49be --- /dev/null +++ b/packages/migrations/src/migrations/20241119135647_vehiculesAdaptes-type.js @@ -0,0 +1,38 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +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 } + */ +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; + `, + ); +};