-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: introduce Opportunity creation for Broken Backlinks #475
Changes from all commits
e42c99a
5e292e6
23222a4
388ea88
a6ebd99
4f8447c
9e18fb9
719e169
820a6da
ab68d5e
3fc08cc
e48b890
a0a4d6f
2bad3a6
17117b8
dbfac63
0253e6b
ae57409
f812826
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,70 @@ export async function retrieveSiteBySiteId(dataAccess, siteId, log) { | |
throw new Error(`Error getting site ${siteId}: ${e.message}`); | ||
} | ||
} | ||
|
||
/** | ||
* Synchronizes existing suggestions with new data by removing outdated suggestions | ||
* and adding new ones. | ||
* | ||
* @param {Object} params - The parameters for the sync operation. | ||
* @param {Object} params.opportunity - The opportunity object to synchronize suggestions for. | ||
* @param {Array} params.newData - Array of new data objects to sync. | ||
* @param {Function} params.buildKey - Function to generate a unique key for each item. | ||
* @param {Function} params.mapNewSuggestion - Function to map new data to suggestion objects. | ||
* @param {Object} params.log - Logger object for error reporting. | ||
* @returns {Promise<void>} - Resolves when the synchronization is complete. | ||
*/ | ||
export async function syncSuggestions({ | ||
opportunity, | ||
newData, | ||
buildKey, | ||
mapNewSuggestion, | ||
log, | ||
}) { | ||
const newDataKeys = new Set(newData.map(buildKey)); | ||
const existingSuggestions = await opportunity.getSuggestions(); | ||
|
||
// Remove outdated suggestions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the existing suggestion was entered via back-office-ui as the source, we should NOT delete it, irrespective of whether the same suggestion exists in the new audit or not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will handles this as a next step. This will require an additional property in the data model |
||
await Promise.all( | ||
existingSuggestions | ||
.filter((existing) => !newDataKeys.has(buildKey(existing))) | ||
.map((suggestion) => suggestion.remove()), | ||
); | ||
|
||
// Update existing suggestions | ||
await Promise.all( | ||
existingSuggestions | ||
.filter((existing) => { | ||
const existingKey = buildKey(existing); | ||
return newDataKeys.has(existingKey); | ||
}) | ||
.map((existing) => { | ||
const newDataItem = newData.find((data) => buildKey(data) === buildKey(existing)); | ||
existing.setData(newDataItem); | ||
return existing.save(); | ||
}), | ||
); | ||
|
||
// Prepare new suggestions | ||
const newSuggestions = newData | ||
.filter((data) => !existingSuggestions.some( | ||
(existing) => buildKey(existing) === buildKey(data), | ||
)) | ||
.map(mapNewSuggestion); | ||
|
||
// Add new suggestions if any | ||
dzehnder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (newSuggestions.length > 0) { | ||
const suggestions = await opportunity.addSuggestions(newSuggestions); | ||
|
||
if (suggestions.errorItems?.length > 0) { | ||
log.error(`Suggestions for siteId ${opportunity.getSiteId()} contains ${suggestions.errorItems.length} items with errors`); | ||
suggestions.errorItems.forEach((errorItem) => { | ||
log.error(`Item ${JSON.stringify(errorItem.item)} failed with error: ${errorItem.error}`); | ||
}); | ||
|
||
if (suggestions.createdItems?.length <= 0) { | ||
throw new Error(`Failed to create suggestions for siteId ${opportunity.getSiteId()}`); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯