aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-04-27 11:14:43 -0700
committerAdam Soltys <[email protected]>2020-04-28 22:33:02 -0700
commite166ab2c743e4c66eacb51c6484711c5965dd090 (patch)
treea3b309cb38ceceb6e581963f0001a6869452b531 /lib/utils.js
parentfae6e8b55ea12989d688eb777c002222199c169b (diff)
downloadcoinos-server-e166ab2c743e4c66eacb51c6484711c5965dd090.tar.gz
coinos-server-e166ab2c743e4c66eacb51c6484711c5965dd090.zip
select payments from current account
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js81
1 files changed, 42 insertions, 39 deletions
diff --git a/lib/utils.js b/lib/utils.js
index 2f0214d5..58bf29f9 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,27 +1,10 @@
-const { Op } = require("sequelize");
+const sequelize = require("sequelize");
+const { Op } = sequelize;
getUser = async username => {
- return db.User.findOne({
+ let user = await db.User.findOne({
include: [
{
- model: db.Payment,
- as: "payments",
- order: [["id", "DESC"]],
- where: {
- [Op.or]: {
- received: true,
- amount: {
- [Op.lt]: 0
- }
- }
- },
- limit: 12,
- include: {
- model: db.Account,
- as: "account"
- }
- },
- {
model: db.Account,
as: "accounts"
},
@@ -32,30 +15,31 @@ getUser = async username => {
],
where: { username }
});
+
+ user = user.get({ plain: true });
+
+ user.payments = await db.Payment.findAll({
+ order: [["id", "DESC"]],
+ where: {
+ user_id: user.id,
+ account_id: user.account.id,
+ [Op.or]: {
+ received: true,
+ amount: {
+ [Op.lt]: 0
+ }
+ }
+ },
+ limit: 12
+ });
+
+ return user;
};
getUserById = async id => {
- return db.User.findOne({
+ let user = db.User.findOne({
include: [
{
- model: db.Payment,
- as: "payments",
- order: [["id", "DESC"]],
- where: {
- [Op.or]: {
- received: true,
- amount: {
- [Op.lt]: 0
- }
- }
- },
- limit: 12,
- include: {
- model: db.Account,
- as: "account"
- }
- },
- {
model: db.Account,
as: "accounts"
},
@@ -66,4 +50,23 @@ getUserById = async id => {
],
where: { id }
});
+
+ user = user.get({ plain: true });
+
+ user.payments = await db.Payment.findAll({
+ order: [["id", "DESC"]],
+ where: {
+ user_id: user.id,
+ account_id: user.account.id,
+ [Op.or]: {
+ received: true,
+ amount: {
+ [Op.lt]: 0
+ }
+ }
+ },
+ limit: 12
+ });
+
+ return user;
};
lor: #038 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #369 } /* Name.Variable.Class */ .highlight .vg { color: #D70 } /* Name.Variable.Global */ .highlight .vi { color: #33B } /* Name.Variable.Instance */ .highlight .vm { color: #369 } /* Name.Variable.Magic */ .highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
import { get } from "svelte/store";
import {
  event,
  invoice,
  request,
  newPayment,
  last,
  rate,
  user,
} from "$lib/store";
import { success, sat } from "$lib/utils";
import { PUBLIC_SOCKET } from "$env/static/public";
import { invalidate } from "$app/navigation";
import { browser } from "$app/environment";

let socket, token;

export const auth = () => token && send("login", token);

export const send = (type, data) => {
  socket?.readyState === 1 && socket.send(JSON.stringify({ type, data }));
  return true;
};

export const messages = (data) => ({
  id() {
    last.set(Date.now());
  },
  invoice() {
    invoice.set(data);
  },
  event() {
    event.set(data);
  },
  rate() {
    rate.set(data);
  },

  request() {
    request.set(data);
    invalidate("app:invoice");
  },

  async payment() {
    let { amount, confirmed } = data;
    invalidate("app:user");
    invalidate("app:invoice");
    invalidate("app:payments");

    if (amount > 0) {
      success(`${confirmed ? "Received" : "Detected"} ${sat(amount)}!`);
    }
  },

  connected: auth,
});

const initialReconnectDelay = 1000;
const maxReconnectDelay = 16000;

let currentReconnectDelay = initialReconnectDelay;

export function connect(t) {
  token = t;

  if (socket) return auth();

  socket = new WebSocket(PUBLIC_SOCKET);
  socket.addEventListener("open", onWebsocketOpen);
  socket.addEventListener("close", onWebsocketClose);
  socket.addEventListener("message", onWebsocketMessage);
}

export function close() {
  if (socket) socket.close();
}

function onWebsocketMessage(msg) {
  let { type, data } = JSON.parse(msg.data);
  messages(data)[type] && messages(data)[type]();
}

function onWebsocketOpen() {
  currentReconnectDelay = initialReconnectDelay;
}

function onWebsocketClose() {
  socket = null;
  setTimeout(() => {
    reconnectToWebsocket();
  }, currentReconnectDelay + Math.floor(Math.random() * 3000));
}

function reconnectToWebsocket() {
  if (currentReconnectDelay < maxReconnectDelay) {
    currentReconnectDelay *= 2;
  }
  connect(token);
}