Skip to content

Commit

Permalink
Merge pull request #705 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.33]
  • Loading branch information
one-community authored Jan 17, 2024
2 parents 4a8a010 + a1e2024 commit 5eabffa
Show file tree
Hide file tree
Showing 8 changed files with 667 additions and 655 deletions.
14 changes: 6 additions & 8 deletions src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ const taskController = function (Task) {
return res.status(400).send({ error: 'This is not a valid task' });
}

const hoursLogged = await timeEntryHelper.getAllHoursLoggedForSpecifiedProject(taskId);
const hoursLogged = await timeEntryHelper.getAllHoursLoggedForSpecifiedTask(taskId);
task.set('hoursLogged', hoursLogged, { strict: false });

// Fetch the resource names for all resources
Expand All @@ -815,12 +815,10 @@ const taskController = function (Task) {
resource.name = resourceNames[index] !== ' ' ? resourceNames[index] : resource.name;
});

res.status(200).send(task);
return res.status(200).send(task);
} catch (error) {
// Generic error message, you can adjust as needed
res
.status(500)
.send({ error: 'Internal Server Error', details: error.message });
return res.status(500).send({ error: 'Internal Server Error', details: error.message });
}
};

Expand Down Expand Up @@ -875,9 +873,9 @@ const taskController = function (Task) {
};

const getTasksForTeamsByUser = async (req, res) => {
const userId = mongoose.Types.ObjectId(req.params.userId);
try {
const userId = mongoose.Types.ObjectId(req.params.userId);
const teamsData = await taskHelper.getTasksForTeams(userId);
const teamsData = await taskHelper.getTasksForTeams(userId, req.body.requestor);
if (teamsData.length > 0) {
res.status(200).send(teamsData);
} else {
Expand All @@ -888,7 +886,7 @@ const taskController = function (Task) {
}
} catch (error) {
console.log(error);
res.status(400).send(error);
res.status(400).send({ error });
}
};

Expand Down
92 changes: 46 additions & 46 deletions src/controllers/timeEntryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ const timeEntrycontroller = function (TimeEntry) {
return res.status(400).send({ error });
}

if (!mongoose.Types.ObjectId.isValid(timeEntryId)) {
const error = 'ObjectIds are not correctly formed';
return res.status(400).send({ error });
}

const {
personId,
hours: newHours = '00',
Expand All @@ -179,6 +174,18 @@ const timeEntrycontroller = function (TimeEntry) {
dateOfWork: newDateOfWork,
} = req.body;

const type = req.body.entryType;
const isGeneralEntry = isGeneralTimeEntry(type);

if (
!mongoose.Types.ObjectId.isValid(timeEntryId)
|| ((isGeneralEntry || type === 'project')
&& !mongoose.Types.ObjectId.isValid(newProjectId))
) {
const error = 'ObjectIds are not correctly formed';
return res.status(400).send({ error });
}

const isForAuthUser = personId === req.body.requestor.requestorId;
const isSameDayTimeEntry = moment().tz('America/Los_Angeles').format('YYYY-MM-DD') === newDateOfWork;
const canEdit = (await hasPermission(req.body.requestor, 'editTimeEntry')) || (isForAuthUser && isSameDayTimeEntry);
Expand All @@ -191,24 +198,7 @@ const timeEntrycontroller = function (TimeEntry) {
const session = await mongoose.startSession();
session.startTransaction();

const type = req.body.entryType;
const isGeneralEntry = isGeneralTimeEntry(type);

try {
if (!timeEntryId) {
const error = 'ObjectId in request param is not in correct format';
return res.status(400).send({ error });
}

if (
!mongoose.Types.ObjectId.isValid(timeEntryId)
|| ((isGeneralEntry || type === 'project')
&& !mongoose.Types.ObjectId.isValid(newProjectId)
)) {
const error = 'ObjectIds are not correctly formed';
return res.status(400).send({ error });
}

// Get initial timeEntry by timeEntryId
const timeEntry = await TimeEntry.findById(timeEntryId);
if (!timeEntry) {
Expand Down Expand Up @@ -338,7 +328,7 @@ const timeEntrycontroller = function (TimeEntry) {
timeEntry.dateOfWork = moment(newDateOfWork).format('YYYY-MM-DD');
await timeEntry.save();

return res.status(200).send({ message: 'Successfully updated time entry' });
return res.status(200).send(timeEntry);
} catch (err) {
await session.abortTransaction();
return res.status(400).send({ error: err.toString() });
Expand Down Expand Up @@ -509,30 +499,40 @@ const timeEntrycontroller = function (TimeEntry) {
},
' -createdDateTime',
)
.populate('projectId')
.sort({ lastModifiedDateTime: -1 })
.then((results) => {
const data = [];
results.forEach((element) => {
const record = {};

record._id = element._id;
record.notes = element.notes;
record.isTangible = element.isTangible;
record.personId = element.personId;
record.projectId = element.projectId ? element.projectId._id : '';
record.projectName = element.projectId
? element.projectId.projectName
: '';
record.dateOfWork = element.dateOfWork;
[record.hours, record.minutes] = formatSeconds(element.totalSeconds);
data.push(record);
});
res.status(200).send(data);
})
.catch((error) => {
res.status(400).send(error);
.populate('personId')
.populate('projectId')
.populate('taskId')
.populate('wbsId')
.sort({ lastModifiedDateTime: -1 })
.then((results) => {
const data = [];

results.forEach((element) => {
const record = {};
record._id = element._id;
record.notes = element.notes;
record.isTangible = element.isTangible;
record.personId = element.personId._id;
record.userProfile = element.personId;
record.dateOfWork = element.dateOfWork;
[record.hours, record.minutes] = formatSeconds(element.totalSeconds);
record.projectId = element.projectId._id;
record.projectName = element.projectId.projectName;
record.projectCategory = element.projectId.category.toLowerCase();
record.taskId = element.taskId?._id || null;
record.taskName = element.taskId?.taskName || null;
record.taskClassification = element.taskId?.classification?.toLowerCase() || null;
record.wbsId = element.wbsId?._id || null;
record.wbsName = element.wbsId?.wbsName || null;

data.push(record);
});

res.status(200).send(data);
})
.catch((error) => {
res.status(400).send(error);
});
};

const getTimeEntriesForSpecifiedProject = function (req, res) {
Expand Down
Loading

0 comments on commit 5eabffa

Please sign in to comment.