Skip to content

fix workperiodService reusable method and unnecessary conversion #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions src/services/WorkPeriodService.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ async function _checkUserPermissionForWriteWorkPeriod (currentUser) {
}
}

/**
* Checks if one of the date is missing and autocalculates it.
* @param {Object} data workPeriod data object
*/
async function _autoCalculateDates (data) {
if (data.startDate && !data.endDate) {
const date = new Date(data.startDate)
date.setDate(date.getDate() + 6)
data.endDate = date
} else if (!data.startDate && data.endDate) {
const date = new Date(data.endDate)
date.setDate(date.getDate() - 6)
data.startDate = date
}
}

/**
* Get workPeriod by id
* @param {Object} currentUser the user who perform this operation.
Expand Down Expand Up @@ -162,15 +178,7 @@ async function createWorkPeriod (currentUser, workPeriod) {
// check permission
await _checkUserPermissionForWriteWorkPeriod(currentUser)
// If one of the dates are missing then auto-calculate it
if (workPeriod.startDate && !workPeriod.endDate) {
const date = new Date(workPeriod.startDate)
date.setDate(date.getDate() + 6)
workPeriod.endDate = date
} else if (!workPeriod.startDate && workPeriod.endDate) {
const date = new Date(workPeriod.endDate)
date.setDate(date.getDate() - 6)
workPeriod.startDate = date
}
_autoCalculateDates(workPeriod)

const resourceBooking = await helper.ensureResourceBookingById(workPeriod.resourceBookingId) // ensure resource booking exists
workPeriod.projectId = resourceBooking.projectId
Expand Down Expand Up @@ -232,20 +240,8 @@ async function updateWorkPeriod (currentUser, id, data) {
data.userHandle = user.handle
}
// If one of the dates are missing then auto-calculate it
if (data.startDate && !data.endDate) {
const date = new Date(data.startDate)
date.setDate(date.getDate() + 6)
data.endDate = date
} else if (!data.startDate && data.endDate) {
const date = new Date(data.endDate)
date.setDate(date.getDate() - 6)
data.startDate = date
}
// change the date format to match with database model
if (data.startDate && data.endDate) {
data.startDate = moment(data.startDate).format('YYYY-MM-DD')
data.endDate = moment(data.endDate).format('YYYY-MM-DD')
}
_autoCalculateDates(data)

data.updatedBy = await helper.getUserId(currentUser.userId)
let updated = null
try {
Expand All @@ -259,8 +255,7 @@ async function updateWorkPeriod (currentUser, id, data) {
}

await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue })
const result = _.assign(workPeriod.dataValues, data)
return result
return updated.dataValues
}

/**
Expand Down