Add invite endpoint, add validation to invit endpoint
This commit is contained in:
1 parent
52ac2792c0
commit
428e018aff
4 files changed
+55
-1875
No files matched your search
Generated
+11
-1872
File diff suppressed because it is too large.
Load diff
@@ -25,7 +25,6 @@
|
|||||||
"dayjs": "1.11.11",
|
"dayjs": "1.11.11",
|
||||||
"joi": "17.13.1",
|
"joi": "17.13.1",
|
||||||
"jwt-decode": "^4.0.0",
|
"jwt-decode": "^4.0.0",
|
||||||
"mjml-react": "^2.0.8",
|
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
"react-redux": "9.1.2",
|
"react-redux": "9.1.2",
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ const {
|
|||||||
recoveryTokenValidation,
|
recoveryTokenValidation,
|
||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
deleteUserParamValidation,
|
deleteUserParamValidation,
|
||||||
|
inviteRoleValidation,
|
||||||
|
inviteBodyValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
@@ -214,8 +216,19 @@ const userEditController = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const inviteController = async (req, res) => {
|
const inviteController = async (req, res, next) => {
|
||||||
return res.status(200).json({ success: true, msg: "Invite sent" });
|
try {
|
||||||
|
// Only admins can invite
|
||||||
|
const token = getTokenFromHeaders(req.headers);
|
||||||
|
const { role } = jwt.decode(token);
|
||||||
|
await inviteRoleValidation.validateAsync({ roles: role });
|
||||||
|
await inviteBodyValidation.validateAsync(req.body);
|
||||||
|
return res.status(200).json({ success: true, msg: "Invite sent" });
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,17 @@
|
|||||||
const joi = require("joi");
|
const joi = require("joi");
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Custom Validators
|
||||||
|
//****************************************
|
||||||
|
|
||||||
|
const roleValidatior = (role) => (value, helpers) => {
|
||||||
|
console.log(role);
|
||||||
|
if (!value.includes(role)) {
|
||||||
|
throw new joi.ValidationError(`You do not have ${role} authorization`);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Auth
|
// Auth
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -87,6 +99,20 @@ const deleteUserParamValidation = joi.object({
|
|||||||
email: joi.string().email().required(),
|
email: joi.string().email().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const inviteRoleValidation = joi.object({
|
||||||
|
roles: joi.custom(roleValidatior("admin")).required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const inviteBodyValidation = joi.object({
|
||||||
|
email: joi.string().trim().email().required().messages({
|
||||||
|
"string.empty": "Email is required",
|
||||||
|
"string.email": "Must be a valid email address",
|
||||||
|
}),
|
||||||
|
role: joi.string().required().messages({
|
||||||
|
"string.empty": "Role is required",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Monitors
|
// Monitors
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -195,11 +221,14 @@ const deletePageSpeedCheckParamValidation = joi.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
roleValidatior,
|
||||||
loginValidation,
|
loginValidation,
|
||||||
registerValidation,
|
registerValidation,
|
||||||
recoveryValidation,
|
recoveryValidation,
|
||||||
recoveryTokenValidation,
|
recoveryTokenValidation,
|
||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
|
inviteRoleValidation,
|
||||||
|
inviteBodyValidation,
|
||||||
getMonitorByIdValidation,
|
getMonitorByIdValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
monitorValidation,
|
monitorValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user