refactor invite to its own route and controller

This commit is contained in:
Alex Holliday committed 2024-08-26 17:43:56 -07:00
1 parent 3e63a32669
commit ac37211e32
10 files changed
+161 -35

No files matched your search

+30 -10
View File
@@ -2,15 +2,25 @@ const InviteToken = require("../../../models/InviteToken");
const crypto = require("crypto");
const { errorMessages } = require("../../../utils/messages");
const requestInviteToken = async (req, res) => {
/**
* Request an invite token for a user.
*
* This function deletes any existing invite tokens for the user's email,
* generates a new token, saves it, and then returns the new token.
*
* @param {Object} userData - The user data.
* @param {string} userData.email - The user's email.
* @param {mongoose.Schema.Types.ObjectId} userData.teamId - The ID of the team.
* @param {Array} userData.role - The user's role(s).
* @param {Date} [userData.expiry=Date.now] - The expiry date of the token. Defaults to the current date and time.
* @returns {Promise<InviteToken>} The invite token.
* @throws {Error} If there is an error.
*/
const requestInviteToken = async (userData) => {
try {
await InviteToken.deleteMany({ email: req.body.email });
let inviteToken = new InviteToken({
email: req.body.email,
role: req.body.role,
teamId: req.body.teamId,
token: crypto.randomBytes(32).toString("hex"),
});
await InviteToken.deleteMany({ email: userData.email });
userData.token = crypto.randomBytes(32).toString("hex");
let inviteToken = new InviteToken(userData);
await inviteToken.save();
return inviteToken;
} catch (error) {
@@ -18,10 +28,20 @@ const requestInviteToken = async (req, res) => {
}
};
const getInviteToken = async (req, res) => {
/**
* Retrieves and deletes an invite token.
*
* This function searches for an invite token in the database and deletes it.
* If the invite token is not found, it throws an error.
*
* @param {string} token - The invite token to search for.
* @returns {Promise<InviteToken>} The invite token data.
* @throws {Error} If the invite token is not found or there is another error.
*/
const getInviteToken = async (token) => {
try {
const invite = await InviteToken.findOneAndDelete({
token: req.body.token,
token,
});
if (invite === null) {
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND);
+7 -12
View File
@@ -14,35 +14,30 @@ const { ParseBoolean } = require("../../../utils/utils");
* @returns {Promise<UserModel>}
* @throws {Error}
*/
const insertUser = async (req, res) => {
const insertUser = async (userData, imageFile) => {
try {
const userData = { ...req.body };
if (req.file) {
if (imageFile) {
// 1. Save the full size image
userData.profileImage = {
data: req.file.buffer,
contentType: req.file.mimetype,
data: imageFile.buffer,
contentType: imageFile.mimetype,
};
// 2. Get the avatar sized image
const avatar = await GenerateAvatarImage(req.file);
const avatar = await GenerateAvatarImage(imageFile);
userData.avatarImage = avatar;
}
let teamId;
// Is user superadmin? If so, create team
// Handle creating team if superadmin
if (userData.role.includes("superadmin")) {
const team = new TeamModel({
email: userData.email,
});
teamId = team._id;
userData.teamId = team._id;
await team.save();
} else {
teamId = userData.teamId;
}
const newUser = new UserModel(userData);
newUser.teamId = teamId;
await newUser.save();
return await UserModel.findOne({ _id: newUser._id })
.select("-password")