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

+6 -2
View File
@@ -25,7 +25,11 @@ const Account = ({ open = "profile" }) => {
let tabList = ["Profile", "Password", "Team"]; let tabList = ["Profile", "Password", "Team"];
const { user } = useSelector((state) => state.auth); const { user } = useSelector((state) => state.auth);
if (!user.role.includes("admin")) tabList = ["Profile", "Password"]; const requiredRoles = ["superadmin", "admin"];
const hideTeams = !requiredRoles.some((role) => user.role.includes(role));
if (hideTeams) {
tabList = ["Profile", "Password"];
}
return ( return (
<Box <Box
@@ -71,7 +75,7 @@ const Account = ({ open = "profile" }) => {
</Box> </Box>
<ProfilePanel /> <ProfilePanel />
<PasswordPanel /> <PasswordPanel />
{user.role.includes("admin") && <TeamPanel />} {!hideTeams && <TeamPanel />}
</TabContext> </TabContext>
</Box> </Box>
); );
+5 -4
View File
@@ -444,24 +444,26 @@ const Register = ({ isSuperAdmin }) => {
password: "", password: "",
confirm: "", confirm: "",
role: [], role: [],
teamId: "",
}); });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
const [step, setStep] = useState(0); const [step, setStep] = useState(0);
useEffect(() => { useEffect(() => {
const fetchInvite = async () => { const fetchInvite = async () => {
console.log("FETCHING");
if (token !== undefined) { if (token !== undefined) {
try { try {
const res = await networkService.verifyInvitationToken(token); const res = await networkService.verifyInvitationToken(token);
const { role, email } = res.data.data; const { role, email, teamId } = res.data.data;
setForm({ ...form, email, role }); setForm({ ...form, email, role, teamId });
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
} }
} }
}; };
fetchInvite(); fetchInvite();
}, [token, form]); }, []);
/** /**
* Validates the form data against the validation schema. * Validates the form data against the validation schema.
@@ -494,7 +496,6 @@ const Register = ({ isSuperAdmin }) => {
const handleStepOne = async (e) => { const handleStepOne = async (e) => {
e.preventDefault(); e.preventDefault();
let error = validateForm({ let error = validateForm({
firstName: form.firstName, firstName: form.firstName,
lastName: form.lastName, lastName: form.lastName,
+1
View File
@@ -76,6 +76,7 @@ const credentials = joi.object({
return value; return value;
}), }),
role: joi.array(), role: joi.array(),
teamId: joi.string().allow("").optional(),
}); });
const monitorValidation = joi.object({ const monitorValidation = joi.object({
+2 -1
View File
@@ -226,7 +226,8 @@ const inviteController = async (req, res, next) => {
try { try {
// Only admins can invite // Only admins can invite
const token = getTokenFromHeaders(req.headers); const token = getTokenFromHeaders(req.headers);
const { role, firstname } = jwt.decode(token); const { role, firstname, teamId } = jwt.decode(token);
req.body.teamId = teamId;
try { try {
await inviteRoleValidation.validateAsync({ roles: role }); await inviteRoleValidation.validateAsync({ roles: role });
await inviteBodyValidation.validateAsync(req.body); await inviteBodyValidation.validateAsync(req.body);
+1
View File
@@ -8,6 +8,7 @@ const requestInviteToken = async (req, res) => {
let inviteToken = new InviteToken({ let inviteToken = new InviteToken({
email: req.body.email, email: req.body.email,
role: req.body.role, role: req.body.role,
teamId: req.body.teamId,
token: crypto.randomBytes(32).toString("hex"), token: crypto.randomBytes(32).toString("hex"),
}); });
await inviteToken.save(); await inviteToken.save();
+2
View File
@@ -37,6 +37,8 @@ const insertUser = async (req, res) => {
}); });
teamId = team._id; teamId = team._id;
await team.save(); await team.save();
} else {
teamId = userData.teamId;
} }
const newUser = new UserModel(userData); const newUser = new UserModel(userData);
+6
View File
@@ -7,6 +7,12 @@ const InviteTokenSchema = mongoose.Schema(
required: true, required: true,
unique: true, unique: true,
}, },
teamId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Team",
immutable: true,
required: true,
},
role: { role: {
type: Array, type: Array,
required: true, required: true,
+3 -1
View File
@@ -5,7 +5,7 @@ const joi = require("joi");
//**************************************** //****************************************
const roleValidatior = (role) => (value, helpers) => { const roleValidatior = (role) => (value, helpers) => {
const hasRole = roles.some((role) => value.includes(role)); const hasRole = role.some((role) => value.includes(role));
if (!hasRole) { if (!hasRole) {
throw new Joi.ValidationError( throw new Joi.ValidationError(
`You do not have the required authorization. Required roles: ${roles.join(", ")}` `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")) .items(joi.string().valid("superadmin", "admin", "user"))
.min(1) .min(1)
.required(), .required(),
teamId: joi.string().allow("").required(),
}); });
const editUserParamValidation = joi.object({ const editUserParamValidation = joi.object({
@@ -135,6 +136,7 @@ const inviteBodyValidation = joi.object({
"string.email": "Must be a valid email address", "string.email": "Must be a valid email address",
}), }),
role: joi.array().required(), role: joi.array().required(),
teamId: joi.string().required(),
}); });
const inviteVerifciationBodyValidation = joi.object({ const inviteVerifciationBodyValidation = joi.object({