11const jwt = require ( 'jsonwebtoken' ) ;
2+ const { ObjectId } = require ( 'mongodb' ) ;
23
34const EmailController = require ( './email.controller' ) ;
45const { CONFIG_AUTH } = require ( '../config' ) ;
@@ -311,7 +312,7 @@ UserController.updateManagedProjects = async function (req, res) {
311312 managedByUsers = managedByUsers . filter ( ( id ) => id !== UserId ) ;
312313 }
313314
314- // Update user's managedProjects
315+ // Update user's managedProjects
315316 user . managedProjects = managedProjects ;
316317 await user . save ( { validateBeforeSave : false } ) ;
317318
@@ -328,6 +329,23 @@ UserController.updateManagedProjects = async function (req, res) {
328329
329330UserController . bulkUpdateManagedProjects = async function ( req , res ) {
330331 const { bulkOps } = req . body ;
332+
333+ // Convert string IDs to ObjectId in bulkOps
334+ bulkOps . forEach ( ( op ) => {
335+ if ( op ?. updateOne ?. filter . _id ) {
336+ op . updateOne . filter . _id = ObjectId ( op . updateOne . filter . _id ) ;
337+ }
338+ if ( op ?. updateOne ?. update ) {
339+ const update = op . updateOne . update ;
340+ if ( update ?. $addToSet ?. managedProjects ) {
341+ update . $addToSet . managedProjects = ObjectId ( update . $addToSet . managedProjects ) ;
342+ }
343+ if ( update ?. $pull ?. managedProjects ) {
344+ update . $pull . managedProjects = ObjectId ( update . $pull . managedProjects ) ;
345+ }
346+ }
347+ } ) ;
348+
331349 try {
332350 const result = await User . bulkWrite ( bulkOps ) ;
333351 res . status ( 200 ) . json ( result ) ;
0 commit comments