Skip to content

Commit

Permalink
Merge pull request #145 from SocialGouv/fix-236/224-feat-241/242
Browse files Browse the repository at this point in the history
fix: 236/224 et feat 241/242
  • Loading branch information
gweill-guigops authored Apr 15, 2024
2 parents c3923f8 + 37d137f commit 174e120
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 36 deletions.
38 changes: 37 additions & 1 deletion packages/backend/src/controllers/demandeSejour/depose.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ module.exports = async function post(req, res, next) {
const cc = await DemandeSejour.getEmailCcList(
declaration.organisme.personneMorale.siren ?? "personnePhysique",
);

const filteredCc = cc.filter((d) => !destinataires.includes(d));
if (destinataires) {
await Send(
MailUtils.usagers.declarationSejour.sendAR2mois({
cc: cc,
cc: filteredCc,
declaration,
dest: destinataires,
}),
Expand All @@ -153,6 +155,40 @@ module.exports = async function post(req, res, next) {
message: "Une erreur est survenue lors de l'envoi de mails",
});
}
try {
const destinatairesBack =
await DemandeSejour.getEmailBack(departementSuivi);

if (destinatairesBack) {
const departements = declaration.hebergement.hebergements.map(
(h) => h.coordonnees.adresse.departement,
);
const destinatairesBackCc =
await DemandeSejour.getEmailBackCc(departements);

const filteredBackCc = destinatairesBackCc.filter(
(d) => !destinatairesBack.includes(d),
);
await Send(
MailUtils.bo.declarationSejour.sendDeclarationNotify({
cc: filteredBackCc,
departementSuivi,
departementsSecondaires: departements.filter(
(d) => d !== departementSuivi,
),
destinataires: destinatairesBack,
id: declaration.idFonctionnelle,
}),
);
}
} catch (error) {
log.w(error);
return res.status(400).json({
message:
"Une erreur est survenue lors de l'envoi de mails aux usagers back office",
});
}

try {
DSuuid = await PdfDeclaration2Mois(
declaration,
Expand Down
11 changes: 3 additions & 8 deletions packages/backend/src/controllers/organisme/finalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ const logger = require("../../utils/logger");
const log = logger(module.filename);

module.exports = async function finalize(req, res, next) {
const organismeId = req.params.organismeId;
log.i("IN", { organismeId });

if (!organismeId) {
log.w("missing or invalid parameter");
return res.status(400).json({ message: "paramètre manquant ou erroné." });
}
const { id: userId } = req.decoded;
log.i("IN", { userId });

try {
await Organisme.finalize(organismeId);
await Organisme.finalize(userId);
log.i("DONE");
return res.status(200).json({
message: "sauvegarde organisme OK",
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/schemas/organisme.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const personneMoraleSchema = () => ({
...personneSchema({
showAdresse: true,
showEmail: true,
showTelephone: false,
showTelephone: true,
}),
}),
siegeSocial: yup.boolean().required(),
Expand Down
67 changes: 49 additions & 18 deletions packages/backend/src/services/DemandeSejour.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,26 @@ const query = {
where (${getDepartementWhereQuery(departementCodes)})
AND ds.id = $1
`,
getEmailBack: `
SELECT DISTINCT u.mail AS mail
FROM back.users u
WHERE u.ter_code = $1
`,
getEmailBackCc: `
WITH regions AS (
SELECT
ARRAY_AGG(parent_code) as parent_code
FROM geo.territoires
WHERE code = ANY($1)
)
SELECT DISTINCT u.mail AS mail
FROM back.users u, regions
WHERE u.ter_code = ANY($1)
OR u.ter_code = ANY(regions.parent_code)
OR u.ter_code = 'FRA'
`,
getEmailCcList: `
SELECT u.mail AS mail
SELECT DISTINCT u.mail AS mail
FROM front.users u
JOIN front.user_organisme uo ON u.id = uo.use_id
JOIN front.organismes o ON uo.org_id = o.id
Expand All @@ -200,7 +218,7 @@ const query = {
o.personne_morale->>'siegeSocial' = 'true'
`,
getEmailToList: `
SELECT u.mail AS mail
SELECT DISTINCT u.mail AS mail
FROM front.users u
JOIN front.user_organisme uo
ON u.id = uo.use_id
Expand Down Expand Up @@ -424,14 +442,14 @@ module.exports.create = async (
);
log.d(response);
const { demandeId } = response.rows[0];
log.d("create - DONE", { demandeId });
log.i("create - DONE", { demandeId });
return demandeId;
};

module.exports.get = async (userId) => {
log.i("get - IN", { userId });
const response = await pool.query(query.get, [userId]);
log.d("get - DONE");
log.i("get - DONE");
const demandes = response.rows;
return demandes;
};
Expand Down Expand Up @@ -524,7 +542,7 @@ module.exports.getByDepartementCodes = async (
query.getByDepartementCodesTotal(searchQuery, departementCodes),
);

log.d("getByAdminId - DONE");
log.i("getByAdminId - DONE");
return {
demandes_sejour: response.rows,
total: total.rows.find((t) => t.count)?.count ?? 0,
Expand All @@ -540,13 +558,13 @@ module.exports.getById = async (demandeId, departementCodes) => {
const { rows: demande } = await pool.query(query.getById(departementCodes), [
demandeId,
]);
log.d("getById - DONE");
log.i("getById - DONE");
log.d(demande);
return demande[0];
};

module.exports.update = async (type, demandeSejourId, parametre) => {
log.w("update - IN", { demandeSejourId, parametre, type });
log.i("update - IN", { demandeSejourId, parametre, type });
let response;
switch (type) {
case "organisme": {
Expand Down Expand Up @@ -633,7 +651,7 @@ module.exports.update = async (type, demandeSejourId, parametre) => {
log.d("wrong type");
return null;
}
log.d("update - DONE");
log.i("update - DONE");
const demandeId = response.rows[0].demandeId ?? null;
return demandeId;
};
Expand Down Expand Up @@ -688,20 +706,33 @@ module.exports.getNextIndex = async () => {
log.i("getNextIndex - IN");
const { rows: data } = await pool.query(query.getNextIndex);
log.d(data[0].index);
log.d("getNextIndex - DONE");
log.i("getNextIndex - DONE");
return data[0].index ?? null;
};
module.exports.getEmailToList = async (organismeId) => {
log.i("getEmailToList - IN", organismeId);
const { rows: data } = await pool.query(query.getEmailToList, [organismeId]);
log.d("getEmailToList - DONE");
return data.map((m) => m.mail).join(",") ?? null;
log.i("getEmailToList - DONE");
return data.map((m) => m.mail);
};

module.exports.getEmailCcList = async (siren) => {
log.i("getEmailCcList - IN", siren);
const { rows: data } = await pool.query(query.getEmailCcList, [siren]);
log.d("getEmailCcList - DONE");
return data.map((m) => m.mail).join(",") ?? null;
log.i("getEmailCcList - DONE");
return data.map((m) => m.mail);
};
module.exports.getEmailBack = async (departement) => {
log.i("getEmailBack - IN", departement);
const { rows: data } = await pool.query(query.getEmailBack, [departement]);
log.i("getEmailBack - DONE");
return data.map((m) => m.mail);
};
module.exports.getEmailBackCc = async (departements) => {
log.i("getEmailBackCc - IN", departements);
const { rows: data } = await pool.query(query.getEmailBackCc, [departements]);
log.i("getEmailBackCc - DONE");
return data.map((m) => m.mail);
};
module.exports.insertEvent = async (
source,
Expand All @@ -722,7 +753,7 @@ module.exports.insertEvent = async (
typePrecision,
metaData,
]);
log.d("insertEvent - DONE");
log.i("insertEvent - DONE");
return response[0].eventId ?? null;
};

Expand All @@ -732,7 +763,7 @@ module.exports.addFile = async (declarationId, file) => {
declarationId,
file,
]);
log.d("addFile - DONE");
log.i("addFile - DONE");
return response[0].declarationId;
};

Expand All @@ -741,7 +772,7 @@ module.exports.historique = async (declarationId) => {
const { rows: response } = await pool.query(query.historique, [
declarationId,
]);
log.d("historique - DONE");
log.i("historique - DONE");
return response;
};

Expand All @@ -751,7 +782,7 @@ module.exports.updateStatut = async (
event = null,
cb = null,
) => {
log.i("update status - IN", { demandeSejourId, statut });
log.i("updateStatut - IN", { demandeSejourId, statut });
const client = await pool.connect();

try {
Expand All @@ -775,7 +806,7 @@ module.exports.updateStatut = async (

await client.query("COMMIT");
const demandeId = response.rows[0].demandeId ?? null;
log.w("update status - OUT");
log.i("updateStatut - DONE");

return demandeId;
} catch (e) {
Expand Down
9 changes: 5 additions & 4 deletions packages/backend/src/services/Organisme.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const query = {
AND a.supprime = false
) AS agrement
FROM front.organismes o
JOIN front.user_organisme uo ON o.id = org_id
JOIN front.user_organisme uo ON o.id = uo.org_id
WHERE 1=1
${Object.keys(criterias)
.map((criteria, i) => ` AND ${criteria} = $${i + 1}`)
Expand Down Expand Up @@ -249,17 +249,18 @@ module.exports.update = async (type, parametre, organismeId) => {
log.i("update - DONE");
};

module.exports.finalize = async function (organismeId) {
log.i("finalize - IN", { organismeId });
module.exports.finalize = async function (userId) {
log.i("finalize - IN", { userId });
if (!regions) {
regions = await Regions.fetch();
}
if (!organismeSchema) {
organismeSchema = Organisme.schema(regions);
}
const criterias = {
"o.id": organismeId,
"uo.use_id": userId,
};
log.i(...query.get(criterias));
const { rows, rowCount } = await pool.query(...query.get(criterias));
if (rowCount !== 1) {
throw new AppError("Organisme non trouvé", {
Expand Down
42 changes: 42 additions & 0 deletions packages/backend/src/utils/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,48 @@ module.exports = {
params,
});

return params;
},
},
declarationSejour: {
sendDeclarationNotify: ({
id,
destinataires,
cc,
departementSuivi,
departementsSecondaires,
}) => {
log.i("sendDeclarationNotify - In", {
destinataires,
});
if (!destinataires) {
const message = `Le paramètre destinataires manque à la requête`;
log.w(`sendDeclarationNotify - ${message}`);
throw new AppError(message);
}

const params = {
cc,
from: senderEmail,
html: `
<p>Bonjour,</p>
<p>La déclaration de séjour ${id} vient d'être déposée sur le portail VAO</p>
<p>Le(s) département(s) ${departementSuivi} est(sont) en charge de l'instruction principale de cette déclaration.</p>
${
departementsSecondaires.length > 0
? `<p>Les départements ${departementsSecondaires.join(", ")} sont en charge du contrôle d'au moins un hébergement.</p>`
: ""
}
`,
replyTo: senderEmail,
subject: `nouvelle déclaration : ${id}`,
to: destinataires,
};
log.d("sendDeclarationNotify post email", {
params,
});

return params;
},
},
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend-bo/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ export default defineNuxtConfig({
},
srcDir: "src",
ssr: false,
security: {
enabled: process.env.NODE_ENV === "production",
},
});
3 changes: 3 additions & 0 deletions packages/frontend-usagers/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ export default defineNuxtConfig({
},
srcDir: "src",
ssr: false,
security: {
enabled: process.env.NODE_ENV === "production",
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const { value: file } = useField("file");
async function next() {
log.i("next - IN");
if (file.value.type !== "application/pdf") {
if (file.value.type && file.value.type !== "application/pdf") {
toaster.error("L'agrément doit obligatoirement être au format PDF");
} else {
if (!meta.value.dirty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
>
<DsfrButton
v-if="props.initOrganisme.complet === false"
label="Finaliser la fiche organisateur"
:disabled="!meta.valid"
@click.prevent="finalizeOrganisme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@
"
class="fr-fieldset__element"
>
>
<DsfrInputGroup
name="precisionPreparationPilluliers"
:readonly="!props.modifiable"
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-usagers/src/utils/organisme.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const personneMoraleSchema = {
responsableSejour: yup.object({
...personne.schema({
showAdresse: true,
showTelephone: false,
showTelephone: true,
showEmail: true,
}),
}),
Expand Down

0 comments on commit 174e120

Please sign in to comment.