File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11email
2- j.avni @iitg.ac.in
2+ aditya.samal @iitg.ac.in
Original file line number Diff line number Diff line change @@ -5,37 +5,22 @@ const updateBRs = async (req, res) => {
55 try {
66 const { emails } = req . body ;
77
8- if ( ! emails || emails . length < = 0 ) {
8+ if ( ! emails || emails . length == = 0 ) {
99 return res . status ( 400 ) . json ( { error : "emails are required" } ) ;
1010 }
1111
12- const alreadyExists = [ ] ;
13- const notUser = [ ] ;
12+ for ( const { email } of emails ) {
13+ const user = await User . findOne ( { email } ) ;
1414
15- for ( const emailobj of emails ) {
16- const email = emailobj . email ;
17- const user = await User . findOne ( { email : email } ) ;
1815 if ( user ) {
19- user . isBR = true ;
20- await user . save ( ) ;
16+ if ( ! user . isBR ) {
17+ user . isBR = true ;
18+ await user . save ( ) ;
19+ }
20+ await BR . updateOne ( { email } , { $set : { email } } , { upsert : true } ) ;
2121 } else {
22- notUser . push ( email ) ;
23- continue ;
22+ await BR . updateOne ( { email } , { $set : { email } } , { upsert : true } ) ;
2423 }
25- const exists = await BR . findOne ( { email } ) ;
26- if ( exists ) {
27- alreadyExists . push ( email ) ;
28- continue ;
29- }
30- await BR . create ( { email } ) ;
31- }
32-
33- if ( alreadyExists . length > 0 || notUser . length > 0 ) {
34- return res . status ( 409 ) . json ( {
35- error : "Some BRs already exist or do not exist in users" ,
36- existingEmails : alreadyExists ,
37- notInUsers : notUser ,
38- } ) ;
3924 }
4025
4126 res . status ( 201 ) . json ( { message : "BRs updated successfully" } ) ;
You can’t perform that action at this time.
0 commit comments