Skip to content
Open
Changes from all 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
53 changes: 48 additions & 5 deletions src/utils/data-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export const handleOutdatedSuggestions = async ({
}) => {
const { Suggestion } = context.dataAccess;
const { log } = context;

// LOG: All existing suggestions before filtering
const existingSuggestionsData = existingSuggestions.map((s) => ({
key: buildKey(s.getData()), status: s.getStatus(),
}));
log.debug('[TRACKING1] Existing suggestions:', JSON.stringify(existingSuggestionsData, null, 2));

const existingOutdatedSuggestions = existingSuggestions
.filter((existing) => !newDataKeys.has(buildKey(existing.getData())))
.filter((existing) => ![
Expand All @@ -121,6 +128,12 @@ export const handleOutdatedSuggestions = async ({
SuggestionDataAccess.STATUSES.SKIPPED,
].includes(existing.getStatus()));

// LOG: Filtered suggestions that will be marked as outdated
const outdatedSuggestionsData = existingOutdatedSuggestions.map((s) => ({
key: buildKey(s.getData()), status: s.getStatus(),
}));
log.debug('[TRACKING1.1] ToBeOutdated suggestions:', JSON.stringify(outdatedSuggestionsData, null, 2));

// prevents JSON.stringify overflow
log.debug(`Outdated suggestions count: ${existingOutdatedSuggestions.length}`);
if (existingOutdatedSuggestions.length > 0 && existingOutdatedSuggestions.length <= 10) {
Expand Down Expand Up @@ -210,12 +223,26 @@ export async function syncSuggestions({
log.debug(`Existing suggestions = ${existingSuggestions.length}: ${JSON.stringify(existingSuggestions, null, 2)}`);

// Update existing suggestions
// LOG: Existing suggestions before update filtering
const beforeUpdateFilterData = existingSuggestions.map((s) => ({
key: buildKey(s.getData()), status: s.getStatus(),
}));
log.debug('[TRACKING2] BeforeUpdateFilter suggestions:', JSON.stringify(beforeUpdateFilterData, null, 2));

const existingSuggestionsToUpdate = existingSuggestions
.filter((existing) => {
const existingKey = buildKey(existing.getData());
return newDataKeys.has(existingKey);
});

// LOG: Existing suggestions after update filtering
const toBeUpdatedData = existingSuggestionsToUpdate.map((s) => ({
key: buildKey(s.getData()), status: s.getStatus(),
}));
log.debug('[TRACKING2.1] ToBeUpdated suggestionss:', JSON.stringify(toBeUpdatedData, null, 2));

await Promise.all(
existingSuggestions
.filter((existing) => {
const existingKey = buildKey(existing.getData());
return newDataKeys.has(existingKey);
})
existingSuggestionsToUpdate
.map((existing) => {
const newDataItem = newData.find((data) => buildKey(data) === buildKey(existing.getData()));
existing.setData(mergeDataFunction(existing.getData(), newDataItem));
Expand All @@ -236,6 +263,16 @@ export async function syncSuggestions({
// Prepare new suggestions
const { site } = context;
const requiresValidation = Boolean(site?.requiresValidation);

// LOG: Before new suggestions filtering
const newDataForLogging = newData.map((data) => ({ key: buildKey(data) }));
log.debug('[TRACKING3] NewData:', JSON.stringify(newDataForLogging, null, 2));

const existingForBlockingData = existingSuggestions.map((s) => ({
key: buildKey(s.getData()), status: s.getStatus(),
}));
log.debug('[TRACKING3] ExistingForBlocking:', JSON.stringify(existingForBlockingData, null, 2));

const newSuggestions = newData
.filter((data) => !existingSuggestions.some(
(existing) => buildKey(existing.getData()) === buildKey(data),
Expand All @@ -249,6 +286,12 @@ export async function syncSuggestions({
};
});

// LOG: After new suggestions filtering
const finalNewSuggestionsData = newSuggestions.map((s) => ({
key: buildKey(s.data), status: s.status,
}));
log.debug('[TRACKING3.1] FinalNewSuggestions:', JSON.stringify(finalNewSuggestionsData, null, 2));

// Add new suggestions if any
if (newSuggestions.length > 0) {
const suggestions = await opportunity.addSuggestions(newSuggestions);
Expand Down