From f64cb79c135b581b1c682dc426288c43e965c437 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Tue, 16 Dec 2025 18:54:50 +0530 Subject: [PATCH] Establishment user welcome email : GWM --- .../establishment_user.controller.js | 77 ++++++++++++++++++- app/routes/routes.js | 31 ++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/app/controllers/establishment_user.controller.js b/app/controllers/establishment_user.controller.js index 1b19893..d5be30c 100644 --- a/app/controllers/establishment_user.controller.js +++ b/app/controllers/establishment_user.controller.js @@ -228,7 +228,6 @@ exports.changeUserPassword = async (req, res) => { } }; - // Delete user exports.deleteUser = async (req, res) => { try { @@ -243,3 +242,79 @@ exports.deleteUser = async (req, res) => { return res.status(500).send({'status':"failed",'message':err.message }); } }; + + + +// Trigger user creation email +exports.triggerEstablishmentUsersWelcomeEmail = async (req, res) => { + try { + let { establishment_id } = req.body; + establishment_id = Number(establishment_id); + + if (!Number.isInteger(establishment_id) || establishment_id <= 0) { + return res.status(400).json({ + status: "error", + message: "Invalid establishment_id", + }); + } + + const establishment = await Establishment.findByPk(establishment_id); + if (!establishment) { + return res.status(404).json({ + status: "error", + message: "Establishment not found", + }); + } + + const users = await EstablishmentUser.findAll({ + where: { + establishment_id, + is_active: true, + }, + }); + + for (const user of users) { + // Only for users who never logged in + if (user.last_login === null) { + + // ✅ Generate random 8 character password + const password = crypto.randomBytes(4).toString("hex"); // 8 chars + + const hashedPassword = await bcrypt.hash(password, 10); + + // ✅ Update password + await EstablishmentUser.update( + { password: hashedPassword }, + { where: { id: user.id } } + ); + + // ✅ Send email + await sendEmailService( + user.email, + "establishment_user_creation_to_user", + { + contact_name: user.name, + portal_url: process.env.FE_BASE_URL, + username: user.email, + password: password, + support_email: process.env.SUPPORT_EMAIL, + support_phone: process.env.SUPPORT_PHONE, + } + ); + } + } + + return res.status(200).json({ + status: "success", + message: "Email sent successfully.", + }); + + } catch (error) { + console.error(error); + return res.status(500).json({ + status: "error", + message: error.message, + }); + } +}; + diff --git a/app/routes/routes.js b/app/routes/routes.js index 6796ab4..55ff84a 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -965,6 +965,37 @@ router.put("/establishment-users/:id/change-password",[verifySignature, verifyTo */ router.delete("/establishment-users/:id",[verifySignature, verifyToken], establishmentUserController.deleteUser); +/** + * @swagger + * /api/trigger-establishment-users-welcome-email: + * post: + * summary: Sending Establishment User Welcome Email + * tags: [Establishment Users] + * security: + * - appSignature: [] + * cookieAuth: [] # or bearerAuth: [] if you use Authorization header + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - establishment_id + * properties: + * establishment_id: + * type: integer + * example: 1 + * responses: + * 200: + * description: Email send success + * 400: + * description: Missing or invalid fields + * 500: + * description: Server error + */ +router.post("/trigger-establishment-users-welcome-email",[verifySignature, verifyToken], establishmentUserController.triggerEstablishmentUsersWelcomeEmail); +