Skip to content

Commit

Permalink
refactor(voucher): resolvers in saperator files (#4507)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 authored Jun 21, 2024
1 parent 2294dd5 commit 1ea9cfb
Show file tree
Hide file tree
Showing 9 changed files with 514 additions and 402 deletions.
10 changes: 8 additions & 2 deletions apps/voucher/app/api/lnurlw/[unique-hash]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ export async function GET(
return Response.json({ error: "Withdraw link not found", status: 404 })
if (withdrawLink instanceof Error)
return Response.json({ error: "Internal Server Error", status: 500 })
if (withdrawLink.status === Status.Paid)
return Response.json({ error: "Withdraw link claimed", status: 500 })
if (withdrawLink.status === Status.Pending)
return Response.json({
error:
"Withdrawal link is in pending state. Please contact support if the error persists.",
status: 500,
})
if (withdrawLink.status !== Status.Active)
return Response.json({ error: "Withdraw link is not Active", status: 500 })

const client = escrowApolloClient()
const realTimePriceResponse = await getRealtimePriceQuery({
Expand Down
27 changes: 20 additions & 7 deletions apps/voucher/app/api/lnurlw/callback/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri

try {
const withdrawLink = await getWithdrawLinkByK1Query({ k1 })
if (!withdrawLink) {

if (!withdrawLink)
return Response.json({ status: "ERROR", reason: "Withdraw link not found" })
}

if (withdrawLink instanceof Error)
return Response.json({ status: "ERROR", reason: "Internal Server Error" })

if (withdrawLink.id !== id)
return Response.json({ status: "ERROR", reason: "Invalid Request" })

// locking so some else can't use this link at the same time
if (withdrawLink.status === Status.Paid)
return Response.json({ status: "ERROR", reason: "Withdraw link claimed" })
if (withdrawLink.status === Status.Pending)
return Response.json({
status: "ERROR",
reason:
"Withdrawal link is in pending state. Please contact support if the error persists.",
})

if (withdrawLink.status !== Status.Active)
return Response.json({ status: "ERROR", reason: "Withdraw link is not Active" })

const client = escrowApolloClient()

Expand Down Expand Up @@ -78,8 +84,7 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
// }
// }

await updateWithdrawLinkStatus({ id, status: Status.Paid })

await updateWithdrawLinkStatus({ id, status: Status.Pending })
const lnInvoicePaymentSendResponse = await lnInvoicePaymentSend({
client,
input: {
Expand Down Expand Up @@ -111,6 +116,13 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
})
}

if (
lnInvoicePaymentSendResponse?.lnInvoicePaymentSend?.status ===
PaymentSendResult.Pending
) {
return Response.json({ status: "ERROR", reason: "Payment went on Pending state" })
}

if (
lnInvoicePaymentSendResponse?.lnInvoicePaymentSend?.status !==
PaymentSendResult.Success
Expand All @@ -119,6 +131,7 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
return Response.json({ status: "ERROR", reason: "Payment not paid" })
}

await updateWithdrawLinkStatus({ id, status: Status.Paid })
return Response.json({ status: "OK" })
} catch (error) {
console.error("error paying lnurlw", error)
Expand Down
Loading

0 comments on commit 1ea9cfb

Please sign in to comment.