aboutsummaryrefslogtreecommitdiffstats
path: root/routes/invoices.js
blob: 898938ddc072bc2b15bb5a8ed46818f059281a2c (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { g, s } from "$lib/db";
import { generate } from "$lib/invoices";
import { bail, fail, pick } from "$lib/utils";
import whitelist from "$lib/whitelist";
import got from "got";
import config from "$config";
import ln from "$lib/ln";

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

    if (invoice) {
      delete invoice.secret;
      invoice.user = pick(await g(`user:${invoice.uid}`), [
        "id",
        "profile",
        "banner",
        "currency",
        "username",
        "pubkey",
      ]);
    } else if (config.classic) {
      invoice = await got(`${config.classic}/invoice/${id}`).json();
      if (invoice) {
        invoice.id = invoice.uuid;
        invoice.classic = true;
        invoice.user.id = invoice.user.uuid;
        invoice.user.username += "@classic";
      }
    }

    if (invoice) res.send(invoice);
    else res.code(500).send("invoice not found");
  },

  async create({ body: { invoice, user }, user: sender }, res) {
    try {
      res.send(await generate({ invoice, user, sender }));
    } catch (e) {
      console.log(e);
      bail(res, e.message);
    }
  },

  async classic({ params: { username } }, res) {
    let invoice = await got
      .post(`${config.classic}/invoice`, {
        json: {
          invoice: { amount: 0, network: "lightning" },
          user: { username },
        },
      })
      .json();

    res.send(invoice);
  },
};