add invite service
This commit is contained in:
1 parent
3239f555a1
commit
aea79fec2e
5 files changed
+115
-61
No files matched your search
@@ -1,41 +1,46 @@
|
|||||||
import { inviteRoleValidation, inviteBodyValidation, inviteVerificationBodyValidation } from "../validation/joi.js";
|
import { inviteBodyValidation, inviteVerificationBodyValidation } from "../validation/joi.js";
|
||||||
import jwt from "jsonwebtoken";
|
import { asyncHandler } from "../utils/errorUtils.js";
|
||||||
import { getTokenFromHeaders } from "../utils/utils.js";
|
|
||||||
import { asyncHandler, createServerError } from "../utils/errorUtils.js";
|
|
||||||
|
|
||||||
const SERVICE_NAME = "inviteController";
|
const SERVICE_NAME = "inviteController";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller for handling user invitation operations
|
||||||
|
* Manages invite token generation, email sending, and token verification
|
||||||
|
*/
|
||||||
class InviteController {
|
class InviteController {
|
||||||
constructor(db, settingsService, emailService, stringService) {
|
/**
|
||||||
|
* Creates a new InviteController instance
|
||||||
|
* @param {Object} dependencies - Dependencies injected into the controller
|
||||||
|
* @param {Object} dependencies.db - Database connection instance
|
||||||
|
* @param {Object} dependencies.settingsService - Service for managing application settings
|
||||||
|
* @param {Object} dependencies.emailService - Service for sending emails
|
||||||
|
* @param {Object} dependencies.stringService - Service for internationalized strings
|
||||||
|
* @param {Object} dependencies.inviteService - Service for invite-related operations
|
||||||
|
*/
|
||||||
|
constructor({ db, settingsService, emailService, stringService, inviteService }) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.stringService = stringService;
|
this.stringService = stringService;
|
||||||
|
this.inviteService = inviteService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issues an invitation to a new user. Only admins can invite new users. An invitation token is created and sent via email.
|
* Generates an invite token for a user invitation
|
||||||
* @async
|
* @param {Object} req - Express request object
|
||||||
* @param {Object} req - The Express request object.
|
* @param {Object} req.body - Request body containing invite details
|
||||||
* @property {Object} req.headers - The headers of the request.
|
* @param {Object} req.user - Authenticated user object
|
||||||
* @property {string} req.headers.authorization - The authorization header containing the JWT token.
|
* @param {string} req.user.teamId - Team ID of the authenticated user
|
||||||
* @property {Object} req.body - The body of the request.
|
* @param {Object} res - Express response object
|
||||||
* @property {string} req.body.email - The email of the user to be invited.
|
* @returns {Promise<Object>} Response with invite token data
|
||||||
* @param {Object} res - The Express response object.
|
|
||||||
* @param {function} next - The next middleware function.
|
|
||||||
* @returns {Object} The response object with a success status, a message indicating the sending of the invitation, and the invitation token.
|
|
||||||
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
|
|
||||||
*/
|
*/
|
||||||
getInviteToken = asyncHandler(
|
getInviteToken = asyncHandler(
|
||||||
async (req, res, next) => {
|
async (req, res) => {
|
||||||
// Only admins can invite
|
const invite = req.body;
|
||||||
const token = getTokenFromHeaders(req.headers);
|
const teamId = req?.user?.teamId;
|
||||||
const { role, teamId } = jwt.decode(token);
|
invite.teamId = teamId;
|
||||||
req.body.teamId = teamId;
|
await inviteBodyValidation.validateAsync(invite);
|
||||||
await inviteRoleValidation.validateAsync({ roles: role });
|
const inviteToken = await this.inviteService.getInviteToken({ invite, teamId });
|
||||||
await inviteBodyValidation.validateAsync(req.body);
|
|
||||||
|
|
||||||
const inviteToken = await this.db.requestInviteToken({ ...req.body });
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.inviteIssued,
|
msg: this.stringService.inviteIssued,
|
||||||
data: inviteToken,
|
data: inviteToken,
|
||||||
@@ -45,27 +50,26 @@ class InviteController {
|
|||||||
"getInviteToken"
|
"getInviteToken"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends an invitation email to a user
|
||||||
|
* @param {Object} req - Express request object
|
||||||
|
* @param {Object} req.body - Request body containing invite details
|
||||||
|
* @param {Object} req.user - Authenticated user object
|
||||||
|
* @param {string} req.user.teamId - Team ID of the authenticated user
|
||||||
|
* @param {string} req.user.firstName - First name of the authenticated user
|
||||||
|
* @param {Object} res - Express response object
|
||||||
|
* @returns {Promise<Object>} Response with invite token data
|
||||||
|
*/
|
||||||
sendInviteEmail = asyncHandler(
|
sendInviteEmail = asyncHandler(
|
||||||
async (req, res, next) => {
|
async (req, res) => {
|
||||||
// Only admins can invite
|
const inviteRequest = req.body;
|
||||||
const token = getTokenFromHeaders(req.headers);
|
inviteRequest.teamId = req?.user?.teamId;
|
||||||
const { role, firstname, teamId } = jwt.decode(token);
|
await inviteBodyValidation.validateAsync(inviteRequest);
|
||||||
req.body.teamId = teamId;
|
|
||||||
await inviteRoleValidation.validateAsync({ roles: role });
|
|
||||||
await inviteBodyValidation.validateAsync(req.body);
|
|
||||||
|
|
||||||
const inviteToken = await this.db.requestInviteToken({ ...req.body });
|
const inviteToken = await this.inviteService.sendInviteEmail({
|
||||||
const { clientHost } = this.settingsService.getSettings();
|
inviteRequest,
|
||||||
|
firstName: req?.user?.firstName,
|
||||||
const html = await this.emailService.buildEmail("employeeActivationTemplate", {
|
|
||||||
name: firstname,
|
|
||||||
link: `${clientHost}/register/${inviteToken.token}`,
|
|
||||||
});
|
});
|
||||||
const result = await this.emailService.sendEmail(req.body.email, "Welcome to Uptime Monitor", html);
|
|
||||||
if (!result) {
|
|
||||||
throw createServerError("Failed to send invite e-mail... Please verify your settings.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.inviteIssued,
|
msg: this.stringService.inviteIssued,
|
||||||
data: inviteToken,
|
data: inviteToken,
|
||||||
@@ -75,17 +79,25 @@ class InviteController {
|
|||||||
"sendInviteEmail"
|
"sendInviteEmail"
|
||||||
);
|
);
|
||||||
|
|
||||||
inviteVerifyController = asyncHandler(
|
/**
|
||||||
async (req, res, next) => {
|
* Verifies an invite token and returns invite details
|
||||||
|
* @param {Object} req - Express request object
|
||||||
|
* @param {Object} req.body - Request body containing the invite token
|
||||||
|
* @param {string} req.body.token - The invite token to verify
|
||||||
|
* @param {Object} res - Express response object
|
||||||
|
* @returns {Promise<Object>} Response with verified invite data
|
||||||
|
*/
|
||||||
|
verifyInviteToken = asyncHandler(
|
||||||
|
async (req, res) => {
|
||||||
await inviteVerificationBodyValidation.validateAsync(req.body);
|
await inviteVerificationBodyValidation.validateAsync(req.body);
|
||||||
const invite = await this.db.getInviteToken(req.body.token);
|
const invite = await this.inviteService.verifyInviteToken({ inviteToken: req?.body?.token });
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.inviteVerified,
|
msg: this.stringService.inviteVerified,
|
||||||
data: invite,
|
data: invite,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
SERVICE_NAME,
|
SERVICE_NAME,
|
||||||
"inviteVerifyController"
|
"verifyInviteToken"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-7
@@ -61,6 +61,7 @@ import SuperSimpleQueueHelper from "./service/infrastructure/SuperSimpleQueue/Su
|
|||||||
import UserService from "./service/business/userService.js";
|
import UserService from "./service/business/userService.js";
|
||||||
import CheckService from "./service/business/checkService.js";
|
import CheckService from "./service/business/checkService.js";
|
||||||
import DiagnosticService from "./service/business/diagnosticService.js";
|
import DiagnosticService from "./service/business/diagnosticService.js";
|
||||||
|
import InviteService from "./service/business/inviteService.js";
|
||||||
|
|
||||||
//Network service and dependencies
|
//Network service and dependencies
|
||||||
import NetworkService from "./service/infrastructure/networkService.js";
|
import NetworkService from "./service/infrastructure/networkService.js";
|
||||||
@@ -198,7 +199,12 @@ const startApp = async () => {
|
|||||||
stringService,
|
stringService,
|
||||||
});
|
});
|
||||||
const diagnosticService = new DiagnosticService();
|
const diagnosticService = new DiagnosticService();
|
||||||
|
const inviteService = new InviteService({
|
||||||
|
db,
|
||||||
|
settingsService,
|
||||||
|
emailService,
|
||||||
|
stringService,
|
||||||
|
});
|
||||||
// const jobQueueHelper = new JobQueueHelper({
|
// const jobQueueHelper = new JobQueueHelper({
|
||||||
// redisService,
|
// redisService,
|
||||||
// Queue,
|
// Queue,
|
||||||
@@ -304,12 +310,13 @@ const startApp = async () => {
|
|||||||
checkService: ServiceRegistry.get(CheckService.SERVICE_NAME),
|
checkService: ServiceRegistry.get(CheckService.SERVICE_NAME),
|
||||||
});
|
});
|
||||||
|
|
||||||
const inviteController = new InviteController(
|
const inviteController = new InviteController({
|
||||||
ServiceRegistry.get(MongoDB.SERVICE_NAME),
|
db: ServiceRegistry.get(MongoDB.SERVICE_NAME),
|
||||||
ServiceRegistry.get(SettingsService.SERVICE_NAME),
|
settingsService: ServiceRegistry.get(SettingsService.SERVICE_NAME),
|
||||||
ServiceRegistry.get(EmailService.SERVICE_NAME),
|
emailService: ServiceRegistry.get(EmailService.SERVICE_NAME),
|
||||||
ServiceRegistry.get(StringService.SERVICE_NAME)
|
stringService: ServiceRegistry.get(StringService.SERVICE_NAME),
|
||||||
);
|
inviteService,
|
||||||
|
});
|
||||||
|
|
||||||
const maintenanceWindowController = new MaintenanceWindowController(
|
const maintenanceWindowController = new MaintenanceWindowController(
|
||||||
ServiceRegistry.get(MongoDB.SERVICE_NAME),
|
ServiceRegistry.get(MongoDB.SERVICE_NAME),
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ class InviteRoutes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initRoutes() {
|
initRoutes() {
|
||||||
this.router.post("/send", this.inviteController.sendInviteEmail);
|
this.router.post("/send", verifyJWT, isAllowed(["admin", "superadmin"]), this.inviteController.sendInviteEmail);
|
||||||
this.router.post("/verify", this.inviteController.inviteVerifyController);
|
this.router.post("/verify", this.inviteController.verifyInviteToken);
|
||||||
this.router.post("/", verifyJWT, isAllowed(["admin", "superadmin"]), this.inviteController.getInviteToken);
|
this.router.post("/", verifyJWT, isAllowed(["admin", "superadmin"]), this.inviteController.getInviteToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
const SERVICE_NAME = "inviteService";
|
||||||
|
import { createServerError } from "../../utils/errorUtils.js";
|
||||||
|
|
||||||
|
class InviteService {
|
||||||
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
|
constructor({ db, settingsService, emailService, stringService }) {
|
||||||
|
this.db = db;
|
||||||
|
this.settingsService = settingsService;
|
||||||
|
this.emailService = emailService;
|
||||||
|
this.stringService = stringService;
|
||||||
|
}
|
||||||
|
|
||||||
|
getInviteToken = async ({ invite, teamId }) => {
|
||||||
|
invite.teamId = teamId;
|
||||||
|
const inviteToken = await this.db.requestInviteToken(invite);
|
||||||
|
return inviteToken;
|
||||||
|
};
|
||||||
|
|
||||||
|
sendInviteEmail = async ({ inviteRequest, firstName }) => {
|
||||||
|
const inviteToken = await this.db.requestInviteToken({ ...inviteRequest });
|
||||||
|
const { clientHost } = this.settingsService.getSettings();
|
||||||
|
|
||||||
|
const html = await this.emailService.buildEmail("employeeActivationTemplate", {
|
||||||
|
name: firstName,
|
||||||
|
link: `${clientHost}/register/${inviteToken.token}`,
|
||||||
|
});
|
||||||
|
const result = await this.emailService.sendEmail(inviteRequest.email, "Welcome to Uptime Monitor", html);
|
||||||
|
if (!result) {
|
||||||
|
throw createServerError("Failed to send invite e-mail... Please verify your settings.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
verifyInviteToken = async ({ inviteToken }) => {
|
||||||
|
const invite = await this.db.getInviteToken(inviteToken);
|
||||||
|
return invite;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default InviteService;
|
||||||
@@ -87,10 +87,6 @@ const deleteUserParamValidation = joi.object({
|
|||||||
email: joi.string().email().required(),
|
email: joi.string().email().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const inviteRoleValidation = joi.object({
|
|
||||||
roles: joi.custom(roleValidatior(["admin", "superadmin"])).required(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const inviteBodyValidation = joi.object({
|
const inviteBodyValidation = joi.object({
|
||||||
email: joi.string().trim().email().required().messages({
|
email: joi.string().trim().email().required().messages({
|
||||||
"string.empty": "Email is required",
|
"string.empty": "Email is required",
|
||||||
@@ -652,7 +648,6 @@ export {
|
|||||||
recoveryValidation,
|
recoveryValidation,
|
||||||
recoveryTokenBodyValidation,
|
recoveryTokenBodyValidation,
|
||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
inviteRoleValidation,
|
|
||||||
inviteBodyValidation,
|
inviteBodyValidation,
|
||||||
inviteVerificationBodyValidation,
|
inviteVerificationBodyValidation,
|
||||||
createMonitorBodyValidation,
|
createMonitorBodyValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user