aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-12-15 13:35:12 -0800
committerJohn Barratt <[email protected]>2022-12-19 11:25:12 -0800
commit1b807a117871bd6b1e6897f8bd197b27422bb18f (patch)
tree507095d68e69bd46d76283179473e39d52284553
parentfa6b99b04009cce2e22ac5b26f647bb8beb0d147 (diff)
downloadcoinos-server-1b807a117871bd6b1e6897f8bd197b27422bb18f.tar.gz
coinos-server-1b807a117871bd6b1e6897f8bd197b27422bb18f.zip
move voucher endpoints
-rw-r--r--routes/payments.js116
-rw-r--r--routes/users.js102
2 files changed, 110 insertions, 108 deletions
diff --git a/routes/payments.js b/routes/payments.js
index 416bc20a..96791bb3 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -2,14 +2,16 @@ import app from "$app";
import db from "$db";
import config from "$config";
import store from "$lib/store";
+import getAccount from "$lib/account";
import { auth, adminAuth, optionalAuth } from "$lib/passport";
import fs from "fs";
import { join } from "path";
import { Op } from "@sequelize/core";
import send from "./send";
-import { l, warn } from "$lib/logging";
+import { err, l, warn } from "$lib/logging";
import { fail } from "$lib/utils";
+import { emit } from "$lib/sockets";
import bc from "$lib/bitcoin";
import lq from "$lib/liquid";
@@ -107,7 +109,7 @@ app.get("/payments", auth, async (req, res) => {
if (offset) offset = parseInt(offset);
let where = {
- account_id: req.user.account_id,
+ account_id: req.user.account_id
};
if (start || end) where.createdAt = {};
@@ -148,12 +150,12 @@ app.get("/payment/:redeemcode", async (req, res) => {
where: {
redeemcode
},
- include: {
- model: db.Account,
- as: "account"
- }
+ include: { all: true }
});
+ payment = payment.get({ plain: true });
+ payment.redeemer = payment['with'];
+
if (!payment) fail("invalid code");
res.send(payment);
@@ -161,3 +163,105 @@ app.get("/payment/:redeemcode", async (req, res) => {
res.code(500).send(e.message);
}
});
+
+let redeeming = {};
+app.post("/redeem", optionalAuth, async function(req, res) {
+ const { redeemcode } = req.body;
+ try {
+ await db.transaction(async transaction => {
+ if (redeeming[redeemcode]) fail("redemption in progress");
+ redeeming[redeemcode] = true;
+ if (!redeemcode) fail("no code provided");
+
+ 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
+ });
+
+ l("redeeming", redeemcode);
+
+ 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;
+
+ 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)
+ });
+
+ delete redeeming[redeemcode];
+ return res.send({ user });
+ }
+
+ let account = await getAccount(source.account.asset, user, transaction);
+ let { hash, memo, confirmed, fee, network } = source;
+
+ source.redeemed = true;
+ (source.with_id = user.id), await source.save({ transaction });
+
+ let payment = await db.Payment.create(
+ {
+ amount,
+ account_id: account.id,
+ user_id: user.id,
+ hash: "Voucher " + redeemcode,
+ memo,
+ rate: store.rates[user.currency],
+ currency: user.currency,
+ confirmed,
+ network,
+ received: true,
+ fee,
+ with_id: source.user_id
+ },
+ { 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);
+
+ res.send({ payment });
+ });
+ } catch (e) {
+ delete redeeming[redeemcode];
+ console.log(e);
+ err("problem redeeming", e.message);
+ return res.code(500).send("There was a problem redeeming the voucher");
+ }
+});
+
+app.post("/checkRedeemCode", auth, async function(req, res) {
+ const { redeemcode } = req.body;
+
+ const payment = await db.Payment.findOne({ where: { redeemcode } });
+ res.send(payment);
+});
diff --git a/routes/users.js b/routes/users.js
index 02e7e3ed..c4b613eb 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -7,7 +7,6 @@ import axios from "axios";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { authenticator } from "otplib";
-import getAccount from "$lib/account";
import Sequelize from "@sequelize/core";
import bitcoin from "bitcoinjs-lib";
import liquid from "liquidjs-lib";
@@ -489,107 +488,6 @@ app.post("/subscribe", auth, async function(req, res) {
res.sendStatus(201);
});
-let redeeming = {};
-app.post("/redeem", optionalAuth, async function(req, res) {
- const { redeemcode } = req.body;
- try {
- await db.transaction(async transaction => {
- if (redeeming[redeemcode]) fail("redemption in progress");
- redeeming[redeemcode] = true;
- if (!redeemcode) fail("no code provided");
-
- 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
- });
-
- l("redeeming", redeemcode);
-
- 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;
-
- 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)
- });
-
- delete redeeming[redeemcode];
- return res.send({ user });
- }
-
- let account = await getAccount(source.account.asset, user, transaction);
- let { hash, memo, confirmed, fee, network } = source;
-
- 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: store.rates[user.currency],
- currency: user.currency,
- confirmed,
- network,
- received: true,
- fee
- },
- { 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);
-
- res.send({ payment });
- });
- } catch (e) {
- delete redeeming[redeemcode];
- console.log(e);
- err("problem redeeming", e.message);
- return res.code(500).send("There was a problem redeeming the voucher");
- }
-});
-
-app.post("/checkRedeemCode", auth, async function(req, res) {
- const { redeemcode } = req.body;
-
- const payment = await db.Payment.findOne({ where: { redeemcode } });
- res.send(payment);
-});
-
app.post("/password", auth, async function(req, res) {
const { user } = req;
const { password } = req.body;