Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: checkpoint 2 #2

Open
wants to merge 1 commit into
base: checkpoint-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions client/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ export const getInvoices = async (): Promise<GetInvoicesResponse> =>
fetch(`${API_ENDPOINT}/api/invoicesForPlayers`)
.then((a) => a.json())
.then(GetInvoicesResponse.parse);

type GetInvoiceStatusResponse = z.infer<typeof GetInvoiceStatusResponse>;

const GetInvoiceStatusResponse = z.object({
paid: z.boolean(),
})

export const getInvoiceStatus = async (id: number): Promise<GetInvoiceStatusResponse> =>
fetch(`${API_ENDPOINT}/api/invoiceStatus/${id}`)
.then((a) => a.json())
.then(GetInvoiceStatusResponse.parse)
35 changes: 32 additions & 3 deletions client/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { goto } from "$app/navigation";
import { onMount } from "svelte";
import type { PageServerData } from "./$types";
import { getInvoiceStatus } from "$lib";

export let data: PageServerData;

Expand All @@ -10,11 +11,25 @@
let invoicesPaid: boolean = false;

onMount(() => {
console.log("TODO");
const interval = setInterval(async () => {
await checkInvoices();

if (invoicesPaid) {
clearInterval(interval);
}
}, 1000 * 10);
});

async function checkInvoices(): Promise<void> {
console.log("TODO");
if (!data.playerOne || !data.playerTwo) {
return;
}
const playerOneInvoicePaid = await getInvoiceStatus(data.playerOne.id);
const playerTwoInvoicePaid = await getInvoiceStatus(data.playerTwo.id);

invoicesPaid = playerOneInvoicePaid.paid && playerTwoInvoicePaid.paid;

return;
}

function copyToClipboard(payRequest: string | undefined) {
Expand All @@ -28,7 +43,21 @@
}

function submit() {
console.log("TODO");
if (playerOneName === "" || playerTwoName === "") {
return;
}

if (playerOneName.length < 3 || playerTwoName.length < 3) {
return;
}

if (!invoicesPaid) {
return;
}

goto(`/${playerOneName}-${playerTwoName}`);

return;
}

$: disabled = playerOneName === "" || playerTwoName === "" || !invoicesPaid;
Expand Down
20 changes: 19 additions & 1 deletion server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ router.get('/invoicesForPlayers', async (_req, res) => {
});

router.get('/invoiceStatus/:id', async (req, res) => {
console.log("TODO");
try {
console.log('invoiceStatus');

const { id } = req.params;
const idAsInt = parseInt(id);

const invoiceHash = invoicesDb.get(idAsInt);

if (!invoiceHash) {
return res.status(404).send({ error: "Invoice not found" });
}

const status = await getInvoice({ lnd, id: invoiceHash });

return res.status(200).send({ paid: status.is_confirmed });
} catch (err) {
console.error("invoiceStatus error: ", err);
return res.status(500).send({ error: "Server is dumb" });
}
});

interface PayInvoiceRequest extends Request { body: { payreq: string } };
Expand Down