aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/sockets.js2
-rw-r--r--routes/invoices.js4
-rw-r--r--routes/users.js141
3 files changed, 77 insertions, 70 deletions
diff --git a/lib/sockets.js b/lib/sockets.js
index f482a445..8cdb7992 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -188,7 +188,7 @@ wss.on("connection", function(ws, req) {
});
ws.on("close", function(code, reason) {
- if (reason) console.log("closing socket", reason);
+ // if (reason) console.log("closing socket", reason);
try {
const username = names[id];
if (sockets[username] && sockets[username][id]) {
diff --git a/routes/invoices.js b/routes/invoices.js
index 02a8308d..bf2ef0f6 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -31,6 +31,8 @@ app.post(
let { liquidAddress, id, invoice, user, tx } = req.body;
let { blindkey } = invoice;
+ if (invoice.text.text) invoice.text = invoice.text.text;
+
if (invoice.amount < 0) throw new Error("amount out of range");
if (invoice.tip > invoice.amount || invoice.tip > 1000000 || invoice.tip < 0)
throw new Error("tip amount out of range");
@@ -56,6 +58,8 @@ app.post(
invoice.user_id = user.id;
invoice.account_id = user.account_id;
+ console.log(invoice);
+
l.info(
"creating invoice",
user.username,
diff --git a/routes/users.js b/routes/users.js
index c9367c9a..83b0ac7c 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -639,92 +639,95 @@ app.post(
"/redeem",
optionalAuth,
ah(async function(req, res) {
+ const { redeemcode } = req.body;
try {
- await db.transaction(async transaction => {
- const { redeemcode } = req.body;
- if (redeeming[redeemcode]) fail("redemption in progress");
- redeeming[redeemcode] = true;
- if (!redeemcode) fail("no code provided");
+ await db.transaction(async transaction => {
+ if (redeeming[redeemcode]) fail("redemption in progress");
+ redeeming[redeemcode] = true;
+ if (!redeemcode) fail("no code provided");
- let { user } = req;
+ let { user } = req;
- const source = await db.Payment.findOne({
- where: {
- redeemcode: req.body.redeemcode
- },
- include: {
- model: db.Account,
- as: "account"
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ const source = await db.Payment.findOne({
+ where: {
+ redeemcode: req.body.redeemcode
+ },
+ include: {
+ model: db.Account,
+ as: "account"
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
- l.info("redeeming", redeemcode);
+ l.info("redeeming", redeemcode);
- if (!source) fail("Invalid code");
- if (source.redeemed) fail("Voucher has already been redeemed");
- let { amount } = source;
- amount = -amount;
+ if (!source) fail("Invalid code");
+ if (source.redeemed) fail("Voucher has already been redeemed");
+ let { amount } = source;
+ amount = -amount;
- if (!user) {
- const ip =
- req.headers["x-forwarded-for"] || req.connection.remoteAddress;
+ if (!user) {
+ const ip =
+ req.headers["x-forwarded-for"] || req.connection.remoteAddress;
- user = await register(
- {
- username: redeemcode.substr(0, 8),
- password: ""
- },
- ip,
- false
- );
+ user = await register(
+ {
+ username: redeemcode.substr(0, 8),
+ password: ""
+ },
+ ip,
+ false
+ );
- let payload = { username: user.username };
- let token = jwt.sign(payload, config.jwt);
- res.cookie("token", token, {
- expires: new Date(Date.now() + 432000000)
- });
+ let payload = { username: user.username };
+ let token = jwt.sign(payload, config.jwt);
+ res.cookie("token", token, {
+ expires: new Date(Date.now() + 432000000)
+ });
- return res.send({ user });
- }
+ delete redeeming[redeemcode];
+ return res.send({ user });
+ }
- let account = await getAccount(source.account.asset, user, transaction);
- let { hash, memo, confirmed, fee, network } = source;
+ let account = await getAccount(source.account.asset, user, transaction);
+ let { hash, memo, confirmed, fee, network } = source;
- source.redeemed = true;
- await source.save({ transaction });
+ source.redeemed = true;
+ await source.save({ transaction });
- let payment = await db.Payment.create(
- {
- amount,
- account_id: account.id,
- user_id: user.id,
- hash: "Voucher " + redeemcode,
- memo,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- confirmed,
- network,
- received: true,
- fee
- },
- { transaction }
- );
+ let payment = await db.Payment.create(
+ {
+ amount,
+ account_id: account.id,
+ user_id: user.id,
+ hash: "Voucher " + redeemcode,
+ memo,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ confirmed,
+ network,
+ received: true,
+ fee
+ },
+ { transaction }
+ );
- await account.increment({ balance: amount }, { transaction });
- await account.reload({ transaction });
+ await account.increment({ balance: amount }, { transaction });
+ await account.reload({ transaction });
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
- emit(user.username, "payment", payment);
- emit(user.username, "account", account);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
+ emit(user.username, "payment", payment);
+ emit(user.username, "account", account);
- res.send({ payment });
- });
- } catch(e) {
+ res.send({ payment });
+ });
+ } catch (e) {
+ delete redeeming[redeemcode];
l.error("problem redeeming", e.message);
return res.status(500).send("There was a problem redeeming the voucher");
+ }
})
);