aboutsummaryrefslogtreecommitdiffstats
path: root/routes/funding.js
blob: e65a704ef5767910a7458dfa493cae4cb0839816 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { emit } from "$lib/sockets";
import app from "$app";
import config from "$config";
import mailgunFactory from "mailgun-js";
const mailgun = mailgunFactory(config.mailgun);

app.post("/id", auth, upload.single("id"), async (req, res) => {
  const { user } = req;
  if (user.verified === "proof") user.verified = "pending";
  else user.verified = "id";
  await user.save();
  emit(user.username, "user", user);

  let data = {
    subject: "KYC Documents",
    text: `ID uploaded for ${user.username}`,
    from: "[email protected]",
    to: "[email protected]"
  };

  mailgun.messages().send(data);
  l("id uploaded", user.username);

  res.send({});
});

app.post("/proof", auth, upload.single("proof"), async (req, res) => {
  const { user } = req;
  if (user.verified === "id") user.verified = "pending";
  else user.verified = "proof";
  await user.save();
  emit(user.username, "user", user);
  l("id proof uploaded", user.username);

  res.send({});
});

app.post("/funding", auth, async (req, res) => {
  try {
    let { id, amount, code } = req.body;

    if (!code) {
      code = "";
      let d = "ABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
      let l = d.length;
      for (let i = 0; i < 8; i++) {
        code += d.charAt(Math.floor(Math.random() * l));
      }
    }

    let params = {
      user_id: req.user.id,
      amount,
      code
    };

    let deposit;
    if (id) {
      if (!parseFloat(amount) || parseFloat(amount) < 0)
        throw new Error("Invalid amount");
      deposit = await db.Deposit.findOne({ where: { id } });
      deposit.update(params);
      await deposit.save();
    } else {
      deposit = await db.Deposit.create(params);
    }

    res.send(deposit);
  } catch (e) {
    err("funding error", e.message);
    res.code(500).send("Funding request failed");
  }
});

app.post("/withdrawal", auth, async (req, res) => {
  try {
    const { account, amount, email, institution, transit, notes } = req.body;
    if (!parseFloat(amount) || parseFloat(amount) < 0)
      throw new Error("Invalid amount");

    await db.Withdrawal.create({
      user_id: req.user.id,
      account,
      amount,
      email,
      institution,
      transit,
      notes
    });

    res.send({});
  } catch (e) {
    err("withdrawaerr", e.message);
    res.code(500).send("Withdrawal request failed");
  }
});