From 42ae31444bf194761aff499d050f3d0dff7dffd9 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 20 Dec 2024 17:55:08 -0800 Subject: [PATCH] Further consolidate search deletes with other search commands --- api/routes/update.ts | 41 +++++++++++++++++++++------------ api/stately/searches-queries.ts | 1 + 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/api/routes/update.ts b/api/routes/update.ts index 9dae50e..4552c38 100644 --- a/api/routes/update.ts +++ b/api/routes/update.ts @@ -1,5 +1,5 @@ import { captureMessage } from '@sentry/node'; -import { chunk, groupBy, sortBy } from 'es-toolkit'; +import { chunk, groupBy, partition, sortBy } from 'es-toolkit'; import { isEmpty } from 'es-toolkit/compat'; import express from 'express'; import asyncHandler from 'express-async-handler'; @@ -286,8 +286,9 @@ async function statelyUpdate( platformMembershipId: string | undefined, destinyVersion: DestinyVersion, ) { - // We want to group save search and search updates together - const actionKey = (u: ProfileUpdate) => (u.action === 'save_search' ? 'search' : u.action); + // We want to group save/delete search and search updates together + const actionKey = (u: ProfileUpdate) => + u.action === 'save_search' || u.action === 'delete_search' ? 'search' : u.action; const sortedUpdates = sortBy(updates, [actionKey]).flatMap((u): ProfileUpdate[] => { // Separate out tag_cleanup updates into individual updates @@ -382,20 +383,22 @@ async function statelyUpdate( // saved searches and used searches are collectively "searches" case 'search': { const searchUpdates = consolidateSearchUpdates( - group as (UsedSearchUpdate | SavedSearchUpdate)[], + group as (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[], ); - await updateSearches(txn, platformMembershipId!, destinyVersion, searchUpdates); + const [deletes, updates] = partition(searchUpdates, (u) => u.deleted); + if (deletes.length) { + await deleteSearchInStately( + txn, + platformMembershipId!, + destinyVersion, + deletes.map((u) => u.query), + ); + } + if (updates.length) { + await updateSearches(txn, platformMembershipId!, destinyVersion, updates); + } break; } - - case 'delete_search': - await deleteSearchInStately( - txn, - platformMembershipId!, - destinyVersion, - (group as DeleteSearchUpdate[]).map((u) => u.payload.query), - ); - break; } } }); @@ -815,18 +818,26 @@ async function updateItemHashTag( metrics.timing('update.updateItemHashTag', start); } -function consolidateSearchUpdates(updates: (UsedSearchUpdate | SavedSearchUpdate)[]) { +function consolidateSearchUpdates( + updates: (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[], +) { const updatesByQuery = groupBy(updates, (u) => u.payload.query); return Object.values(updatesByQuery).map((group) => { const u: UpdateSearch = { query: group[0].payload.query, type: group[0].payload.type ?? SearchType.Item, incrementUsed: 0, + deleted: false, }; for (const update of group) { if (update.action === 'save_search') { + u.deleted = false; u.saved = update.payload.saved; + } else if (update.action === 'delete_search') { + u.deleted = true; + u.incrementUsed = 0; } else { + u.deleted = false; u.incrementUsed++; } } diff --git a/api/stately/searches-queries.ts b/api/stately/searches-queries.ts index 9ec3104..0d215c6 100644 --- a/api/stately/searches-queries.ts +++ b/api/stately/searches-queries.ts @@ -125,6 +125,7 @@ export interface UpdateSearch { saved?: boolean; /** How much to increment the used count by. */ incrementUsed: number; + deleted: boolean; } /**