Skip to content

Commit

Permalink
Handle side effects for pending items
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed May 23, 2024
1 parent fb43c48 commit afc4864
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 10 additions & 3 deletions api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 10 additions & 1 deletion worker/action.js
Original file line number Diff line number Diff line change
@@ -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 } })
Expand Down Expand Up @@ -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 })
}
}

Expand All @@ -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`
]
}

Expand Down

0 comments on commit afc4864

Please sign in to comment.