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.
This commit is contained in:
Emi Matchu 2024-04-17 09:37:50 -07:00
parent 242665bd02
commit 37bac38973

View file

@ -1018,12 +1018,45 @@ const resolvers = {
return null; return null;
} }
await db.query( 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 `DELETE FROM closet_hangers
WHERE item_id = ? AND user_id = ? AND owned = ?;`, WHERE item_id = ? AND user_id = ? AND owned = ?;`,
[itemId, currentUserId, true], [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 }; return { id: itemId };
}, },
addToItemsCurrentUserWants: async ( addToItemsCurrentUserWants: async (
@ -1083,12 +1116,45 @@ const resolvers = {
return null; return null;
} }
await db.query( 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 `DELETE FROM closet_hangers
WHERE item_id = ? AND user_id = ? AND owned = ?;`, WHERE item_id = ? AND user_id = ? AND owned = ?;`,
[itemId, currentUserId, false], [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 }; return { id: itemId };
}, },
addItemToClosetList: async ( addItemToClosetList: async (