Skip to content

Commit

Permalink
reduce redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaYu927 committed Nov 14, 2023
1 parent 90daec2 commit 8009191
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/controllers/projectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const projectController = function (Project) {

// find if project has any time entries associated with it

timeentry.find({ projectId: record._id, entryType: [ 'default', 'project', null ] }, '_id')
timeentry.find({ projectId: record._id }, '_id')
.then((timeentries) => {
if (timeentries.length > 0) {
res.status(400).send({ error: 'This project has associated time entries and cannot be deleted. Consider inactivaing it instead.' });
Expand Down
27 changes: 13 additions & 14 deletions src/controllers/timeEntryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const timeEntrycontroller = function (TimeEntry) {
}
const items = [];
records.forEach((element) => {
if (element.entryType == 'default' || element.entryType == undefined) {
if (element.entryType === 'default' || element.entryType === undefined) {
const timeentry = new TimeEntry();
timeentry.personId = element.personId;
timeentry.projectId = element.projectId;
Expand Down Expand Up @@ -265,7 +265,7 @@ const timeEntrycontroller = function (TimeEntry) {
};

switch (req.body.entryType) {
case 'default':
default:
if (
!mongoose.Types.ObjectId.isValid(req.body.personId)
|| !mongoose.Types.ObjectId.isValid(req.body.projectId)
Expand Down Expand Up @@ -321,18 +321,18 @@ const timeEntrycontroller = function (TimeEntry) {
res.status(400).send(error);
});

// Add this tangbile time entry to related task's hoursLogged
if ((timeentry.entryType === 'default') && timeentry.isTangible === true) {
try {
const currentTask = await task.findById(req.body.projectId);
currentTask.hoursLogged += (timeentry.totalSeconds / 3600);
await currentTask.save();
} catch (error) {
throw new Error('Failed to find the task by id');
}
}
// checking if logged in hours exceed estimated time after timeentry for a task
if (timeentry.entryType === 'default') {
// Add this tangbile time entry to related task's hoursLogged
if (timeentry.isTangible === true) {
try {
const currentTask = await task.findById(req.body.projectId);
currentTask.hoursLogged += (timeentry.totalSeconds / 3600);
await currentTask.save();
} catch (error) {
throw new Error('Failed to find the task by id');
}
}
// checking if logged in hours exceed estimated time after timeentry for a task
const record = await userProfile.findById(timeentry.personId.toString());
const currentTask = await task.findById(req.body.projectId);
checkTaskOvertime(timeentry, record, currentTask);
Expand Down Expand Up @@ -492,7 +492,6 @@ const timeEntrycontroller = function (TimeEntry) {
const { projectId } = req.params;
TimeEntry.find(
{
entryType: ['default', null],
projectId,
dateOfWork: { $gte: fromDate, $lte: todate },
},
Expand Down
7 changes: 3 additions & 4 deletions src/helpers/dashboardhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ const dashboardhelper = function () {
.tz('America/Los_Angeles')
.endOf('week')
.format('YYYY-MM-DD');
const entryTypes = ['default', null];
return myTeam.aggregate([
{
$match: {
Expand Down Expand Up @@ -196,7 +195,7 @@ const dashboardhelper = function () {
// leaderboard user roles hierarchy
$or: [
{
role: { $in: ['Owner', 'Core Team'] },
role: { $in: ['Owner', 'Core Team'] },
},
{
$and: [
Expand Down Expand Up @@ -289,7 +288,7 @@ const dashboardhelper = function () {
},
{
$in: ['$$timeentry.entryType', ['default', null]],
}
},
],
},
},
Expand Down Expand Up @@ -445,7 +444,7 @@ const dashboardhelper = function () {
$gte: pdtStart,
$lte: pdtEnd,
},
eentryType: { $in: [ 'default', null ] },
entryType: { $in: ['default', null] },
personId: userId,
});

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/taskHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const taskHelper = function () {
// dashboard tasks user roles hierarchy
$or: [
{
role: { $in: ['Owner', 'Core Team'] },
role: { $in: ['Owner', 'Core Team'] },
},
{
$and: [
Expand Down
36 changes: 18 additions & 18 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,31 +578,31 @@ const userHelper = function () {
const missedHours = await userProfile.aggregate([
{
$match: {
role: "Core Team",
isActive: true
}
role: 'Core Team',
isActive: true,
},
},
{
$lookup: {
from: "timeEntries",
localField: "_id",
foreignField: "personId",
from: 'timeEntries',
localField: '_id',
foreignField: 'personId',
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$isTangible", true] },
{ $gte: ["$dateOfWork", startOfLastWeek] },
{ $lte: ["$dateOfWork", endOfLastWeek] },
{ $in: ['$entryType', 'default', null] }
]
}
}
}
{ $eq: ['$isTangible', true] },
{ $gte: ['$dateOfWork', startOfLastWeek] },
{ $lte: ['$dateOfWork', endOfLastWeek] },
{ $in: ['$entryType', 'default', null] },
],
},
},
},
],
as: "timeEntries"
}
as: 'timeEntries',
},
},
{
$project: {
Expand All @@ -621,8 +621,8 @@ const userHelper = function () {
{
$sum: {
$map: {
input: "$timeEntries",
in: "$$this.totalSeconds"
input: '$timeEntries',
in: '$$this.totalSeconds',
}
}
},
Expand Down

0 comments on commit 8009191

Please sign in to comment.