@@ -3,6 +3,10 @@ import {
33 createTaskValidation ,
44 updateTaskValidation ,
55} from '../validations/task.validation.js' ;
6+ import {
7+ createActivityLog ,
8+ generateActivityDetails ,
9+ } from '../utils/activityLogs.utils.js' ;
610
711/**
812 * Helper function to validate required params
@@ -344,6 +348,37 @@ export const createTask = async (req, res, next) => {
344348 } ,
345349 } ) ;
346350
351+ // Log the activity
352+ await createActivityLog ( {
353+ entityType : 'TASK' ,
354+ action : 'CREATED' ,
355+ userId : user . id ,
356+ organizationId,
357+ teamId,
358+ projectId,
359+ sprintId : sprintId || null ,
360+ taskId : task . id ,
361+ details : generateActivityDetails ( 'CREATED' , null , task ) ,
362+ } ) ;
363+
364+ // If task is assigned to someone, log that action too
365+ if ( assignedTo ) {
366+ await createActivityLog ( {
367+ entityType : 'TASK' ,
368+ action : 'ASSIGNED' ,
369+ userId : user . id ,
370+ organizationId,
371+ teamId,
372+ projectId,
373+ sprintId : sprintId || null ,
374+ taskId : task . id ,
375+ details : generateActivityDetails ( 'ASSIGNED' , null , {
376+ assignedTo,
377+ taskId : task . id ,
378+ } ) ,
379+ } ) ;
380+ }
381+
347382 // Fetch project members
348383 const projectMembers = await prisma . projectMember . findMany ( {
349384 where : { projectId } ,
@@ -517,6 +552,9 @@ export const updateTask = async (req, res, next) => {
517552 }
518553 }
519554
555+ // Save old task data for activity logging
556+ const oldTaskData = { ...task } ;
557+
520558 // Update the task
521559 const updatedTask = await prisma . task . update ( {
522560 where : { id : taskId } ,
@@ -536,6 +574,100 @@ export const updateTask = async (req, res, next) => {
536574 } ,
537575 } ) ;
538576
577+ // Log the general update activity
578+ await createActivityLog ( {
579+ entityType : 'TASK' ,
580+ action : 'UPDATED' ,
581+ userId : user . id ,
582+ organizationId,
583+ teamId,
584+ projectId,
585+ sprintId : updatedTask . sprintId || null ,
586+ taskId : updatedTask . id ,
587+ details : generateActivityDetails ( 'UPDATED' , oldTaskData , updatedTask ) ,
588+ } ) ;
589+
590+ // Assignment change
591+ if ( assignedTo !== undefined && assignedTo !== oldTaskData . assignedTo ) {
592+ if ( assignedTo === null ) {
593+ // Task was unassigned
594+ await createActivityLog ( {
595+ entityType : 'TASK' ,
596+ action : 'UNASSIGNED' ,
597+ userId : user . id ,
598+ organizationId,
599+ teamId,
600+ projectId,
601+ sprintId : updatedTask . sprintId || null ,
602+ taskId : updatedTask . id ,
603+ details : generateActivityDetails ( 'UNASSIGNED' , oldTaskData , null ) ,
604+ } ) ;
605+ } else if ( oldTaskData . assignedTo === null ) {
606+ // Task was assigned
607+ await createActivityLog ( {
608+ entityType : 'TASK' ,
609+ action : 'ASSIGNED' ,
610+ userId : user . id ,
611+ organizationId,
612+ teamId,
613+ projectId,
614+ sprintId : updatedTask . sprintId || null ,
615+ taskId : updatedTask . id ,
616+ details : generateActivityDetails ( 'ASSIGNED' , null , {
617+ assignedTo,
618+ taskId : updatedTask . id ,
619+ } ) ,
620+ } ) ;
621+ } else {
622+ // Assignment was changed
623+ await createActivityLog ( {
624+ entityType : 'TASK' ,
625+ action : 'UNASSIGNED' ,
626+ userId : user . id ,
627+ organizationId,
628+ teamId,
629+ projectId,
630+ sprintId : updatedTask . sprintId || null ,
631+ taskId : updatedTask . id ,
632+ details : generateActivityDetails ( 'UNASSIGNED' , oldTaskData , null ) ,
633+ } ) ;
634+
635+ await createActivityLog ( {
636+ entityType : 'TASK' ,
637+ action : 'ASSIGNED' ,
638+ userId : user . id ,
639+ organizationId,
640+ teamId,
641+ projectId,
642+ sprintId : updatedTask . sprintId || null ,
643+ taskId : updatedTask . id ,
644+ details : generateActivityDetails ( 'ASSIGNED' , null , {
645+ assignedTo,
646+ taskId : updatedTask . id ,
647+ } ) ,
648+ } ) ;
649+ }
650+ }
651+
652+ // Sprint change
653+ if ( sprintId !== undefined && sprintId !== oldTaskData . sprintId ) {
654+ await createActivityLog ( {
655+ entityType : 'TASK' ,
656+ action : 'TASK_MOVED' ,
657+ userId : user . id ,
658+ organizationId,
659+ teamId,
660+ projectId,
661+ sprintId : updatedTask . sprintId || null ,
662+ taskId : updatedTask . id ,
663+ details : generateActivityDetails (
664+ 'TASK_MOVED' ,
665+ oldTaskData ,
666+ updatedTask ,
667+ ) ,
668+ } ) ;
669+ }
670+
539671 return res . status ( 200 ) . json ( {
540672 success : true ,
541673 message : 'Task updated successfully' ,
@@ -625,6 +757,8 @@ export const updateTaskPriority = async (req, res, next) => {
625757 } ) ;
626758 }
627759
760+ const oldTaskData = { ...task } ;
761+
628762 // Update task priority
629763 const updatedTask = await prisma . task . update ( {
630764 where : { id : taskId } ,
@@ -635,6 +769,25 @@ export const updateTaskPriority = async (req, res, next) => {
635769 } ,
636770 } ) ;
637771
772+ // Status change
773+ if ( priority && priority !== oldTaskData . priority ) {
774+ await createActivityLog ( {
775+ entityType : 'TASK' ,
776+ action : 'STATUS_CHANGED' ,
777+ userId : user . id ,
778+ organizationId,
779+ teamId,
780+ projectId,
781+ sprintId : updatedTask . sprintId || null ,
782+ taskId : updatedTask . id ,
783+ details : generateActivityDetails (
784+ 'STATUS_CHANGED' ,
785+ oldTaskData ,
786+ updatedTask ,
787+ ) ,
788+ } ) ;
789+ }
790+
638791 return res . status ( 200 ) . json ( {
639792 success : true ,
640793 message : 'Task priority updated successfully' ,
@@ -725,6 +878,8 @@ export const updateTaskStatus = async (req, res, next) => {
725878 } ) ;
726879 }
727880
881+ const oldTaskData = { ...task } ;
882+
728883 // Update task status
729884 const updatedTask = await prisma . task . update ( {
730885 where : { id : taskId } ,
@@ -735,6 +890,25 @@ export const updateTaskStatus = async (req, res, next) => {
735890 } ,
736891 } ) ;
737892
893+ // Status change
894+ if ( status && status !== oldTaskData . status ) {
895+ await createActivityLog ( {
896+ entityType : 'TASK' ,
897+ action : 'STATUS_CHANGED' ,
898+ userId : user . id ,
899+ organizationId,
900+ teamId,
901+ projectId,
902+ sprintId : updatedTask . sprintId || null ,
903+ taskId : updatedTask . id ,
904+ details : generateActivityDetails (
905+ 'STATUS_CHANGED' ,
906+ oldTaskData ,
907+ updatedTask ,
908+ ) ,
909+ } ) ;
910+ }
911+
738912 return res . status ( 200 ) . json ( {
739913 success : true ,
740914 message : 'Task status updated successfully' ,
@@ -1213,6 +1387,19 @@ export const deleteTask = async (req, res, next) => {
12131387 }
12141388 } ) ;
12151389
1390+ // Log the delete activity
1391+ await createActivityLog ( {
1392+ entityType : 'TASK' ,
1393+ action : 'DELETED' ,
1394+ userId : user . id ,
1395+ organizationId,
1396+ teamId,
1397+ projectId,
1398+ sprintId : task . sprintId || null ,
1399+ taskId : task . id ,
1400+ details : generateActivityDetails ( 'DELETED' , task , null ) ,
1401+ } ) ;
1402+
12161403 return res . status ( 200 ) . json ( {
12171404 success : true ,
12181405 message : `Task ${ permanent ? 'permanently ' : '' } deleted successfully` ,
@@ -1367,6 +1554,22 @@ export const restoreTask = async (req, res, next) => {
13671554 } ,
13681555 } ) ;
13691556
1557+ // Log the restore activity
1558+ await createActivityLog ( {
1559+ entityType : 'TASK' ,
1560+ action : 'UPDATED' , // Using UPDATED with specific details
1561+ userId : user . id ,
1562+ organizationId,
1563+ teamId,
1564+ projectId,
1565+ sprintId : restoredTask . sprintId || null ,
1566+ taskId : restoredTask . id ,
1567+ details : {
1568+ action : 'RESTORE' ,
1569+ restoredAt : new Date ( ) ,
1570+ } ,
1571+ } ) ;
1572+
13701573 return res . status ( 200 ) . json ( {
13711574 success : true ,
13721575 message : `Task restored successfully${ restoreSubtasks ? ' with its subtasks' : '' } ` ,
0 commit comments