From 37bac3897348e31d3246cababf62f28fcc10ebad Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Wed, 17 Apr 2024 09:37:50 -0700 Subject: [PATCH] Oops, fix list caching when removing items via Impress 2020 I realized this months ago when adding `updated_at` changes to the other actions around here, and I just forgot this one, and I've had a sticky on my desk about it since then! Finally done! Before this change, clicking "I own this" or "I want this" on the item page, while you own/want the item and it's in a specific list, would remove the item from the list but *not* update the list's `updated_at` timestamp. Now, it does! This affects caching on main DTI, which relies on the list's `updated_at` to decide when to render new HTML. --- src/server/types/Item.js | 86 +++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 10 deletions(-) diff --git a/src/server/types/Item.js b/src/server/types/Item.js index 58f13c5..f375f5a 100644 --- a/src/server/types/Item.js +++ b/src/server/types/Item.js @@ -1018,11 +1018,44 @@ const resolvers = { return null; } - await db.query( - `DELETE FROM closet_hangers - WHERE item_id = ? AND user_id = ? AND owned = ?;`, - [itemId, currentUserId, true], - ); + const connection = await db.getConnection(); + try { + // Get the relevant lists this is currently in. + await connection.beginTransaction(); + const [rows] = await connection.query( + `SELECT DISTINCT list_id FROM closet_hangers + WHERE item_id = ? AND user_id = ? AND owned = ? AND + list_id IS NOT NULL`, + [itemId, currentUserId, true], + ); + + // Mark all these lists as updated. + const listIds = rows.map((row) => row.list_id); + const qs = listIds.map((_) => "?"); + const now = new Date(); + await connection.query( + `UPDATE closet_lists SET updated_at = ? WHERE id IN (${qs})`, + [now, ...listIds], + ); + + // Delete all these hangers. (NOTE: This includes ones not in a list!) + await connection.query( + `DELETE FROM closet_hangers + WHERE item_id = ? AND user_id = ? AND owned = ?;`, + [itemId, currentUserId, true], + ); + + await connection.commit(); + } catch (error) { + try { + await connection.rollback(); + } catch (error2) { + console.warn(`Error rolling back transaction`, error2); + } + throw error; + } finally { + await connection.release(); + } return { id: itemId }; }, @@ -1083,11 +1116,44 @@ const resolvers = { return null; } - await db.query( - `DELETE FROM closet_hangers - WHERE item_id = ? AND user_id = ? AND owned = ?;`, - [itemId, currentUserId, false], - ); + const connection = await db.getConnection(); + try { + // Get the relevant lists this is currently in. + await connection.beginTransaction(); + const [rows] = await connection.query( + `SELECT DISTINCT list_id FROM closet_hangers + WHERE item_id = ? AND user_id = ? AND owned = ? AND + list_id IS NOT NULL`, + [itemId, currentUserId, false], + ); + + // Mark all these lists as updated. + const listIds = rows.map((row) => row.list_id); + const qs = listIds.map((_) => "?"); + const now = new Date(); + await connection.query( + `UPDATE closet_lists SET updated_at = ? WHERE id IN (${qs})`, + [now, ...listIds], + ); + + // Delete all these hangers. (NOTE: This includes ones not in a list!) + await connection.query( + `DELETE FROM closet_hangers + WHERE item_id = ? AND user_id = ? AND owned = ?;`, + [itemId, currentUserId, false], + ); + + await connection.commit(); + } catch (error) { + try { + await connection.rollback(); + } catch (error2) { + console.warn(`Error rolling back transaction`, error2); + } + throw error; + } finally { + await connection.release(); + } return { id: itemId }; },