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 2 commits
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
59 changes: 32 additions & 27 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,37 +1686,42 @@ 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);

UserProfile.findById(userId, async (err, record) => {
if (err || !record) {
res.status(404).send('No valid records found');
return;
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 originalinfringements = record?.infringements ?? [];

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

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
Loading