aboutsummaryrefslogtreecommitdiffstats
path: root/routes/invoices.ts
blob: 4f0f6febc1237b4998c8ec41089574a100f2234b (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
33
34
35
36
37
38
39
40
41
42
43
44
import { g } from "$lib/db";
import { generate } from "$lib/invoices";
import { err } from "$lib/logging";
import { bail, pick } from "$lib/utils";

export default {
  async get(req, res) {
    const {
      params: { id },
    } = req;
    let invoice = await g(`invoice:${id}`);
    if (typeof invoice === "string") invoice = await g(`invoice:${invoice}`);

    if (invoice) {
      invoice.secret = undefined;
      invoice.user = pick(await g(`user:${invoice.uid}`), [
        "id",
        "profile",
        "banner",
        "currency",
        "username",
        "pubkey",
      ]);

      invoice.items ||= [];
    }
    if (invoice) res.send(invoice);
    else res.code(500).send("invoice not found");
  },

  async create(req, res) {
    let { body, user } = req;
    if (body.user) user = body.user;
    const { invoice } = body;

    try {
      res.send(await generate({ invoice, user }));
    } catch (e) {
      console.log(e);
      err("problem generating invoice", e.message);
      bail(res, e.message);
    }
  },
};