aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-11-12 23:48:54 -0800
committerAdam Soltys <[email protected]>2022-11-12 23:48:59 -0800
commitdc3a9e0de4dc3c62b1951276d169a47c99e69f6a (patch)
treee372abf7492f2641d4e9c68398ac35f911bb2895
parent19ef127b5c43613fac4d1cb15e62d7412b247c56 (diff)
downloadcoinos-server-dc3a9e0de4dc3c62b1951276d169a47c99e69f6a.tar.gz
coinos-server-dc3a9e0de4dc3c62b1951276d169a47c99e69f6a.zip
request endpoints
-rw-r--r--routes/requests.js65
1 files changed, 53 insertions, 12 deletions
diff --git a/routes/requests.js b/routes/requests.js
index 0ef997b9..70ab2b07 100644
--- a/routes/requests.js
+++ b/routes/requests.js
@@ -2,7 +2,7 @@ import app from "$app";
import db from "$db";
import { auth } from "$lib/passport";
import { emit } from "$lib/sockets";
-import { Op } from "@sequelize/core";
+import { Op, col } from "@sequelize/core";
app.get("/request/:id", auth, async (req, res) => {
try {
@@ -40,26 +40,48 @@ app.get("/requests", auth, async (req, res) => {
let invoices = (
await db.Request.findAll({
where: {
- requester_id: req.user.id,
+ [Op.or]: {
+ recipient_id: req.user.id,
+ requester_id: req.user.id
+ },
createdAt: { [Op.gt]: day }
- //"$invoice.received$": { [Op.lt]: "$invoice.amount$" }
},
order: [["createdAt", "DESC"]],
- include: {
- model: db.Invoice,
- as: "invoice",
- include: {
+ include: [
+ {
+ model: db.Invoice,
+ as: "invoice",
+ where: { received: { [Op.lt]: col("invoice.amount") } },
+ include: {
+ model: db.User,
+ as: "user",
+ attributes: ["username", "profile"]
+ }
+ },
+ {
model: db.User,
- as: "user"
+ as: "requester",
+ attributes: ["username", "profile"]
}
- }
+ ]
})
)
- .map(r => r.invoice && r.invoice.get({ plain: true }))
- .filter(Boolean);
+ .map(
+ r =>
+ r.invoice && {
+ request_id: r.id,
+ requester: r.requester.get({ plain: true }),
+ ...r.invoice.get({ plain: true })
+ }
+ )
+ .filter(r => r);
let requests = await db.Request.findAll({
- where: { recipient_id: req.user.id, createdAt: { [Op.gt]: day }, invoice_id: { [Op.eq]: null } },
+ where: {
+ recipient_id: req.user.id,
+ createdAt: { [Op.gt]: day },
+ invoice_id: { [Op.eq]: null }
+ },
order: [["createdAt", "DESC"]],
include: {
attributes: ["username", "profile"],
@@ -94,3 +116,22 @@ app.post("/requests", auth, async (req, res) => {
res.code(500).send(e.message);
}
});
+
+app.post("/requests/delete", auth, async (req, res) => {
+ try {
+ let r = await db.Request.findOne({
+ where: {
+ id: req.body.request_id,
+ [Op.or]: { requester_id: req.user.id, recipient_id: req.user.id }
+ }
+ });
+
+ if (!r) throw new Error("request not found");
+ await r.destroy();
+
+ res.send();
+ } catch (e) {
+ console.log(e);
+ res.code(500).send(e.message);
+ }
+});