|
| 1 | +"use strict"; |
| 2 | +const express = require("express"); |
| 3 | +const Services = { |
| 4 | + AutomatedEmail: require("../../services/automatedEmails.service"), |
| 5 | +}; |
| 6 | +const Middleware = { |
| 7 | + Auth: require("../../middlewares/auth.middleware"), |
| 8 | +}; |
| 9 | +const PROJECT_CONSTANTS = require("../../constants/general.constant"); |
| 10 | +const Constants = { |
| 11 | + STATUSES: [ |
| 12 | + PROJECT_CONSTANTS.HACKER_STATUS_ACCEPTED, |
| 13 | + PROJECT_CONSTANTS.HACKER_STATUS_DECLINED, |
| 14 | + ], |
| 15 | +}; |
| 16 | + |
| 17 | +module.exports = { |
| 18 | + activate: function (apiRouter) { |
| 19 | + const automatedEmailRouter = express.Router(); |
| 20 | + |
| 21 | + /** |
| 22 | + * @api {post} /email/automated/status/:status Send emails to all hackers with specified status |
| 23 | + * @apiName sendAutomatedStatusEmails |
| 24 | + * @apiGroup Email |
| 25 | + * @apiVersion 0.0.8 |
| 26 | + * |
| 27 | + * @apiParam {string} status Status of hackers to email (Accepted/Declined) |
| 28 | + * |
| 29 | + * @apiSuccess {string} message Success message |
| 30 | + * @apiSuccess {object} data Contains counts of successful and failed emails |
| 31 | + * @apiSuccessExample {object} Success-Response: |
| 32 | + * { |
| 33 | + * "message": "Successfully sent emails", |
| 34 | + * "data": { |
| 35 | + * "success": 50, |
| 36 | + * "failed": 2 |
| 37 | + * } |
| 38 | + * } |
| 39 | + */ |
| 40 | + automatedEmailRouter |
| 41 | + .route("/automated/status/:status") |
| 42 | + .post( |
| 43 | + Middleware.Auth.ensureAuthenticated(), |
| 44 | + Middleware.Auth.ensureAuthorized(), |
| 45 | + async (req, res) => { |
| 46 | + const { status } = req.params; |
| 47 | + |
| 48 | + if (!Constants.STATUSES.includes(status)) { |
| 49 | + return res.status(400).json({ |
| 50 | + message: "Invalid status", |
| 51 | + data: {}, |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + const results = |
| 57 | + await Services.AutomatedEmail.sendAutomatedStatusEmails( |
| 58 | + status, |
| 59 | + ); |
| 60 | + return res.status(200).json({ |
| 61 | + message: "Successfully sent emails", |
| 62 | + data: results, |
| 63 | + }); |
| 64 | + } catch (err) { |
| 65 | + return res.status(500).json({ |
| 66 | + message: err.message, |
| 67 | + data: {}, |
| 68 | + }); |
| 69 | + } |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + apiRouter.use("/email", automatedEmailRouter); |
| 74 | + }, |
| 75 | +}; |
0 commit comments