IO-2782-Send-Promanager-Welcome-Email - Send ProManager welcome email
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -96,6 +96,20 @@ const sendServerEmail = async ({ subject, text }) => {
|
||||
}
|
||||
};
|
||||
|
||||
const sendProManagerWelcomeEmail = async (to, subject, html) => {
|
||||
try {
|
||||
await transporter.sendMail({
|
||||
from: `ProManager <noreply@promanager.web-est.com>`,
|
||||
to,
|
||||
subject,
|
||||
html
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
logger.log("server-email-failure", "error", null, null, error);
|
||||
}
|
||||
};
|
||||
|
||||
const sendTaskEmail = async ({ to, subject, text, attachments }) => {
|
||||
try {
|
||||
transporter.sendMail(
|
||||
@@ -309,5 +323,6 @@ module.exports = {
|
||||
sendEmail,
|
||||
sendServerEmail,
|
||||
sendTaskEmail,
|
||||
sendProManagerWelcomeEmail,
|
||||
emailBounce
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("../utils/logger");
|
||||
const path = require("path");
|
||||
const { auth } = require("firebase-admin");
|
||||
const { sendProManagerWelcomeEmail } = require("../email/sendemail");
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
||||
@@ -10,6 +10,7 @@ const client = require("../graphql-client/graphql-client").client;
|
||||
|
||||
const serviceAccount = require(process.env.FIREBASE_ADMINSDK_JSON);
|
||||
const adminEmail = require("../utils/adminEmail");
|
||||
const generateEmailTemplate = require("../email/generateTemplate");
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
@@ -24,7 +25,8 @@ exports.createUser = async (req, res) => {
|
||||
ioadmin: true
|
||||
});
|
||||
|
||||
const { email, displayName, password, shopid, authlevel } = req.body;
|
||||
const { email, displayName, password, shopid, authlevel, validemail } = req.body;
|
||||
|
||||
try {
|
||||
const userRecord = await admin.auth().createUser({ email, displayName, password });
|
||||
|
||||
@@ -42,6 +44,7 @@ exports.createUser = async (req, res) => {
|
||||
user: {
|
||||
email: email.toLowerCase(),
|
||||
authid: userRecord.uid,
|
||||
validemail,
|
||||
associations: {
|
||||
data: [{ shopid, authlevel, active: true }]
|
||||
}
|
||||
@@ -58,6 +61,121 @@ exports.createUser = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
exports.promanagerWelcomeEmail = (req, res) => {
|
||||
const { authid, email } = req.body;
|
||||
|
||||
// Gate the operation to only admin users
|
||||
if (!adminEmail.includes(req.user.email) && !req.user.ioadmin) {
|
||||
logger.log("admin-update-user-unauthorized", "ERROR", req.user.email, null, {
|
||||
request: req.body,
|
||||
user: req.user
|
||||
});
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
|
||||
admin
|
||||
.auth()
|
||||
.getUser(authid)
|
||||
.then((userRecord) => {
|
||||
if (!userRecord) {
|
||||
res.status(404).json({ message: "User not found in Firebase." });
|
||||
return Promise.reject("User not found in Firebase.");
|
||||
}
|
||||
|
||||
// Fetch user data from the database using GraphQL
|
||||
return client.request(
|
||||
`
|
||||
query GET_USER_BY_EMAIL($email: String!) {
|
||||
users(where: { email: { _eq: $email } }) {
|
||||
email
|
||||
validemail
|
||||
associations {
|
||||
id
|
||||
shopid
|
||||
bodyshop {
|
||||
id
|
||||
convenient_company
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
{ email: email.toLowerCase() }
|
||||
);
|
||||
})
|
||||
.then((dbUserResult) => {
|
||||
const dbUser = dbUserResult?.users?.[0];
|
||||
if (!dbUser) {
|
||||
res.status(404).json({ message: "User not found in database." });
|
||||
return Promise.reject("User not found in database.");
|
||||
}
|
||||
|
||||
// Check if the email is valid before proceeding
|
||||
if (!dbUser.validemail) {
|
||||
logger.log("admin-send-welcome-email-skip", "ADMIN", req.user.email, null, {
|
||||
message: "User email is not valid, skipping email.",
|
||||
email
|
||||
});
|
||||
return res.status(200).json({ message: "User email is not valid, email not sent." });
|
||||
}
|
||||
|
||||
// Check if convenient_company is equal to "promanager"
|
||||
const convenientCompany = dbUser.associations?.[0]?.bodyshop?.convenient_company;
|
||||
if (convenientCompany !== "promanager") {
|
||||
logger.log("admin-send-welcome-email-skip", "ADMIN", req.user.email, null, {
|
||||
message: `convenient_company is not "promanager", skipping email.`,
|
||||
convenientCompany
|
||||
});
|
||||
return res.status(200).json({ message: `convenient_company is not "promanager", email not sent.` });
|
||||
}
|
||||
|
||||
// Generate password reset link
|
||||
return admin
|
||||
.auth()
|
||||
.generatePasswordResetLink(dbUser.email)
|
||||
.then((resetLink) => ({
|
||||
dbUser,
|
||||
resetLink
|
||||
}));
|
||||
})
|
||||
.then(({ dbUser, resetLink }) => {
|
||||
// Send email logic here (replace this with your email-sending service)
|
||||
return sendProManagerWelcomeEmail({
|
||||
to: dbUser.email,
|
||||
subject: "Welcome to the ProManager platform.",
|
||||
html: generateEmailTemplate({
|
||||
header: "",
|
||||
subHeader: "",
|
||||
body: `
|
||||
<p>Welcome to the ProManager platform. Please click the link below to reset your password:</p>
|
||||
<p><a href="${resetLink}">Reset your password</a></p>
|
||||
<p>User Details:</p>
|
||||
<ul>
|
||||
<li>Email: ${dbUser.email}</li>
|
||||
</ul>
|
||||
`
|
||||
})
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
logger.log("admin-send-welcome-email", "ADMIN", req.user.email, null, {
|
||||
request: req.body,
|
||||
ioadmin: true,
|
||||
emailSentTo: email
|
||||
});
|
||||
res.status(200).json({ message: "Welcome email sent successfully." });
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.log("admin-send-welcome-email-error", "ERROR", req.user.email, null, {
|
||||
error
|
||||
});
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({ message: "Error sending welcome email.", error });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
exports.updateUser = (req, res) => {
|
||||
logger.log("admin-update-user", "ADMIN", req.user.email, null, {
|
||||
request: req.body,
|
||||
|
||||
@@ -14,5 +14,6 @@ router.post("/updatecounter", validateAdminMiddleware, updateCounter);
|
||||
router.post("/updateuser", fb.updateUser);
|
||||
router.post("/getuser", fb.getUser);
|
||||
router.post("/createuser", fb.createUser);
|
||||
router.post("/promanagerwelcome", fb.promanagerWelcomeEmail);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user