From 8009191ffa3dd7333bd0d84b8162e3d231bd8440 Mon Sep 17 00:00:00 2001 From: AriaYu927 Date: Tue, 14 Nov 2023 15:28:08 +0800 Subject: [PATCH] reduce redundant code --- src/controllers/projectController.js | 2 +- src/controllers/timeEntryController.js | 27 ++++++++++--------- src/helpers/dashboardhelper.js | 7 +++-- src/helpers/taskHelper.js | 2 +- src/helpers/userHelper.js | 36 +++++++++++++------------- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/controllers/projectController.js b/src/controllers/projectController.js index dc92af354..a88378985 100644 --- a/src/controllers/projectController.js +++ b/src/controllers/projectController.js @@ -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.' }); diff --git a/src/controllers/timeEntryController.js b/src/controllers/timeEntryController.js index aa7eff6ae..8adc6693d 100644 --- a/src/controllers/timeEntryController.js +++ b/src/controllers/timeEntryController.js @@ -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; @@ -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) @@ -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); @@ -492,7 +492,6 @@ const timeEntrycontroller = function (TimeEntry) { const { projectId } = req.params; TimeEntry.find( { - entryType: ['default', null], projectId, dateOfWork: { $gte: fromDate, $lte: todate }, }, diff --git a/src/helpers/dashboardhelper.js b/src/helpers/dashboardhelper.js index 1c03c2f88..041b4e948 100644 --- a/src/helpers/dashboardhelper.js +++ b/src/helpers/dashboardhelper.js @@ -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: { @@ -196,7 +195,7 @@ const dashboardhelper = function () { // leaderboard user roles hierarchy $or: [ { - role: { $in: ['Owner', 'Core Team'] }, + role: { $in: ['Owner', 'Core Team'] }, }, { $and: [ @@ -289,7 +288,7 @@ const dashboardhelper = function () { }, { $in: ['$$timeentry.entryType', ['default', null]], - } + }, ], }, }, @@ -445,7 +444,7 @@ const dashboardhelper = function () { $gte: pdtStart, $lte: pdtEnd, }, - eentryType: { $in: [ 'default', null ] }, + entryType: { $in: ['default', null] }, personId: userId, }); diff --git a/src/helpers/taskHelper.js b/src/helpers/taskHelper.js index 30bb220e9..b91020039 100644 --- a/src/helpers/taskHelper.js +++ b/src/helpers/taskHelper.js @@ -43,7 +43,7 @@ const taskHelper = function () { // dashboard tasks user roles hierarchy $or: [ { - role: { $in: ['Owner', 'Core Team'] }, + role: { $in: ['Owner', 'Core Team'] }, }, { $and: [ diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 5824d5d73..be9e40bbc 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -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: { @@ -621,8 +621,8 @@ const userHelper = function () { { $sum: { $map: { - input: "$timeEntries", - in: "$$this.totalSeconds" + input: '$timeEntries', + in: '$$this.totalSeconds', } } },