11const Project = require ( '../models/Project' ) ;
22const Task = require ( '../models/Task' ) ;
33const User = require ( '../models/User' ) ;
4+ const Notification = require ( '../models/Notification' ) ;
45const taskUtil = require ( '../utils/task-util' ) ;
56
67exports . createTask = async ( taskData , userId ) => {
@@ -19,6 +20,21 @@ exports.createTask = async (taskData, userId) => {
1920 const task = new Task ( { ...taskData , createdBy : userId } ) ;
2021 await task . save ( ) ;
2122
23+ if ( Array . isArray ( task . assignedTo ) && task . assignedTo . length > 0 ) {
24+ const notifications = task . assignedTo
25+ . filter ( ( uid ) => uid . toString ( ) !== userId . toString ( ) )
26+ . map ( ( uid ) => ( {
27+ message : `${ task . title } 업무가 할당되었습니다.` ,
28+ receiver : uid ,
29+ sender : userId ,
30+ noticeType : 'task_assigned' ,
31+ relatedTask : task . _id ,
32+ } ) ) ;
33+
34+ if ( notifications . length > 0 )
35+ await Notification . insertMany ( notifications ) ;
36+ }
37+
2238 return task ;
2339 } catch ( err ) {
2440 console . error ( err ) ;
@@ -94,15 +110,31 @@ exports.updateTaskInfo = async (taskData, taskId, userId) => {
94110 throw new Error ( 'date data are invalidate' ) ;
95111 }
96112
97- const result = await Task . findByIdAndUpdate (
113+ const task = await Task . findByIdAndUpdate (
98114 taskId ,
99115 {
100116 $set : taskData ,
101117 } ,
102118 { new : true , runValidators : true }
103119 ) ;
104120
105- return result ;
121+ if ( Array . isArray ( task . assignedTo ) && task . assignedTo . length > 0 ) {
122+ const notifications = task . assignedTo
123+ . filter ( ( uid ) => uid . toString !== userId . toString )
124+ . map ( ( uid ) => ( {
125+ message : `${ task . title } 업무가 업데이트 되었습니다.` ,
126+ receiver : uid ,
127+ sender : userId ,
128+ noticeType : 'task_updated' ,
129+ relatedTask : task . _id ,
130+ } ) ) ;
131+
132+ if ( notifications . length > 0 ) {
133+ await Notification . insertMany ( notifications ) ;
134+ }
135+ }
136+
137+ return task ;
106138 } catch ( err ) {
107139 console . error ( err ) ;
108140 throw new Error ( 'Error occured during updating task' ) ;
@@ -171,14 +203,31 @@ exports.addManagers = async (taskId, userId, managerId) => {
171203 }
172204 await taskUtil . isExistingResource ( User , managerId ) ;
173205
174- const updatedTask = Task . findByIdAndUpdate (
206+ const alreadyAssigned = task . assignedTo . map ( ( uid ) => uid . toString ( ) ) ;
207+
208+ const updatedTask = await Task . findByIdAndUpdate (
175209 taskId ,
176210 {
177211 $addToSet : { assignedTo : managerId } ,
178212 } ,
179213 { new : true }
180214 ) ;
181215
216+ if (
217+ ! alreadyAssigned . includes ( managerId . toString ( ) ) &&
218+ updatedTask . assignedTo . length > 0
219+ ) {
220+ const notification = {
221+ message : `${ updatedTask . title } 업무가 할당되었습니다.` ,
222+ receiver : managerId ,
223+ sender : userId ,
224+ noticeType : 'task_assigned' ,
225+ relatedTask : updatedTask . _id ,
226+ } ;
227+
228+ await Notification . create ( notification ) ;
229+ }
230+
182231 return updatedTask ;
183232 } catch ( err ) {
184233 console . error ( err ) ;
0 commit comments