|
1 | 1 | import { captureMessage } from '@sentry/node'; |
2 | | -import { chunk, groupBy, sortBy } from 'es-toolkit'; |
| 2 | +import { chunk, groupBy, partition, sortBy } from 'es-toolkit'; |
3 | 3 | import { isEmpty } from 'es-toolkit/compat'; |
4 | 4 | import express from 'express'; |
5 | 5 | import asyncHandler from 'express-async-handler'; |
@@ -286,8 +286,9 @@ async function statelyUpdate( |
286 | 286 | platformMembershipId: string | undefined, |
287 | 287 | destinyVersion: DestinyVersion, |
288 | 288 | ) { |
289 | | - // We want to group save search and search updates together |
290 | | - const actionKey = (u: ProfileUpdate) => (u.action === 'save_search' ? 'search' : u.action); |
| 289 | + // We want to group save/delete search and search updates together |
| 290 | + const actionKey = (u: ProfileUpdate) => |
| 291 | + u.action === 'save_search' || u.action === 'delete_search' ? 'search' : u.action; |
291 | 292 |
|
292 | 293 | const sortedUpdates = sortBy(updates, [actionKey]).flatMap((u): ProfileUpdate[] => { |
293 | 294 | // Separate out tag_cleanup updates into individual updates |
@@ -382,20 +383,22 @@ async function statelyUpdate( |
382 | 383 | // saved searches and used searches are collectively "searches" |
383 | 384 | case 'search': { |
384 | 385 | const searchUpdates = consolidateSearchUpdates( |
385 | | - group as (UsedSearchUpdate | SavedSearchUpdate)[], |
| 386 | + group as (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[], |
386 | 387 | ); |
387 | | - await updateSearches(txn, platformMembershipId!, destinyVersion, searchUpdates); |
| 388 | + const [deletes, updates] = partition(searchUpdates, (u) => u.deleted); |
| 389 | + if (deletes.length) { |
| 390 | + await deleteSearchInStately( |
| 391 | + txn, |
| 392 | + platformMembershipId!, |
| 393 | + destinyVersion, |
| 394 | + deletes.map((u) => u.query), |
| 395 | + ); |
| 396 | + } |
| 397 | + if (updates.length) { |
| 398 | + await updateSearches(txn, platformMembershipId!, destinyVersion, updates); |
| 399 | + } |
388 | 400 | break; |
389 | 401 | } |
390 | | - |
391 | | - case 'delete_search': |
392 | | - await deleteSearchInStately( |
393 | | - txn, |
394 | | - platformMembershipId!, |
395 | | - destinyVersion, |
396 | | - (group as DeleteSearchUpdate[]).map((u) => u.payload.query), |
397 | | - ); |
398 | | - break; |
399 | 402 | } |
400 | 403 | } |
401 | 404 | }); |
@@ -815,18 +818,26 @@ async function updateItemHashTag( |
815 | 818 | metrics.timing('update.updateItemHashTag', start); |
816 | 819 | } |
817 | 820 |
|
818 | | -function consolidateSearchUpdates(updates: (UsedSearchUpdate | SavedSearchUpdate)[]) { |
| 821 | +function consolidateSearchUpdates( |
| 822 | + updates: (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[], |
| 823 | +) { |
819 | 824 | const updatesByQuery = groupBy(updates, (u) => u.payload.query); |
820 | 825 | return Object.values(updatesByQuery).map((group) => { |
821 | 826 | const u: UpdateSearch = { |
822 | 827 | query: group[0].payload.query, |
823 | 828 | type: group[0].payload.type ?? SearchType.Item, |
824 | 829 | incrementUsed: 0, |
| 830 | + deleted: false, |
825 | 831 | }; |
826 | 832 | for (const update of group) { |
827 | 833 | if (update.action === 'save_search') { |
| 834 | + u.deleted = false; |
828 | 835 | u.saved = update.payload.saved; |
| 836 | + } else if (update.action === 'delete_search') { |
| 837 | + u.deleted = true; |
| 838 | + u.incrementUsed = 0; |
829 | 839 | } else { |
| 840 | + u.deleted = false; |
830 | 841 | u.incrementUsed++; |
831 | 842 | } |
832 | 843 | } |
|
0 commit comments