From afc4864d34c7f5ca70daf622f92ce4b54868f673 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 23 May 2024 01:11:40 -0500 Subject: [PATCH] Handle side effects for pending items --- api/resolvers/item.js | 13 ++++++++++--- worker/action.js | 11 ++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 894d26d23..413bed5c1 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -1437,15 +1437,22 @@ export const createItem = async (parent, { forward, options, ...item }, { me, mo item.invoice = invoice } - await enqueueDeletionJob(item, models) + item.comments = [] - // TODO: don't notify users on pending items + // we need to insert these even for pending items to show toasts + // we will delete the jobs if payment failed + await enqueueDeletionJob(item, models) await createReminderAndJob({ me, item, models }) + + const isPending = !!item.invoice + if (isPending) { + return item + } + await createMentions(item, models) notifyUserSubscribers({ models, item }) notifyTerritorySubscribers({ models, item }) - item.comments = [] return item } diff --git a/worker/action.js b/worker/action.js index 0d7b9640a..5407e45ba 100644 --- a/worker/action.js +++ b/worker/action.js @@ -1,4 +1,6 @@ +import { createMentions } from '@/api/resolvers/item' import serialize from '@/api/resolvers/serial' +import { notifyTerritorySubscribers, notifyUserSubscribers } from '@/lib/webPush' export async function finalizeAction ({ data: { hash }, models }) { const invoice = await models.invoice.findUnique({ where: { hash } }) @@ -53,6 +55,10 @@ export async function handleAction ({ data: { actionType, actionId, actionData } item.boost > 0 && models.$executeRaw(`SELECT item_act(${item.id}::INTEGER, ${item.userId}::INTEGER, 'BOOST'::"ItemActType", ${item.boost}::INTEGER)`), item.maxBid && models.$executeRaw(`SELECT run_auction(${item.id}::INTEGER)`) ], { models }) + + await createMentions(item, models) + notifyUserSubscribers({ models, item }) + notifyTerritorySubscribers({ models, item }) } } @@ -64,7 +70,10 @@ export function actionErrorQueries ({ data: { actionType, actionId }, models }) models.$queryRaw` UPDATE "Item" SET status = 'FAILED' - WHERE id = ${actionId} AND status = 'PENDING'` + WHERE id = ${actionId} AND status = 'PENDING'`, + models.$queryRaw`DELETE FROM "Reminder" WHERE "itemId" = ${actionId}`, + models.$queryRaw`DELETE FROM pgboss.job WHERE name = 'reminder' AND data->>'itemId' = ${actionId}::text`, + models.$queryRaw`DELETE FROM pgboss.job WHERE name = 'deleteItem' AND data->>'id' = ${actionId}::text` ] }