11const jwt = require ( 'jsonwebtoken' ) ;
2+ const { ObjectId } = require ( 'mongodb' ) ;
23
34const EmailController = require ( './email.controller' ) ;
45const { CONFIG_AUTH } = require ( '../config' ) ;
@@ -27,6 +28,25 @@ UserController.user_list = async function (req, res) {
2728 }
2829} ;
2930
31+ UserController . user_by_email = async function ( req , res ) {
32+ const { headers } = req ;
33+ const { email } = req . params ;
34+
35+ console . log ( 'email: ' , email ) ;
36+
37+ if ( headers [ 'x-customrequired-header' ] !== expectedHeader ) {
38+ return res . sendStatus ( 403 ) ;
39+ }
40+
41+ try {
42+ const user = await User . find ( { email } ) ;
43+ return res . status ( 200 ) . send ( user ) ;
44+ } catch ( err ) {
45+ console . log ( err ) ;
46+ return res . sendStatus ( 400 ) ;
47+ }
48+ } ;
49+
3050// Get list of Users with accessLevel 'admin' or 'superadmin' with GET
3151UserController . admin_list = async function ( req , res ) {
3252 const { headers } = req ;
@@ -104,8 +124,6 @@ UserController.user_by_id = async function (req, res) {
104124
105125 try {
106126 const user = await User . findById ( UserId ) ;
107- // TODO throw 404 if User.findById returns empty object
108- // and look downstream to see whether 404 would break anything
109127 return res . status ( 200 ) . send ( user ) ;
110128 } catch ( err ) {
111129 console . error ( err ) ;
@@ -294,7 +312,7 @@ UserController.updateManagedProjects = async function (req, res) {
294312 managedByUsers = managedByUsers . filter ( ( id ) => id !== UserId ) ;
295313 }
296314
297- // Update user's managedProjects
315+ // Update user's managedProjects
298316 user . managedProjects = managedProjects ;
299317 await user . save ( { validateBeforeSave : false } ) ;
300318
@@ -309,4 +327,31 @@ UserController.updateManagedProjects = async function (req, res) {
309327 }
310328} ;
311329
330+ UserController . bulkUpdateManagedProjects = async function ( req , res ) {
331+ 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+
349+ try {
350+ const result = await User . bulkWrite ( bulkOps ) ;
351+ res . status ( 200 ) . json ( result ) ;
352+ } catch ( err ) {
353+ res . status ( 500 ) . json ( { error : err . message } ) ;
354+ }
355+ } ;
356+
312357module . exports = UserController ;
0 commit comments