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 jwt from "jsonwebtoken";
|
||||
import { getTokenFromHeaders } from "../utils/utils.js";
|
||||
import { asyncHandler, createServerError } from "../utils/errorUtils.js";
|
||||
import { inviteBodyValidation, inviteVerificationBodyValidation } from "../validation/joi.js";
|
||||
import { asyncHandler } from "../utils/errorUtils.js";
|
||||
|
||||
const SERVICE_NAME = "inviteController";
|
||||
|
||||
/**
|
||||
* Controller for handling user invitation operations
|
||||
* Manages invite token generation, email sending, and token verification
|
||||
*/
|
||||
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.settingsService = settingsService;
|
||||
this.emailService = emailService;
|
||||
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.
|
||||
* @async
|
||||
* @param {Object} req - The Express request object.
|
||||
* @property {Object} req.headers - The headers of the request.
|
||||
* @property {string} req.headers.authorization - The authorization header containing the JWT token.
|
||||
* @property {Object} req.body - The body of the request.
|
||||
* @property {string} req.body.email - The email of the user to be invited.
|
||||
* @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).
|
||||
* Generates an invite token for a user invitation
|
||||
* @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 {Object} res - Express response object
|
||||
* @returns {Promise<Object>} Response with invite token data
|
||||
*/
|
||||
getInviteToken = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
// Only admins can invite
|
||||
const token = getTokenFromHeaders(req.headers);
|
||||
const { role, teamId } = jwt.decode(token);
|
||||
req.body.teamId = teamId;
|
||||
await inviteRoleValidation.validateAsync({ roles: role });
|
||||
await inviteBodyValidation.validateAsync(req.body);
|
||||
|
||||
const inviteToken = await this.db.requestInviteToken({ ...req.body });
|
||||
async (req, res) => {
|
||||
const invite = req.body;
|
||||
const teamId = req?.user?.teamId;
|
||||
invite.teamId = teamId;
|
||||
await inviteBodyValidation.validateAsync(invite);
|
||||
const inviteToken = await this.inviteService.getInviteToken({ invite, teamId });
|
||||
return res.success({
|
||||
msg: this.stringService.inviteIssued,
|
||||
data: inviteToken,
|
||||
@@ -45,27 +50,26 @@ class InviteController {
|
||||
"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(
|
||||
async (req, res, next) => {
|
||||
// Only admins can invite
|
||||
const token = getTokenFromHeaders(req.headers);
|
||||
const { role, firstname, teamId } = jwt.decode(token);
|
||||
req.body.teamId = teamId;
|
||||
await inviteRoleValidation.validateAsync({ roles: role });
|
||||
await inviteBodyValidation.validateAsync(req.body);
|
||||
async (req, res) => {
|
||||
const inviteRequest = req.body;
|
||||
inviteRequest.teamId = req?.user?.teamId;
|
||||
await inviteBodyValidation.validateAsync(inviteRequest);
|
||||
|
||||
const inviteToken = await this.db.requestInviteToken({ ...req.body });
|
||||
const { clientHost } = this.settingsService.getSettings();
|
||||
|
||||
const html = await this.emailService.buildEmail("employeeActivationTemplate", {
|
||||
name: firstname,
|
||||
link: `${clientHost}/register/${inviteToken.token}`,
|
||||
const inviteToken = await this.inviteService.sendInviteEmail({
|
||||
inviteRequest,
|
||||
firstName: req?.user?.firstName,
|
||||
});
|
||||
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({
|
||||
msg: this.stringService.inviteIssued,
|
||||
data: inviteToken,
|
||||
@@ -75,17 +79,25 @@ class InviteController {
|
||||
"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);
|
||||
const invite = await this.db.getInviteToken(req.body.token);
|
||||
const invite = await this.inviteService.verifyInviteToken({ inviteToken: req?.body?.token });
|
||||
return res.success({
|
||||
msg: this.stringService.inviteVerified,
|
||||
data: invite,
|
||||
});
|
||||
},
|
||||
SERVICE_NAME,
|
||||
"inviteVerifyController"
|
||||
"verifyInviteToken"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user