aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sockets.js44
-rw-r--r--lib/utils.js65
2 files changed, 35 insertions, 74 deletions
diff --git a/lib/sockets.js b/lib/sockets.js
index e297703b..89beffb3 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -27,19 +27,19 @@ broadcast = (type, data) => {
}
};
-server.on("upgrade", function (req, socket, head) {
- wss.handleUpgrade(req, socket, head, function (ws) {
+server.on("upgrade", function(req, socket, head) {
+ wss.handleUpgrade(req, socket, head, function(ws) {
wss.emit("connection", ws, req);
});
});
const names = {};
-wss.on("connection", function (ws, req) {
+wss.on("connection", function(ws, req) {
const id = uuidv4();
setTimeout(() => ws.ok || ws.close(), 2000);
- ws.on("message", async (token) => {
+ ws.on("message", async token => {
if (!token || token === "null") return;
try {
const username = jwt.decode(token).username;
@@ -47,24 +47,48 @@ wss.on("connection", function (ws, req) {
sockets[username][id] = ws;
names[id] = username;
+ let user = await getUser(username);
+ let payments = await user.getPayments({
+ where: {
+ account_id: user.account_id,
+ },
+ order: [["id", "DESC"]],
+ limit: 12,
+ include: {
+ model: db.Account,
+ as: "account"
+ }
+ });
+ let accounts = await user.getAccounts();
+ user = user.get({ plain: true });
+ user.accounts = accounts;
+ user.payments = payments;
+
ws.ok = true;
ws.send(JSON.stringify({ type: "nodes", data: networks }));
ws.send(JSON.stringify({ type: "rates", data: app.get("rates") }));
- ws.send(JSON.stringify({ type: "login", data: await getUser(username) }));
+ ws.send(JSON.stringify({ type: "login", data: user }));
l.info("socket open", username, id);
} catch (e) {
ws.close();
- l.error("problem opening socket", token, e.message, id, req.connection.remoteAddress);
+ l.error(
+ "problem opening socket",
+ token,
+ e.message,
+ id,
+ req.connection.remoteAddress
+ );
}
});
- ws.on("close", function () {
+ ws.on("close", function() {
try {
const username = names[id];
- if (sockets[username] && sockets[username][id]) delete sockets[username][id];
+ if (sockets[username] && sockets[username][id])
+ delete sockets[username][id];
l.info("socket closed", username, id);
- } catch(e) {
+ } catch (e) {
l.error("problem closing socket", e.message);
- }
+ }
});
});
diff --git a/lib/utils.js b/lib/utils.js
index 5674f2fc..17b5d134 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -6,10 +6,6 @@ getUser = async (username, transaction) => {
include: [
{
model: db.Account,
- as: "accounts"
- },
- {
- model: db.Account,
as: "account"
}
],
@@ -24,34 +20,6 @@ getUser = async (username, transaction) => {
let user = await db.User.findOne(params);
if (!user) throw new Error("User not found");
- user = user.get({ plain: true });
-
- params = {
- order: [["id", "DESC"]],
- where: {
- user_id: user.id,
- account_id: user.account_id,
- [Op.or]: {
- received: true,
- amount: {
- [Op.lt]: 0
- }
- }
- },
- limit: 12,
- include: {
- model: db.Account,
- as: "account"
- }
- };
-
- if (transaction) {
- params.lock = transaction.LOCK.UPDATE;
- params.transaction = transaction;
- }
-
- user.payments = await db.Payment.findAll(params);
-
return user;
};
@@ -60,10 +28,6 @@ getUserById = async (id, transaction) => {
include: [
{
model: db.Account,
- as: "accounts"
- },
- {
- model: db.Account,
as: "account"
}
],
@@ -76,34 +40,7 @@ getUserById = async (id, transaction) => {
}
let user = await db.User.findOne(params);
-
- user = user.get({ plain: true });
-
- params = {
- order: [["id", "DESC"]],
- where: {
- user_id: user.id,
- account_id: user.account_id,
- [Op.or]: {
- received: true,
- amount: {
- [Op.lt]: 0
- }
- }
- },
- limit: 12,
- include: {
- model: db.Account,
- as: "account"
- }
- };
-
- if (transaction) {
- params.lock = transaction.LOCK.UPDATE;
- params.transaction = transaction;
- }
-
- user.payments = await db.Payment.findAll(params);
+ if (!user) throw new Error("User not found");
return user;
};