Skip to content

Commit 5d9ff51

Browse files
committed
BR Update logic fixed
1 parent 1d33831 commit 5d9ff51

2 files changed

Lines changed: 10 additions & 25 deletions

File tree

admin/public/BR.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
email
2-
j.avni@iitg.ac.in
2+
aditya.samal@iitg.ac.in

server/modules/br/br.controller.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff 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" });

0 commit comments

Comments
 (0)