Establishment user welcome email : GWM
This commit is contained in:
parent
3a6c3c6c8f
commit
f64cb79c13
@ -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,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user