@@ -12,6 +12,7 @@ const { getPaginatedLink } = require("../utils/helper");
12
12
const { updateUserStatusOnTaskUpdate, updateStatusOnTaskCompletion } = require ( "../models/userStatus" ) ;
13
13
const dataAccess = require ( "../services/dataAccessLayer" ) ;
14
14
const { parseSearchQuery } = require ( "../utils/tasks" ) ;
15
+ const { addTaskCreatedAtAndUpdatedAtFields } = require ( "../services/tasks" ) ;
15
16
/**
16
17
* Creates new task
17
18
*
@@ -24,9 +25,12 @@ const addNewTask = async (req, res) => {
24
25
const { id : createdBy } = req . userData ;
25
26
const dependsOn = req . body . dependsOn ;
26
27
let userStatusUpdate ;
28
+ const timeStamp = Math . round ( Date . now ( ) / 1000 ) ;
27
29
const body = {
28
30
...req . body ,
29
31
createdBy,
32
+ createdAt : timeStamp ,
33
+ updatedAt : timeStamp ,
30
34
} ;
31
35
delete body . dependsOn ;
32
36
const { taskId, taskDetails } = await tasks . updateTask ( body ) ;
@@ -270,7 +274,7 @@ const updateTask = async (req, res) => {
270
274
if ( ! task . taskData ) {
271
275
return res . boom . notFound ( "Task not found" ) ;
272
276
}
273
- const requestData = { ...req . body } ;
277
+ const requestData = { ...req . body , updatedAt : Math . round ( Date . now ( ) / 1000 ) } ;
274
278
if ( requestData ?. assignee ) {
275
279
const user = await dataAccess . retrieveUsers ( { username : requestData . assignee } ) ;
276
280
if ( ! user . userExists ) {
@@ -310,8 +314,10 @@ const updateTask = async (req, res) => {
310
314
*/
311
315
const updateTaskStatus = async ( req , res , next ) => {
312
316
try {
317
+ req . body . updatedAt = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ;
313
318
let userStatusUpdate ;
314
319
const taskId = req . params . id ;
320
+ const { userStatusFlag } = req . query ;
315
321
const { id : userId , username } = req . userData ;
316
322
const task = await tasks . fetchSelfTask ( taskId , userId ) ;
317
323
@@ -320,18 +326,34 @@ const updateTaskStatus = async (req, res, next) => {
320
326
if ( task . taskData . status === TASK_STATUS . VERIFIED || req . body . status === TASK_STATUS . MERGED )
321
327
return res . boom . forbidden ( "Status cannot be updated. Please contact admin." ) ;
322
328
323
- if ( task . taskData . status === TASK_STATUS . DONE && req . body . percentCompleted < 100 ) {
324
- if ( req . body . status === TASK_STATUS . DONE || ! req . body . status ) {
325
- return res . boom . badRequest ( "Task percentCompleted can't updated as status is DONE" ) ;
329
+ if ( userStatusFlag ) {
330
+ if ( task . taskData . status === TASK_STATUS . DONE && req . body . percentCompleted < 100 ) {
331
+ if ( req . body . status === TASK_STATUS . DONE || ! req . body . status ) {
332
+ return res . boom . badRequest ( "Task percentCompleted can't updated as status is DONE" ) ;
333
+ }
334
+ }
335
+
336
+ if (
337
+ ( req . body . status === TASK_STATUS . DONE || req . body . status === TASK_STATUS . VERIFIED ) &&
338
+ task . taskData . percentCompleted !== 100
339
+ ) {
340
+ if ( req . body . percentCompleted !== 100 ) {
341
+ return res . boom . badRequest ( "Status cannot be updated. Task is not done yet" ) ;
342
+ }
326
343
}
327
344
}
328
345
346
+ if ( task . taskData . status === TASK_STATUS . COMPLETED && req . body . percentCompleted < 100 ) {
347
+ if ( req . body . status === TASK_STATUS . COMPLETED || ! req . body . status ) {
348
+ return res . boom . badRequest ( "Task percentCompleted can't updated as status is COMPLETED" ) ;
349
+ }
350
+ }
329
351
if (
330
- ( req . body . status === TASK_STATUS . DONE || req . body . status === TASK_STATUS . VERIFIED ) &&
352
+ ( req . body . status === TASK_STATUS . COMPLETED || req . body . status === TASK_STATUS . VERIFIED ) &&
331
353
task . taskData . percentCompleted !== 100
332
354
) {
333
355
if ( req . body . percentCompleted !== 100 ) {
334
- return res . boom . badRequest ( "Status cannot be updated. Task is not done yet" ) ;
356
+ return res . boom . badRequest ( "Status cannot be updated. Task is not completed yet" ) ;
335
357
}
336
358
}
337
359
@@ -428,6 +450,11 @@ const assignTask = async (req, res) => {
428
450
429
451
const updateStatus = async ( req , res ) => {
430
452
try {
453
+ const { action, field } = req . body ;
454
+ if ( action === "ADD" && field === "CREATED_AT+UPDATED_AT" ) {
455
+ const updateStats = await addTaskCreatedAtAndUpdatedAtFields ( ) ;
456
+ return res . json ( updateStats ) ;
457
+ }
431
458
const response = await tasks . updateTaskStatus ( ) ;
432
459
return res . status ( 200 ) . json ( response ) ;
433
460
} catch ( error ) {
0 commit comments