From 3a217080186ecad1878ea3a1af24d17b0beff84c Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 11 Nov 2022 14:47:24 -0800 Subject: sort contacts --- routes/users.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/routes/users.js b/routes/users.js index 319a39c4..02e7e3ed 100644 --- a/routes/users.js +++ b/routes/users.js @@ -673,12 +673,19 @@ app.get("/contacts", auth, async function(req, res) { model: db.User, as: "with" }, - attributes: ["createdAt"], + attributes: [ + [Sequelize.fn("max", Sequelize.col("payments_model.createdAt")), "last"] + ], group: ["with_id"], - order: [['createdAt'], [{ model: db.User, as: "with" }, "username", "ASC"]] + order: [[{ model: db.User, as: "with" }, "username", "ASC"]] }); - res.send(contacts.map(c => ({ ...c.with.get({ plain: true }), last: c.createdAt }))); + res.send( + contacts.map(c => { + c = c.get({ plain: true }); + return { ...c.with, last: c.last }; + }) + ); } catch (e) { console.log(e); res.code(500).send(e.message); -- cgit v1.2.3