Skip to content

Commit

Permalink
Merge pull request #1099 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.99]
  • Loading branch information
one-community authored Sep 12, 2024
2 parents d15e4c4 + b5d5536 commit 9d42d12
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 74 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion src/controllers/logincontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const logincontroller = function () {

res.status(200).send({ token });
} else {
res.status(403).send({
res.status(404).send({
message: 'Invalid password.',
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/logincontroller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('logincontroller module', () => {
expect(findOneSpy).toHaveBeenCalledWith({ email: mockReqModified.body.email });

assertResMock(
403,
404,
{
message: 'Invalid password.',
},
Expand Down
13 changes: 10 additions & 3 deletions src/controllers/ownerMessageController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const helper = require('../utilities/permissions');

const ownerMessageController = function (OwnerMessage) {
const getOwnerMessage = async function (req, res) {
try {
const results = await OwnerMessage.find({});
if (results.length === 0) { // first time initialization
if (results.length === 0) {
// first time initialization
const ownerMessage = new OwnerMessage();
await ownerMessage.save();
res.status(200).send({ ownerMessage });
Expand All @@ -15,7 +18,9 @@ const ownerMessageController = function (OwnerMessage) {
};

const updateOwnerMessage = async function (req, res) {
if (req.body.requestor.role !== 'Owner') {
if (
!(await helper.hasPermission(req.body.requestor, 'editHeaderMessage'))
) {
res.status(403).send('You are not authorized to create messages!');
}
const { isStandard, newMessage } = req.body;
Expand All @@ -40,7 +45,9 @@ const ownerMessageController = function (OwnerMessage) {
};

const deleteOwnerMessage = async function (req, res) {
if (req.body.requestor.role !== 'Owner') {
if (
!(await helper.hasPermission(req.body.requestor, 'editHeaderMessage'))
) {
res.status(403).send('You are not authorized to delete messages!');
}
try {
Expand Down
44 changes: 20 additions & 24 deletions src/controllers/profileInitialSetupController.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,30 +523,26 @@ const profileInitialSetupController = function (
*/
const getSetupInvitation = (req, res) => {
const { role } = req.body.requestor;
if (role === 'Administrator' || role === 'Owner') {
try {
ProfileInitialSetupToken.find({ isSetupCompleted: false })
.sort({ createdDate: -1 })
.exec((err, result) => {
// Handle the result
if (err) {
LOGGER.logException(err);
return res
.status(500)
.send(
'Internal Error: Please retry. If the problem persists, please contact the administrator',
);
}
return res.status(200).send(result);
});
} catch (error) {
LOGGER.logException(error);
return res
.status(500)
.send(
'Internal Error: Please retry. If the problem persists, please contact the administrator',
);
}

const { permissions } = req.body.requestor;
let user_permissions = ['getUserProfiles','postUserProfile','putUserProfile','changeUserStatus']
if ((role === 'Administrator') || (role === 'Owner') || (role === 'Manager') || (role === 'Mentor') || user_permissions.some(e=>permissions.frontPermissions.includes(e))) {
try{
ProfileInitialSetupToken
.find({ isSetupCompleted: false })
.sort({ createdDate: -1 })
.exec((err, result) => {
// Handle the result
if (err) {
LOGGER.logException(err);
return res.status(500).send('Internal Error: Please retry. If the problem persists, please contact the administrator');
}
return res.status(200).send(result);
});
} catch (error) {
LOGGER.logException(error);
return res.status(500).send('Internal Error: Please retry. If the problem persists, please contact the administrator');
}
} else {
return res.status(403).send('You are not authorized to get setup history.');
}
Expand Down
19 changes: 15 additions & 4 deletions src/controllers/timeEntryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,15 @@ const timeEntrycontroller = function (TimeEntry) {
const recalculateHoursByCategoryAllUsers = async function (req, res) {
const session = await mongoose.startSession();
session.startTransaction();
let keepAliveInterval;

try {
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Transfer-Encoding', 'chunked');
keepAliveInterval = setInterval(() => {
res.write('Processing... keep connection alive\n');
}, 150 * 1000); // interval of 150 seconds

const userprofiles = await UserProfile.find({}, '_id').lean();

const recalculationPromises = userprofiles.map(async (userprofile) => {
Expand All @@ -1385,13 +1392,17 @@ const timeEntrycontroller = function (TimeEntry) {
await Promise.all(recalculationPromises);

await session.commitTransaction();
return res.status(200).send({
message: 'finished the recalculation for hoursByCategory for all users',
});
clearInterval(keepAliveInterval);
res.write('finished the recalculation for hoursByCategory for all users\n');
return res.end();
} catch (err) {
await session.abortTransaction();
if (keepAliveInterval) {
clearInterval(keepAliveInterval);
}
logger.logException(err);
return res.status(500).send({ error: err.toString() });
res.write(`error: ${err.toString()}\n`);
return res.end();
} finally {
session.endSession();
}
Expand Down
77 changes: 75 additions & 2 deletions src/controllers/titleController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const Team = require('../models/team');
const Project = require('../models/project');
const cacheClosure = require('../utilities/nodeCache');
const { getAllTeamCodeHelper } = require("./userProfileController");
const userProfileController = require("./userProfileController");
const userProfile = require('../models/userProfile');
const project = require('../models/project');
const controller = userProfileController(userProfile, project);
const getAllTeamCodeHelper = controller.getAllTeamCodeHelper;

const titlecontroller = function (Title) {
const cache = cacheClosure();
Expand All @@ -22,7 +26,6 @@ const titlecontroller = function (Title) {

const postTitle = async function (req, res) {
const title = new Title();

title.titleName = req.body.titleName;
title.teamCode = req.body.teamCode;
title.projectAssigned = req.body.projectAssigned;
Expand Down Expand Up @@ -81,6 +84,72 @@ const titlecontroller = function (Title) {
.catch((error) => res.status(404).send(error));
};

// update title function.
const updateTitle = async function (req, res) {
try{

const filter=req.body.id;

// valid title name
if (!req.body.titleName.trim()) {
res.status(400).send({ message: 'Title cannot be empty.' });
return;
}

// if media is empty
if (!req.body.mediaFolder.trim()) {
res.status(400).send({ message: 'Media folder cannot be empty.' });
return;
}
const shortnames = req.body.titleName.trim().split(' ');
let shortname;
if (shortnames.length > 1) {
shortname = (shortnames[0][0] + shortnames[1][0]).toUpperCase();
} else if (shortnames.length === 1) {
shortname = shortnames[0][0].toUpperCase();
}
req.body.shortName = shortname;

// Validate team code by checking if it exists in the database
if (!req.body.teamCode) {
res.status(400).send({ message: 'Please provide a team code.' });
return;
}

const teamCodeExists = await checkTeamCodeExists(req.body.teamCode);
if (!teamCodeExists) {
res.status(400).send({ message: 'Invalid team code. Please provide a valid team code.' });
return;
}

// validate if project exist
const projectExist = await checkProjectExists(req.body.projectAssigned._id);
if (!projectExist) {
res.status(400).send({ message: 'Project is empty or not exist.' });
return;
}

// validate if team exist
if (req.body.teamAssiged && req.body.teamAssiged._id === 'N/A') {
res.status(400).send({ message: 'Team not exists.' });
return;
}
const result = await Title.findById(filter);
result.titleName = req.body.titleName;
result.teamCode = req.body.teamCode;
result.projectAssigned = req.body.projectAssigned;
result.mediaFolder = req.body.mediaFolder;
result.teamAssiged = req.body.teamAssiged;
const updatedTitle = await result.save();
res.status(200).send({ message: 'Update successful', updatedTitle });

}catch(error){
console.log(error);
res.status(500).send({ message: 'An error occurred', error });
}

};

const deleteTitleById = async function (req, res) {
const { titleId } = req.params;
Title.deleteOne({ _id: titleId })
Expand All @@ -98,6 +167,7 @@ const titlecontroller = function (Title) {
}
})
.catch((error) => {
console.log(error)
res.status(500).send(error);
});
};
Expand Down Expand Up @@ -126,12 +196,15 @@ const titlecontroller = function (Title) {
}
}



return {
getAllTitles,
getTitleById,
postTitle,
deleteTitleById,
deleteAllTitles,
updateTitle
};
};

Expand Down
Loading

0 comments on commit 9d42d12

Please sign in to comment.