Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sai Venkatesh- Backend - Improve speed and function of deleting multiple blue squares #1130

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 71 additions & 26 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,37 +1686,82 @@ const userProfileController = function (UserProfile, Project) {
});
};

const deleteInfringements = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'deleteInfringements'))) {
res.status(403).send('You are not authorized to delete blue square');
return;
}
const { userId, blueSquareId } = req.params;
// console.log(userId, blueSquareId);
// const deleteInfringements = async function (req, res) {
// if (!(await hasPermission(req.body.requestor, 'deleteInfringements'))) {
// res.status(403).send('You are not authorized to delete blue square');
// return;
// }
// const { userId, blueSquareId } = req.params;
// // console.log(userId, blueSquareId);

// UserProfile.findById(userId, async (err, record) => {
// if (err || !record) {
// res.status(404).send('No valid records found');
// return;
// }

// const originalinfringements = record?.infringements ?? [];

// record.infringements = originalinfringements.filter(
// (infringement) => !infringement._id.equals(blueSquareId),
// );

// record
// .save()
// .then((results) => {
// userHelper.notifyInfringements(originalinfringements, results.infringements);
// res.status(200).json({
// _id: record._id,
// });
// })
// .catch((error) => res.status(400).send(error));
// });
// };
SFA23SCM35V marked this conversation as resolved.
Show resolved Hide resolved

UserProfile.findById(userId, async (err, record) => {
if (err || !record) {
res.status(404).send('No valid records found');
return;
}

/*
Used async/await consistently, removing the callback approach from findById.
Simplified error handling by wrapping the entire block in a try/catch.
Replaced redundant return statements with direct res responses for permission and record checks.
Improved readability and structure by removing unnecessary comments and logs.

const originalinfringements = record?.infringements ?? [];
*/
SFA23SCM35V marked this conversation as resolved.
Show resolved Hide resolved

record.infringements = originalinfringements.filter(
(infringement) => !infringement._id.equals(blueSquareId),
const deleteInfringements = async (req, res) => {
try {
// Check permissions
if (!(await hasPermission(req.body.requestor, 'deleteInfringements'))) {
return res.status(403).send('You are not authorized to delete blue square');
}

const { userId, blueSquareId } = req.params;
const record = await UserProfile.findById(userId).exec();

// Check if the record exists
if (!record) {
return res.status(404).send('No valid records found');
}

const originalInfringements = record.infringements || [];

// Filter out the infringement to be deleted
record.infringements = originalInfringements.filter(
(infringement) => !infringement._id.equals(blueSquareId)
);

record
.save()
.then((results) => {
userHelper.notifyInfringements(originalinfringements, results.infringements);
res.status(200).json({
_id: record._id,
});
})
.catch((error) => res.status(400).send(error));
});

// Save the updated record
const updatedRecord = await record.save();

// Notify about the updated infringements
userHelper.notifyInfringements(originalInfringements, updatedRecord.infringements);

// Respond with success
res.status(200).json({ _id: updatedRecord._id });
} catch (error) {
res.status(400).send(error.message);
SFA23SCM35V marked this conversation as resolved.
Show resolved Hide resolved
}
};


const getProjectsByPerson = async function (req, res) {
try {
Expand Down