This commit is contained in:
@@ -3,6 +3,73 @@ const Employee = db.employee;
|
||||
|
||||
var bcrypt = require("bcryptjs");
|
||||
const { splitSite } = require("../middlewares");
|
||||
|
||||
const ALL_PERMISSIONS = [
|
||||
'Dashboard',
|
||||
'Admin View',
|
||||
'View_Info Screen',
|
||||
'Edit_Info Screen',
|
||||
'View_Customer Info _Personal Info',
|
||||
'View_Customer Info _Care & Services',
|
||||
'View_Customer Info _Medical & Insurance',
|
||||
'View_Customer Info _Confidential Details',
|
||||
'View_Customer Info _Form Submission',
|
||||
'Edit_Customer Info _ Personal Info',
|
||||
'Edit_Customer Info _ Care & Services',
|
||||
'Edit_Customer Info _ Medical & Insurance',
|
||||
'Edit_Customer Info _ Confidential Details',
|
||||
'Edit_Customer Info _ Form Submission',
|
||||
'Discharge_Customer',
|
||||
'Reactivate_Customer',
|
||||
'Create_Customer',
|
||||
'Export_Customer Report',
|
||||
'View _Calendar _Medical Appointment',
|
||||
'View _Calendar _Activities',
|
||||
'View _Calendar _Attendance Notes',
|
||||
'View _Calendar _Meal Plan',
|
||||
'View _Calendar _Important Dates',
|
||||
'Edit&Create _Calendar _Medical Appointment',
|
||||
'Edit&Create _Calendar _Activities',
|
||||
'Edit&Create _Calendar _Attendance Notes',
|
||||
'Edit&Create _Calendar _Meal Plan',
|
||||
'Edit&Create _Calendar _Important Dates',
|
||||
'View_Messaging',
|
||||
'Sent_Messaging',
|
||||
'View_Messaging Template',
|
||||
'Create&Edit_Messaging Template',
|
||||
'View_Vehicle info_Basic Info',
|
||||
'View_Vehicle info_Documents',
|
||||
'View_Vehicle info_Repair Records',
|
||||
'Edit_Vehicle info_Basic Info',
|
||||
'Edit_Vehicle info_Documents',
|
||||
'Edit_Vehicle info_Repair Records',
|
||||
'Add_New Vehicle',
|
||||
'Archive_Vehicle',
|
||||
'Delete_Vehicle',
|
||||
'Export_Vehicle Report',
|
||||
'View_Transportation Schedule_Route Overview',
|
||||
'Create&Edit_Transportation Schedule',
|
||||
'Export_Transportation Schedule Report',
|
||||
'View_Route Template',
|
||||
'Create&Edit_Route Template',
|
||||
'View_Driver Assignment for Appointment',
|
||||
'Edit_Driver Assignment for Appointment',
|
||||
'View_Provider Info',
|
||||
'Create & Edit _Provider Info',
|
||||
'View_Appointment Request',
|
||||
'Edit & Create_Appointment Request',
|
||||
'View_Appointment Calendar',
|
||||
'Edit & Create_Appointment Calendar',
|
||||
'Medical Template',
|
||||
'View_Meal Status',
|
||||
'Edit_Meal Status',
|
||||
'View_Seating Chart',
|
||||
'Edit_Seating Chart',
|
||||
'Employee page',
|
||||
'Set Permission for Employee'
|
||||
];
|
||||
|
||||
const hasAllPermissionsByUsername = (username) => (username || '').toString().trim().toLowerCase() === 'testadmin03';
|
||||
// Create and Save a new Employee (driver, distributor, admin)
|
||||
exports.createEmployee = (req, res) => {
|
||||
// Validate request
|
||||
@@ -11,13 +78,16 @@ exports.createEmployee = (req, res) => {
|
||||
return;
|
||||
}
|
||||
const site = splitSite.findSiteNumber(req);
|
||||
const normalizedUsername = req.body.username || req.body.email || '';
|
||||
const requestPermissions = Array.isArray(req.body.permissions) ? req.body.permissions : [];
|
||||
// Create a Employee
|
||||
const employee = new Employee({
|
||||
username: req.body.username || req.body.email || '',
|
||||
username: normalizedUsername,
|
||||
name_cn: req.body.name_cn || '',
|
||||
email: req.body.email || '',
|
||||
password: req.body.password ? bcrypt.hashSync(req.body.password, 8) : '',
|
||||
roles: req.body.roles || [],
|
||||
permissions: hasAllPermissionsByUsername(normalizedUsername) ? ALL_PERMISSIONS : requestPermissions,
|
||||
mobile_phone: req.body.mobile_phone || '',
|
||||
home_phone: req.body.home_phone || '',
|
||||
language: req.body.language || '',
|
||||
@@ -204,13 +274,24 @@ exports.updateEmployee = (req, res) => {
|
||||
if (req.body.password) {
|
||||
req.body.password = bcrypt.hashSync(req.body.password, 8);
|
||||
}
|
||||
Employee.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
|
||||
.then(data => {
|
||||
if (!data) {
|
||||
Employee.findById(id)
|
||||
.then((existingEmployee) => {
|
||||
if (!existingEmployee) {
|
||||
res.status(404).send({
|
||||
message: `Cannot update employee with id=${id}. Maybe Employee was not found!`
|
||||
});
|
||||
} else res.send({ success: true, message: "Employee was updated successfully." });
|
||||
return null;
|
||||
}
|
||||
const nextData = Object.assign({}, req.body);
|
||||
const effectiveUsername = nextData.username || existingEmployee.username;
|
||||
if (hasAllPermissionsByUsername(effectiveUsername)) {
|
||||
nextData.permissions = ALL_PERMISSIONS;
|
||||
}
|
||||
return Employee.findByIdAndUpdate(id, nextData, { useFindAndModify: false });
|
||||
})
|
||||
.then((data) => {
|
||||
if (!data) return;
|
||||
res.send({ success: true, message: "Employee was updated successfully." });
|
||||
})
|
||||
.catch(err => {
|
||||
res.status(500).send({
|
||||
|
||||
@@ -8,6 +8,9 @@ module.exports = mongoose => {
|
||||
roles: [{
|
||||
type: String
|
||||
}],
|
||||
permissions: [{
|
||||
type: String
|
||||
}],
|
||||
mobile_phone: String,
|
||||
home_phone: String,
|
||||
language: String,
|
||||
|
||||
Reference in New Issue
Block a user