aboutsummaryrefslogtreecommitdiffstats
path: root/lib
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
parentfae6e8b55ea12989d688eb777c002222199c169b (diff)
downloadcoinos-server-e166ab2c743e4c66eacb51c6484711c5965dd090.tar.gz
coinos-server-e166ab2c743e4c66eacb51c6484711c5965dd090.zip
select payments from current account
Diffstat (limited to 'lib')
-rw-r--r--lib/passport.js4
-rw-r--r--lib/sockets.js1
-rw-r--r--lib/utils.js81
3 files changed, 44 insertions, 42 deletions
diff --git a/lib/passport.js b/lib/passport.js
index e0c2dd62..1d80d9b8 100644
--- a/lib/passport.js
+++ b/lib/passport.js
@@ -16,8 +16,9 @@ passport = Passport.use(
secretOrKey: config.jwt
},
async (payload, next) => {
+ const { username } = payload;
try {
- next(null, await getUser(payload.username));
+ next(null, await db.User.findOne({ where: { username } }));
} catch (e) {
l.error("error finding user for session", e);
next(null, { username: "guest" });
@@ -28,4 +29,3 @@ passport = Passport.use(
auth = passport.authenticate("jwt", { session: false });
app.use(passport.initialize());
-
diff --git a/lib/sockets.js b/lib/sockets.js
index 9a5457c3..fe93bb9f 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -36,7 +36,6 @@ socket.sockets.on("connect", async socket => {
socket.emit("connected");
if (app.get("rates")) socket.emit("rate", app.get("rates").ask);
socket.on("getuser", async (data, callback) => {
- l.info("socket open", socket.request.user);
callback(await getUser(socket.request.user));
});
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;
};