Skip to content

Commit

Permalink
fix(hebergement): db refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM committed Nov 14, 2024
1 parent 262f42e commit 3b62721
Show file tree
Hide file tree
Showing 18 changed files with 829 additions and 221 deletions.
6 changes: 4 additions & 2 deletions packages/backend/src/controllers/hebergement/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const HebergementSchema = require("../../schemas/hebergement");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
const AppError = require("../../utils/error");
const FOUser = require("../../services/FoUser");

const log = logger(module.filename);

Expand Down Expand Up @@ -44,11 +45,12 @@ module.exports = async function post(req, res, next) {
}

try {
const id = await Hebergement.create(userId, hebergement);
const organismeId = await FOUser.getUserOrganisme(userId);
const id = await Hebergement.create(userId, organismeId, hebergement);

return res.status(200).json({
id,
message: "sauvegarde organisme OK",
message: "sauvegarde hebegement OK",
});
} catch (error) {
log.w("DONE with error");
Expand Down
16 changes: 6 additions & 10 deletions packages/backend/src/controllers/hebergement/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ const log = logger(module.filename);

module.exports = async function post(req, res, next) {
const hebergementId = req.params.id;
const { nom, coordonnees, informationsLocaux, informationsTransport } =
req.body;
log.i("IN", {
coordonnees,
hebergementId,
informationsLocaux,
informationsTransport,
nom,
});
const { body, decoded } = req;
const userId = decoded.id;

const { nom, coordonnees, informationsLocaux, informationsTransport } = body;

if (
!nom ||
Expand All @@ -36,6 +31,7 @@ module.exports = async function post(req, res, next) {
);
}
let hebergement;

try {
hebergement = await yup.object(HebergementSchema.schema()).validate(
{
Expand All @@ -54,7 +50,7 @@ module.exports = async function post(req, res, next) {
}

try {
await Hebergement.update(hebergementId, hebergement);
await Hebergement.update(userId, hebergementId, hebergement);
log.i("DONE");
return res.sendStatus(200);
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/schemas/parts/adresse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const yup = require("yup");
const schema = ({ isFromAPIAdresse } = {}) => {
return isFromAPIAdresse
? {
cleInsee: yup.string().nullable(true),
codeInsee: yup.string().required("ce champ est obligatoire"),
codePostal: yup.string().required("ce champ est obligatoire"),
coordinates: yup.array().required("ce champ est obligatoire"),
Expand Down
111 changes: 86 additions & 25 deletions packages/backend/src/services/DemandeSejour.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,38 @@ WHERE
RETURNING
id as "declarationId"
`,
unlinkToHebergement: `
DELETE FROM FRONT.DEMANDE_SEJOUR_TO_HEBERGEMENT
WHERE
DEMANDE_SEJOUR_ID = $1;
`,
linkToHebergements: (nbRows) => `
INSERT INTO
FRONT.DEMANDE_SEJOUR_TO_HEBERGEMENT (
DEMANDE_SEJOUR_ID,
HEBERGEMENT_ID,
DATE_DEBUT,
DATE_FIN
)
VALUES
${new Array(nbRows)
.fill(null)
.map(
(_, index) =>
`($1, $${3 * index + 2}, $${3 * index + 3}, $${3 * index + 4})`,
)
.join(",")}
`,
};

const linkToHebergements = async (client, declarationId, hebergements) => {
await client.query(query.unlinkToHebergement, [declarationId]);
if (hebergements.length > 0) {
await client.query(query.linkToHebergements(hebergements.length), [
declarationId,
...hebergements.flatMap((h) => [h.hebergementId, h.dateDebut, h.dateFin]),
]);
}
};

module.exports.create = async ({
Expand Down Expand Up @@ -841,33 +873,51 @@ module.exports.create = async ({

module.exports.copy = async (declaration) => {
log.i("copy - IN");
const response = await pool.query(
...query.copy(
declaration.organismeId,
`COPIE - ${declaration.libelle}`,
declaration.dateDebut,
declaration.dateFin,
declaration.duree,
declaration.periode,
declaration.responsableSejour,
declaration.organisme,
declaration.hebergement,
declaration.informationsVacanciers,
declaration.informationsPersonnel,
declaration.informationsTransport,
declaration.projetSejour,
declaration.informationsSanitaires,
declaration.files,
),
);
log.d(response);
const { declarationId } = response.rows[0];
const client = await pool.connect();
let declarationId;
try {
await client.query("BEGIN");
const response = await client.query(
...query.copy(
declaration.organismeId,
`COPIE - ${declaration.libelle}`,
declaration.dateDebut,
declaration.dateFin,
declaration.duree,
declaration.periode,
declaration.responsableSejour,
declaration.organisme,
declaration.hebergement,
declaration.informationsVacanciers,
declaration.informationsPersonnel,
declaration.informationsTransport,
declaration.projetSejour,
declaration.informationsSanitaires,
declaration.files,
),
);
log.d(response);
console.log(declaration.hebergement);
declarationId = response.rows[0].declarationId;
await linkToHebergements(
client,
declarationId,
declaration.hebergement?.hebergements ?? [],
);
await client.query("COMMIT");
} catch (error) {
await client.query("ROLLBACK");
throw error;
} finally {
client.release();
}
log.i("copy - DONE", { declarationId });
return declarationId;
};

module.exports.delete = async (declarationId, userId) => {
log.i("delete - IN");
await pool.query(query.unlinkToHebergement, [declarationId]);
const { rowCount } = await pool.query(...query.delete(declarationId, userId));
log.i("delete - DONE");
return rowCount;
Expand Down Expand Up @@ -1182,10 +1232,21 @@ module.exports.update = async (type, declarationId, parametre) => {
}
case "hebergements": {
log.d("hebergements", declarationId);
response = await pool.query(query.updateHebergement, [
parametre,
declarationId,
]);
const client = await pool.connect();
try {
await client.query("BEGIN");
await linkToHebergements(client, declarationId, parametre.hebergements);
response = await client.query(query.updateHebergement, [
parametre,
declarationId,
]);
await client.query("COMMIT");
} catch (error) {
await client.query("ROLLBACK");
throw error;
} finally {
client.release();
}
break;
}
default:
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/services/FoUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ${additionalParamsQuery}
`,
additionalParams,
],
getUserOragnisme: `SELECT org_id as "organismeId" FROM front.user_organisme WHERE use_id = $1`,
};

module.exports.read = async ({
Expand Down Expand Up @@ -150,3 +151,8 @@ module.exports.readOne = async (id) => {
log.i("readOne - DONE");
return users[0];
};

module.exports.getUserOrganisme = async (userId) => {
const { rows } = await pool.query(query.getUserOragnisme, [userId]);
return rows[0]?.organismeId ?? null;
};
Loading

0 comments on commit 3b62721

Please sign in to comment.