aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/invoice/+page.server.ts
blob: b8a2fe066f12131c82f8f089d9ca02889d39c4a4 (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
import { auth, post } from "$lib/utils";
import { error, redirect } from "@sveltejs/kit";
import type { Invoice } from "$lib/types";

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

  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");
  }

  let { id } = invoice;

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