aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/invoice/+page.server.ts
blob: 64c49ebc1934bdad06430404c995245d85bb776a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import getRates from "$lib/rates";
import type { Invoice } from "$lib/types";
import { auth, post } from "$lib/utils";
import { error, redirect } from "@sveltejs/kit";

export const load = async ({ cookies, parent }) => {
	const aid = cookies.get("aid");
	let { subject, user } = await parent();

	const rates = await getRates();
	if (aid === user.id) redirect(307, `/${user.username}/receive`);

	let invoice: Invoice = {
		aid,
		rate: rates[user?.currency || subject?.currency],
	};

	if (!user) user = subject;

	try {
		invoice = await post("/invoice", { invoice, aid, user }, auth(cookies));
	} catch (e) {
		console.log(e);
		error(500, "Failed to generate invoice");
	}

	const { id } = invoice;

	if (invoice.memoPrompt && !invoice.memo) {
		redirect(307, `/invoice/${id}/memo`);
	} else redirect(307, `/invoice/${id}`);
};