aboutsummaryrefslogtreecommitdiffstats
path: root/routes/send.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-07-29 16:40:30 -0700
committerAdam Soltys <[email protected]>2020-07-29 16:40:30 -0700
commit99d71f034cfb7c1675263539fa6aecb17429d134 (patch)
tree6d29806937436fde454f357a8dd5f7a30fa6b8bd /routes/send.js
parentffee4c7051982f3677d6717d24e7b357a89802eb (diff)
downloadcoinos-server-99d71f034cfb7c1675263539fa6aecb17429d134.tar.gz
coinos-server-99d71f034cfb7c1675263539fa6aecb17429d134.zip
redeem codes
Diffstat (limited to 'routes/send.js')
-rw-r--r--routes/send.js165
1 files changed, 86 insertions, 79 deletions
diff --git a/routes/send.js b/routes/send.js
index 00943d29..8ffff4dd 100644
--- a/routes/send.js
+++ b/routes/send.js
@@ -1,4 +1,5 @@
const axios = require("axios");
+const uuidv4 = require("uuid/v4");
module.exports = ah(async (req, res, next) => {
let { amount, asset, memo, username } = req.body;
@@ -9,14 +10,14 @@ module.exports = ah(async (req, res, next) => {
return res.status(500).send("Amount must be greater than zero");
try {
- await db.transaction(async transaction => {
+ await db.transaction(async (transaction) => {
let account = await db.Account.findOne({
where: {
user_id: user.id,
- asset
+ asset,
},
lock: transaction.LOCK.UPDATE,
- transaction
+ transaction,
});
if (account.balance < amount) {
@@ -28,20 +29,24 @@ module.exports = ah(async (req, res, next) => {
account.balance -= amount;
await account.save({ transaction });
- let payment = await db.Payment.create(
- {
- amount: -amount,
- account_id: account.id,
- memo,
- user_id: user.id,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- confirmed: true,
- hash: "Internal Transfer",
- network: "COINOS"
- },
- { transaction }
- );
+ const params = {
+ amount: -amount,
+ account_id: account.id,
+ memo,
+ user_id: user.id,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ confirmed: true,
+ hash: "Internal Transfer",
+ network: "COINOS",
+ };
+
+ if (!username) {
+ l.info("creating redeemable payment");
+ params.redeemcode = uuidv4();
+ }
+
+ let payment = await db.Payment.create(params, { transaction });
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
@@ -53,80 +58,82 @@ module.exports = ah(async (req, res, next) => {
emit(user.username, "user", user);
res.send(payment);
- user = await db.User.findOne({
- where: { username }
- });
+ if (username) {
+ user = await db.User.findOne({
+ where: { username },
+ });
- let params = {
- user_id: user.id,
- asset
- };
+ let params = {
+ user_id: user.id,
+ asset,
+ };
- account = await db.Account.findOne({
- where: params,
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ account = await db.Account.findOne({
+ where: params,
+ lock: transaction.LOCK.UPDATE,
+ transaction,
+ });
- if (account) {
- account.balance += amount;
- await account.save({ transaction });
- } else {
- let name = asset.substr(0, 6);
- let domain;
- let ticker = asset.substr(0, 3).toUpperCase();
- let precision = 8;
+ if (account) {
+ account.balance += amount;
+ await account.save({ transaction });
+ } else {
+ let name = asset.substr(0, 6);
+ let domain;
+ let ticker = asset.substr(0, 3).toUpperCase();
+ let precision = 8;
- const assets = (await axios.get("https://assets.blockstream.info/"))
- .data;
+ const assets = (await axios.get("https://assets.blockstream.info/"))
+ .data;
- if (assets[asset]) {
- ({ domain, ticker, precision, name } = assets[asset]);
- } else {
- const existing = await db.Account.findOne({
- where: {
- asset
- },
- order: [["id", "ASC"]],
- limit: 1
- });
+ if (assets[asset]) {
+ ({ domain, ticker, precision, name } = assets[asset]);
+ } else {
+ const existing = await db.Account.findOne({
+ where: {
+ asset,
+ },
+ order: [["id", "ASC"]],
+ limit: 1,
+ });
- l.info("existing", existing);
+ l.info("existing", existing);
- if (existing) {
- ({ domain, ticker, precision, name } = existing);
+ if (existing) {
+ ({ domain, ticker, precision, name } = existing);
+ }
}
- }
- params = { ...params, ...{ domain, ticker, precision, name } };
- params.balance = amount;
- params.pending = 0;
- account = await db.Account.create(params, { transaction });
- }
+ params = { ...params, ...{ domain, ticker, precision, name } };
+ params.balance = amount;
+ params.pending = 0;
+ account = await db.Account.create(params, { transaction });
+ }
- payment = await db.Payment.create(
- {
- amount,
- account_id: account.id,
- user_id: user.id,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- confirmed: true,
- hash: "Internal Transfer",
- memo,
- network: "COINOS",
- received: true
- },
- { transaction }
- );
+ payment = await db.Payment.create(
+ {
+ amount,
+ account_id: account.id,
+ user_id: user.id,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ confirmed: true,
+ hash: "Internal Transfer",
+ memo,
+ network: "COINOS",
+ received: true,
+ },
+ { transaction }
+ );
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
- emit(user.username, "account", account);
- emit(user.username, "payment", payment);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
+ emit(user.username, "account", account);
+ emit(user.username, "payment", payment);
- l.info("received internal", user.username, amount);
- notify(user, `Received ${amount} SAT`);
+ l.info("received internal", user.username, amount);
+ notify(user, `Received ${amount} SAT`);
+ }
});
} catch (e) {
l.error(