Skip to content

Commit

Permalink
chore(sentry): add instrumentation to declaration 8jours and 2mois
Browse files Browse the repository at this point in the history
  • Loading branch information
achauve committed Sep 6, 2024
1 parent 14632f4 commit 24368a8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
.prettierrc
.eslintcache
.local
.cache

yarn-error.log
.yarn
Expand Down
96 changes: 52 additions & 44 deletions packages/backend/src/services/mail/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Sentry = require("@sentry/node");
const logger = require("../../utils/logger");
const AppError = require("../../utils/error");
const { getTransporter } = require("./transporter");
Expand All @@ -8,49 +9,56 @@ const log = logger(module.filename);

module.exports.mailService = {
send: async (payload) => {
log.i("send - IN", { payload });

const mail = { ...payload };

if (typeof mail.to === "string") {
mail.to = [mail.to];
}

if (typeof mail.cc === "string") {
mail.cc = [mail.cc];
}

try {
await mailSchema.validate(mail, {
stripUnknown: true,
});
} catch (error) {
log.w(error.name, error.errors.join(", "));
throw new AppError(error.errors.join(", "), {
cause: error,
name: "ValidationError",
statusCode: 423,
});
}

if ([...(mail.to ?? []), ...(mail.cc ?? [])].length === 0) {
throw new AppError("At least one element in to or cc is expected", {
name: "ValidationError",
statusCode: 423,
});
}
const transporter = getTransporter();

try {
await transporter.sendMail(mail);
} catch (error) {
log.w(error.message);
throw new AppError("An unexpected error happens !", {
cause: error,
statusCode: 500,
});
}

log.i("send - DONE");
await Sentry.startSpan({ name: "services.mail.send" }, async () => {
await doSend(payload);
});
},
};

async function doSend(payload) {
log.i("send - IN", { payload });

const mail = { ...payload };

if (typeof mail.to === "string") {
mail.to = [mail.to];
}

if (typeof mail.cc === "string") {
mail.cc = [mail.cc];
}

try {
await mailSchema.validate(mail, {
stripUnknown: true,
});
} catch (error) {
log.w(error.name, error.errors.join(", "));
throw new AppError(error.errors.join(", "), {
cause: error,
name: "ValidationError",
statusCode: 423,
});
}

if ([...(mail.to ?? []), ...(mail.cc ?? [])].length === 0) {
throw new AppError("At least one element in to or cc is expected", {
name: "ValidationError",
statusCode: 423,
});
}
const transporter = getTransporter();

try {
await transporter.sendMail(mail);
} catch (error) {
log.w(error.message);
throw new AppError("An unexpected error happens !", {
cause: error,
statusCode: 500,
});
}

log.i("send - DONE");
return module.exports.mailService.send(payload);
}
15 changes: 14 additions & 1 deletion packages/backend/src/services/pdf/declaration2mois/generate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Sentry = require("@sentry/node");

/* eslint-disable no-param-reassign */
const logger = require("../../../utils/logger");
const build = require("./build");
Expand Down Expand Up @@ -47,4 +49,15 @@ const generate = async (declaration, idFonctionnelle, departementSuivi) => {
}
};

module.exports = generate;
module.exports = module.exports = async (
declaration,
idFonctionnelle,
departementSuivi,
) => {
await Sentry.startSpan(
{ name: "services.pdf.declaration2mois.generate" },
async () => {
return await generate(declaration, idFonctionnelle, departementSuivi);
},
);
};
21 changes: 20 additions & 1 deletion packages/backend/src/services/pdf/declaration8jours/generate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Sentry = require("@sentry/node");

/* eslint-disable no-param-reassign */
const logger = require("../../../utils/logger");
const build = require("./build");
Expand Down Expand Up @@ -56,4 +58,21 @@ const generate = async (
}
};

module.exports = generate;
module.exports = async (
declaration,
idFonctionnelle,
departementSuivi,
dateDeposeA2mois,
) => {
await Sentry.startSpan(
{ name: "services.pdf.declaration8jours.generate" },
async () => {
return await generate(
declaration,
idFonctionnelle,
departementSuivi,
dateDeposeA2mois,
);
},
);
};

0 comments on commit 24368a8

Please sign in to comment.