diff options
Diffstat (limited to 'lib/utils.js')
| -rw-r--r-- | lib/utils.js | 81 |
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; }; |
