Skip to content

Commit

Permalink
Add greenboxEntryCount to json for greenbox api requests
Browse files Browse the repository at this point in the history
This will be necessary for the web side to be able to offer permalink to current greenbox
when browsing without entry number provided, or to know not to offer next greenbox entry
when browsing with a number specified
  • Loading branch information
soolar committed Nov 25, 2023
1 parent b8d45c4 commit 0d1786e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ app
.status(StatusCodes.NOT_FOUND)
.json({ error: "No greenbox data found" });

const greenboxEntryCount = await prisma.greenbox.count({
where: { playerId },
});

return res.status(StatusCodes.OK).json({
greenboxString: latestGreenbox.data,
greenboxLastUpdate: latestGreenbox.time,
greenboxEntryCount,
});
})
.get("/api/greenboxhistory/:playerId/:greenboxNumber", async (req, res) => {
Expand All @@ -53,6 +58,20 @@ app
.status(StatusCodes.BAD_REQUEST)
.json({ error: "greenboxNumber is invalid" });

const greenboxEntryCount = await prisma.greenbox.count({
where: { playerId },
});

if (greenboxEntryCount === 0)
return res.status(StatusCodes.NOT_FOUND).json({
error: `asked for greenbox number ${greenboxNumber}, but player ${playerId} has no greenbox data`,
});

if (greenboxEntryCount < greenboxNumber)
return res.status(StatusCodes.NOT_FOUND).json({
error: `asked for greenbox number ${greenboxNumber}, but player ${playerId} only has ${greenboxEntryCount} greenbox updates`,
});

const greenboxEntry = await prisma.greenbox.findFirst({
where: { playerId },
orderBy: { id: "asc" },
Expand All @@ -72,6 +91,7 @@ app
return res.status(StatusCodes.OK).json({
greenboxString: greenboxEntry.data,
greenboxLastUpdate: greenboxEntry.time,
greenboxEntryCount,
});
})
.get("/webhooks/subsrolling", async (req, res) => {
Expand Down

0 comments on commit 0d1786e

Please sign in to comment.