@@ -10,7 +10,7 @@ const tasksController = {
1010 status : req . body . status ,
1111 notes : req . body . tasknotes ,
1212 } ) ;
13- res . locals . createdTask = createdTask ;
13+ res . locals . task = createdTask ;
1414
1515 return next ( ) ;
1616 } catch ( err ) {
@@ -27,18 +27,18 @@ const tasksController = {
2727 const column = req . body . status ;
2828 let updateQuery ;
2929 if ( column === "backlog" ) {
30- updateQuery = { $push : { backlog : res . locals . createdTask . _id } } ;
30+ updateQuery = { $push : { backlog : res . locals . task . _id } } ;
3131 } else if ( column === "inProgress" ) {
3232 updateQuery = {
33- $push : { inProgress : res . locals . createdTask . _id } ,
33+ $push : { inProgress : res . locals . task . _id } ,
3434 } ;
3535 } else if ( column === "inReview" ) {
3636 updateQuery = {
37- $push : { inReview : res . locals . createdTask . _id } ,
37+ $push : { inReview : res . locals . task . _id } ,
3838 } ;
3939 } else {
4040 updateQuery = {
41- $push : { completed : res . locals . createdTask . _id } ,
41+ $push : { completed : res . locals . task . _id } ,
4242 } ;
4343 }
4444 await Board . updateOne ( { _id : req . body . boardId } , updateQuery ) ;
@@ -69,6 +69,98 @@ const tasksController = {
6969 } ) ;
7070 }
7171 } ,
72+ editTask : async ( req : Request , res : Response , next : NextFunction ) => {
73+ try {
74+ const taskEdits = req . body ;
75+ const editedTask = await Card . findByIdAndUpdate (
76+ taskEdits . taskId ,
77+ {
78+ name : taskEdits . taskname ,
79+ status : taskEdits . status ,
80+ notes : taskEdits . tasknotes ,
81+ } ,
82+ { new : true }
83+ ) ;
84+ res . locals . task = editedTask ;
85+
86+ return next ( ) ;
87+ } catch ( err ) {
88+ // pass error through to global error handler
89+ return next ( {
90+ log : `tasksController.editTask ERROR: ${ err } ` ,
91+ status : 500 ,
92+ message : { err : "Error editing Task" } ,
93+ } ) ;
94+ }
95+ } ,
96+ pullTask : async ( req : Request , res : Response , next : NextFunction ) => {
97+ try {
98+ const column = req . body . startColumn ;
99+ let updateQuery ;
100+ if ( column === "backlog" ) {
101+ updateQuery = { $pull : { backlog : res . locals . task . _id } } ;
102+ } else if ( column === "inProgress" ) {
103+ updateQuery = {
104+ $pull : { inProgress : res . locals . task . _id } ,
105+ } ;
106+ } else if ( column === "inReview" ) {
107+ updateQuery = {
108+ $pull : { inReview : res . locals . task . _id } ,
109+ } ;
110+ } else {
111+ updateQuery = {
112+ $pull : { completed : res . locals . task . _id } ,
113+ } ;
114+ }
115+ await Board . updateOne ( { _id : req . body . boardId } , updateQuery ) ;
116+ return next ( ) ;
117+ } catch ( err ) {
118+ // pass error through to global error handler
119+ return next ( {
120+ log : `tasksController.pullTask ERROR: ${ err } ` ,
121+ status : 500 ,
122+ message : { err : "Error pulling Task" } ,
123+ } ) ;
124+ }
125+ } ,
126+ deleteTask : async ( req : Request , res : Response , next : NextFunction ) => {
127+ try {
128+ const deletedTask = await Card . findOneAndDelete ( {
129+ _id : req . params . taskId ,
130+ } ) ;
131+ res . locals . task = deletedTask ;
132+ return next ( ) ;
133+ } catch ( err ) {
134+ // pass error through to global error handler
135+ return next ( {
136+ log : `tasksController.deleteTask ERROR: ${ err } ` ,
137+ status : 500 ,
138+ message : { err : "Error deleting Task" } ,
139+ } ) ;
140+ }
141+ } ,
142+ clearTask : async ( _req : Request , res : Response , next : NextFunction ) => {
143+ try {
144+ await Card . deleteMany ( {
145+ _id : {
146+ $in : [
147+ ...res . locals . board . backlog ,
148+ ...res . locals . board . inProgress ,
149+ ...res . locals . board . inReview ,
150+ ...res . locals . board . completed ,
151+ ] ,
152+ } ,
153+ } ) ;
154+ return next ( ) ;
155+ } catch ( err ) {
156+ // pass error through to global error handler
157+ return next ( {
158+ log : `tasksController.clearTask ERROR: ${ err } ` ,
159+ status : 500 ,
160+ message : { err : "Error clearing Tasks" } ,
161+ } ) ;
162+ }
163+ } ,
72164} ;
73165
74166export default tasksController ;
0 commit comments