Add teams to a user when they are created

This commit is contained in:
Alex Holliday committed 2024-08-26 13:29:11 -07:00
1 parent a6f5c96f93
commit c39eab49bd
8 files changed
+26 -8

No files matched your search

+2 -1
View File
@@ -226,7 +226,8 @@ const inviteController = async (req, res, next) => {
try {
// Only admins can invite
const token = getTokenFromHeaders(req.headers);
const { role, firstname } = jwt.decode(token);
const { role, firstname, teamId } = jwt.decode(token);
req.body.teamId = teamId;
try {
await inviteRoleValidation.validateAsync({ roles: role });
await inviteBodyValidation.validateAsync(req.body);
+1
View File
@@ -8,6 +8,7 @@ const requestInviteToken = async (req, res) => {
let inviteToken = new InviteToken({
email: req.body.email,
role: req.body.role,
teamId: req.body.teamId,
token: crypto.randomBytes(32).toString("hex"),
});
await inviteToken.save();
+2
View File
@@ -37,6 +37,8 @@ const insertUser = async (req, res) => {
});
teamId = team._id;
await team.save();
} else {
teamId = userData.teamId;
}
const newUser = new UserModel(userData);
+6
View File
@@ -7,6 +7,12 @@ const InviteTokenSchema = mongoose.Schema(
required: true,
unique: true,
},
teamId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Team",
immutable: true,
required: true,
},
role: {
type: Array,
required: true,
+3 -1
View File
@@ -5,7 +5,7 @@ const joi = require("joi");
//****************************************
const roleValidatior = (role) => (value, helpers) => {
const hasRole = roles.some((role) => value.includes(role));
const hasRole = role.some((role) => value.includes(role));
if (!hasRole) {
throw new Joi.ValidationError(
`You do not have the required authorization. Required roles: ${roles.join(", ")}`
@@ -72,6 +72,7 @@ const registerValidation = joi.object({
.items(joi.string().valid("superadmin", "admin", "user"))
.min(1)
.required(),
teamId: joi.string().allow("").required(),
});
const editUserParamValidation = joi.object({
@@ -135,6 +136,7 @@ const inviteBodyValidation = joi.object({
"string.email": "Must be a valid email address",
}),
role: joi.array().required(),
teamId: joi.string().required(),
});
const inviteVerifciationBodyValidation = joi.object({