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

consider removal of duplicates of all fields and not just selected_sites #4074

Merged
merged 2 commits into from
Dec 13, 2024
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
14 changes: 4 additions & 10 deletions src/auth-service/utils/create-preference.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ const prepareUpdate = (body, fieldsToUpdate, fieldsToAddToSet) => {
: [update[field]];

// Remove duplicates for specific fields
const uniqueArray =
field === "selected_sites"
? removeDuplicates(processedArray)
: processedArray;

const uniqueArray = removeDuplicates(processedArray);
update["$set"] = update["$set"] || {};
update["$set"][field] = uniqueArray;
delete update[field];
Expand All @@ -97,10 +93,7 @@ const prepareUpdate = (body, fieldsToUpdate, fieldsToAddToSet) => {
}));

// Remove duplicates for specific fields
const uniqueArray =
field === "selected_sites"
? removeDuplicates(processedArray)
: processedArray;
const uniqueArray = removeDuplicates(processedArray);

update["$set"] = update["$set"] || {};
update["$set"][field] = uniqueArray;
Expand Down Expand Up @@ -392,7 +385,8 @@ const preferences = {
};
}

const update = prepareUpdate(body, fieldsToUpdate, fieldsToAddToSet);
// const update = prepareUpdate(body, fieldsToUpdate, fieldsToAddToSet);
const update = body;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Bypassing duplicate removal in replace method

The direct assignment of body to update bypasses the prepareUpdate function, which:

  1. Contradicts the PR's objective of handling duplicates across all fields
  2. Skips important field processing and validation
  3. Could lead to data inconsistency

Restore the duplicate removal functionality by applying this diff:

-// const update = prepareUpdate(body, fieldsToUpdate, fieldsToAddToSet);
-const update = body;
+const update = prepareUpdate(body, fieldsToUpdate, fieldsToAddToSet);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// const update = prepareUpdate(body, fieldsToUpdate, fieldsToAddToSet);
const update = body;
const update = prepareUpdate(body, fieldsToUpdate, fieldsToAddToSet);

const options = { upsert: true, new: true };

const modifyResponse = await PreferenceModel(tenant).findOneAndUpdate(
Expand Down
Loading