@@ -109,6 +109,22 @@ async function _checkUserPermissionForWriteWorkPeriod (currentUser) {
109109 }
110110}
111111
112+ /**
113+ * Checks if one of the date is missing and autocalculates it.
114+ * @param {Object } data workPeriod data object
115+ */
116+ async function _autoCalculateDates ( data ) {
117+ if ( data . startDate && ! data . endDate ) {
118+ const date = new Date ( data . startDate )
119+ date . setDate ( date . getDate ( ) + 6 )
120+ data . endDate = date
121+ } else if ( ! data . startDate && data . endDate ) {
122+ const date = new Date ( data . endDate )
123+ date . setDate ( date . getDate ( ) - 6 )
124+ data . startDate = date
125+ }
126+ }
127+
112128/**
113129 * Get workPeriod by id
114130 * @param {Object } currentUser the user who perform this operation.
@@ -162,15 +178,7 @@ async function createWorkPeriod (currentUser, workPeriod) {
162178 // check permission
163179 await _checkUserPermissionForWriteWorkPeriod ( currentUser )
164180 // If one of the dates are missing then auto-calculate it
165- if ( workPeriod . startDate && ! workPeriod . endDate ) {
166- const date = new Date ( workPeriod . startDate )
167- date . setDate ( date . getDate ( ) + 6 )
168- workPeriod . endDate = date
169- } else if ( ! workPeriod . startDate && workPeriod . endDate ) {
170- const date = new Date ( workPeriod . endDate )
171- date . setDate ( date . getDate ( ) - 6 )
172- workPeriod . startDate = date
173- }
181+ _autoCalculateDates ( workPeriod )
174182
175183 const resourceBooking = await helper . ensureResourceBookingById ( workPeriod . resourceBookingId ) // ensure resource booking exists
176184 workPeriod . projectId = resourceBooking . projectId
@@ -232,20 +240,8 @@ async function updateWorkPeriod (currentUser, id, data) {
232240 data . userHandle = user . handle
233241 }
234242 // If one of the dates are missing then auto-calculate it
235- if ( data . startDate && ! data . endDate ) {
236- const date = new Date ( data . startDate )
237- date . setDate ( date . getDate ( ) + 6 )
238- data . endDate = date
239- } else if ( ! data . startDate && data . endDate ) {
240- const date = new Date ( data . endDate )
241- date . setDate ( date . getDate ( ) - 6 )
242- data . startDate = date
243- }
244- // change the date format to match with database model
245- if ( data . startDate && data . endDate ) {
246- data . startDate = moment ( data . startDate ) . format ( 'YYYY-MM-DD' )
247- data . endDate = moment ( data . endDate ) . format ( 'YYYY-MM-DD' )
248- }
243+ _autoCalculateDates ( data )
244+
249245 data . updatedBy = await helper . getUserId ( currentUser . userId )
250246 let updated = null
251247 try {
@@ -259,8 +255,7 @@ async function updateWorkPeriod (currentUser, id, data) {
259255 }
260256
261257 await helper . postEvent ( config . TAAS_WORK_PERIOD_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue : oldValue } )
262- const result = _ . assign ( workPeriod . dataValues , data )
263- return result
258+ return updated . dataValues
264259}
265260
266261/**
0 commit comments