Skip to content

Commit

Permalink
Merge pull request #184 from mission-apprentissage/ui-server/add-slac…
Browse files Browse the repository at this point in the history
…k-support

[UI - server] Ajout bot slack
  • Loading branch information
yohanngab authored Nov 6, 2023
2 parents f4c6ad6 + 801ebe5 commit 10252be
Show file tree
Hide file tree
Showing 11 changed files with 1,255 additions and 50 deletions.
2 changes: 1 addition & 1 deletion server/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2020
},
"rules": {
"node/no-unpublished-require": ["error", {
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@hapi/boom": "10.0.0",
"@slack/bolt": "3.14.0",
"axios": "1.3.1",
"body-parser": "1.20.1",
"bunyan": "1.8.15",
Expand Down
6 changes: 5 additions & 1 deletion server/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const env = require("env-var");
module.exports = {
env: env.get("SIRIUS_ENV").default("dev").asString(),
publicUrl: env.get("SIRIUS_PUBLIC_URL").default("http://localhost").asString(),
slackWebhookUrl: env.get("SIRIUS_SLACK_WEBHOOK_URL").asString(),
log: {
type: env.get("SIRIUS_LOG_TYPE").default("console").asString(),
level: env.get("SIRIUS_LOG_LEVEL").default("info").asString(),
Expand All @@ -28,4 +27,9 @@ module.exports = {
},
email_from: env.get("SIRIUS_EMAIL_FROM").default("sirius@test.fr").required().asString(),
},
slack: {
token: env.get("SIRIUS_SLACK_TOKEN").default("").asString(),
signingSecret: env.get("SIRIUS_SLACK_SIGNING_SECRET").default("").asString(),
channel: env.get("SIRIUS_SLACK_CHANNEL").default("").asString(),
},
};
110 changes: 110 additions & 0 deletions server/src/controllers/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { COOKIE_OPTIONS } = require("../utils/authenticate.utils");
const { shootTemplate } = require("../modules/mailer");
const config = require("../config");
const { USER_STATUS } = require("../constants");
const { sendToSlack } = require("../modules/slack");

const createUser = tryCatch(async (req, res) => {
const { success, body } = await usersService.createUser(req.body);
Expand All @@ -31,6 +32,59 @@ const createUser = tryCatch(async (req, res) => {
},
});

const etablissementsDisplay = body.etablissements
.map((etablissement) => {
return `• ${etablissement.siret} - ${
etablissement.onisep_nom || etablissement.enseigne || etablissement.entreprise_raison_sociale
}`;
})
.join("\n");

await sendToSlack([
{
type: "header",
text: {
type: "plain_text",
text: `:bell: *Nouvelle inscription en ${config.env.toUpperCase()}!* :bell:`,
emoji: true,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:star2: *Nom:* ${body.firstName} ${body.lastName}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:email: *Email:* ${body.email}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:school: *Établissements:*\n${etablissementsDisplay}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:memo: *Commentaire:* \n${body.comment}`,
},
},
{
type: "divider",
},
]);

res.status(201).json(body);
});

Expand Down Expand Up @@ -158,6 +212,61 @@ const confirmUser = tryCatch(async (req, res) => {
return res.status(200).json({ success: true });
});

const supportUser = tryCatch(async (req, res) => {
const { title, message } = req.body;
const { email, firstName, lastName } = req.user;

const slackResponse = await sendToSlack([
{
type: "header",
text: {
type: "plain_text",
text: `Demande d'aide en ${config.env.toUpperCase()}!`,
emoji: true,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:raising_hand: *${firstName} ${lastName}* a besoin d'aide!`,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:label: *Titre:*\n${title}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:envelope: *Email:*\n${email}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `:pencil: *Message:*\n${message}`,
},
},
{
type: "divider",
},
]);

return res.status(200).json({ success: slackResponse });
});

module.exports = {
loginUser,
refreshTokenUser,
Expand All @@ -169,4 +278,5 @@ module.exports = {
forgotPassword,
resetPassword,
confirmUser,
supportUser,
};
24 changes: 24 additions & 0 deletions server/src/modules/slack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { App } = require("@slack/bolt");

const config = require("../config");

const sendToSlack = async (main) => {
if (!config.slack.channel) return;

const slack = new App({
token: config.slack.token,
signingSecret: config.slack.signingSecret ?? "",
});

const sent = await slack.client.chat.postMessage({
text: "",
blocks: main,
channel: config.slack.channel,
});

return sent.ok;
};

module.exports = {
sendToSlack,
};
6 changes: 6 additions & 0 deletions server/src/routes/users.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
forgotPasswordSchema,
resetPasswordSchema,
confirmSchema,
supportSchema,
} = require("../validators/users.validators");
const {
loginUser,
Expand All @@ -21,6 +22,7 @@ const {
forgotPassword,
resetPassword,
confirmUser,
supportUser,
} = require("../controllers/users.controller");
const { isAdmin } = require("../middlewares/isAdmin");
const { rateLimiter } = require("../middlewares/rateLimiter");
Expand Down Expand Up @@ -58,6 +60,10 @@ const users = () => {
confirmUser(req, res, next)
);

router.post("/api/users/support/", verifyUser, rateLimiter, validator(supportSchema), (req, res, next) =>
supportUser(req, res, next)
);

return router;
};

Expand Down
6 changes: 6 additions & 0 deletions server/src/validators/users.validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ const confirmSchema = Joi.object({
token: Joi.string().required(),
});

const supportSchema = Joi.object({
title: Joi.string().required(),
message: Joi.string().required(),
});

module.exports = {
loginSchema,
subscribeSchema,
updateSchema,
forgotPasswordSchema,
resetPasswordSchema,
confirmSchema,
supportSchema,
};
Loading

0 comments on commit 10252be

Please sign in to comment.