aboutsummaryrefslogtreecommitdiffstats
path: root/routes/invoices.js
blob: dc0650d15f888981850717be0424466824744fab (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
60
const { Op } = require("sequelize");

app.get("/invoice", auth, ah(async (req, res, next) => {
  try {
    const invoice = await db.Invoice.findOne({
      where: {
        uuid: req.query.uuid,
      },
    });

    res.send(invoice);
  } catch (e) {
    l.error("couldn't find invoice", e);
  }
}));

app.post("/invoice", ah(async (req, res, next) => {
  let { invoice, user } = req.body;
  if (!user) ({ user } = req);
  else {
    user = await db.User.findOne({
      where: {
        username: user.username,
      } 
    }); 
  } 
  if (!user) throw new Error("user not provided");
  invoice.user_id = user.id;
  invoice.account_id = user.account_id;

  l.info(
    "creating invoice",
    user.username,
    invoice.network,
    invoice.amount,
    invoice.tip,
    invoice.currency,
    invoice.rate.toFixed(2)
  );

  if (invoice.network === "LBTC") {
    invoice.unconfidential = (
      await lq.getAddressInfo(invoice.address)
    ).unconfidential;
  }

  const exists = await db.Invoice.findOne({
    where: {
      [Op.or]: {
        address: invoice.address || "undefined",
        text: invoice.text,
      },
    },
  });

  invoice = exists ? await exists.update(invoice) : await db.Invoice.create(invoice);
  addresses[invoice.address] = user.username;
  if (invoice.unconfidential) addresses[invoice.unconfidential] = user.username;
  res.send(invoice);
}));