Skip to content

Commit d48c38a

Browse files
committed
fixes
1 parent aa57173 commit d48c38a

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

web/.env.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ JWT_EXPIRES_IN=7y
88
SESAME_LENGTH=4
99
SENTRY_DSN=
1010
TIME_BEFORE_CHEST_DEATH=3600
11+
CODE_LIFETIME=300
1112
TOTAL_JOYCONS=4
12-
API_KEY=test
13+
API_KEY=test

web/src/apiRouter.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import {Request, Response, Router} from "express";
2-
import {prisma} from "./prisma";
1+
import { Request, Response, Router } from "express";
2+
import { prisma } from "./prisma";
33
import logger from "./logger";
4-
import {generateCode, setLastTimeChestWasAlive} from "./utils";
4+
import { CODE_LIFETIME, generateCode, setLastTimeChestWasAlive } from "./utils";
55

66
const apiRouter = Router();
77

88
apiRouter.use((request: Request, response: Response, next) => {
9-
if (!request.headers['Authorisation'] || request.headers['Authorisation'] !== `Bearer ${process.env.API_KEY}`) {
9+
if (
10+
!request.headers["Authorization"] ||
11+
request.headers["Authorization"] !== `Bearer ${process.env.API_KEY}`
12+
) {
1013
return response.status(403).send("Invalid API Key");
1114
}
1215
return next();
@@ -25,7 +28,7 @@ apiRouter.post("/sesame", async (request: Request, response: Response) => {
2528
where: {
2629
code: sesame,
2730
date: null,
28-
codeGeneratedAt: new Date(Date.now() - 1000 * 60 * 5),
31+
codeGeneratedAt: new Date(Date.now() - CODE_LIFETIME),
2932
},
3033
select: {
3134
return: {

web/src/prisma.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import {PrismaClient} from "@prisma/client";
1+
import { PrismaClient } from "@prisma/client";
2+
import { CODE_LIFETIME } from "./utils";
23

34
export const prisma = new PrismaClient();
45

56
const validOpening = {
6-
codeGeneratedAt: { gte: new Date(Date.now() - 1000 * 60 * 5) },
7+
codeGeneratedAt: { gte: new Date(Date.now() - CODE_LIFETIME) },
78
date: null,
8-
}
9+
};
910

1011
const currentlyBorrowing = {
1112
borrowOpening: { date: { not: null } },
1213
returnOpening: { date: null },
13-
}
14+
};
1415

1516
export const prismaUtils = {
1617
validOpening,

web/src/utils.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {Request} from "express";
1+
import { Request } from "express";
22
import jwt from "jsonwebtoken";
3-
import {prisma, prismaUtils} from "./prisma";
3+
import { prisma, prismaUtils } from "./prisma";
44
import { Prisma } from "@prisma/client";
55

66
export function authenticate(request: Request) {
@@ -34,8 +34,18 @@ export async function generateCode() {
3434
}
3535

3636
export async function getJoyconsLeft() {
37-
const joyconBorrows = await prisma.borrow.findMany({where: {borrowOpening: {OR: [prismaUtils.validOpening, {date: {not: null}}]}, returnOpening: {date: {not: null}}}});
38-
return Number.parseInt(process.env.TOTAL_JOYCONS) - joyconBorrows.reduce((acc, borrow) => acc + borrow.joyconsTaken, 0);
37+
const joyconBorrows = await prisma.borrow.findMany({
38+
where: {
39+
borrowOpening: {
40+
OR: [prismaUtils.validOpening, { date: { not: null } }],
41+
},
42+
returnOpening: { date: { not: null } },
43+
},
44+
});
45+
return (
46+
Number.parseInt(process.env.TOTAL_JOYCONS) -
47+
joyconBorrows.reduce((acc, borrow) => acc + borrow.joyconsTaken, 0)
48+
);
3949
}
4050

4151
const OPENING_INCLUDE_BEFORE_FORMATTING = {
@@ -45,30 +55,32 @@ const OPENING_INCLUDE_BEFORE_FORMATTING = {
4555
},
4656
} as const;
4757

48-
type FormattedOpening = Omit<Prisma.OpeningGetPayload<typeof OPENING_INCLUDE_BEFORE_FORMATTING>, 'return'> & {type: 'borrow' | 'return'}
58+
type FormattedOpening = Omit<
59+
Prisma.OpeningGetPayload<typeof OPENING_INCLUDE_BEFORE_FORMATTING>,
60+
"return"
61+
> & { type: "borrow" | "return" };
4962

50-
export function formatOpening(opening: Prisma.OpeningGetPayload<typeof OPENING_INCLUDE_BEFORE_FORMATTING>): FormattedOpening {
63+
export function formatOpening(
64+
opening: Prisma.OpeningGetPayload<typeof OPENING_INCLUDE_BEFORE_FORMATTING>
65+
): FormattedOpening {
5166
if (!opening) {
5267
return null;
5368
}
5469
if (opening.return) {
5570
opening.borrow = opening.return;
5671
delete opening.return;
57-
return {...opening, type: 'return'};
72+
return { ...opening, type: "return" };
5873
}
5974
delete opening.return;
60-
return {...opening, type: 'borrow'};
75+
return { ...opening, type: "borrow" };
6176
}
6277

6378
export async function getWaitingOpening(userLogin: string) {
6479
const opening = await prisma.opening.findFirst({
6580
where: {
6681
date: null,
6782
code: { not: null },
68-
OR: [
69-
{ borrow: { userLogin } },
70-
{ return: { userLogin } },
71-
],
83+
OR: [{ borrow: { userLogin } }, { return: { userLogin } }],
7284
},
7385
...OPENING_INCLUDE_BEFORE_FORMATTING,
7486
});
@@ -79,10 +91,7 @@ export async function getWaitingOpeningWithValidCode(userLogin: string) {
7991
const opening = await prisma.opening.findFirst({
8092
where: {
8193
...prismaUtils.validOpening,
82-
OR: [
83-
{ borrow: { userLogin } },
84-
{ return: { userLogin } }
85-
]
94+
OR: [{ borrow: { userLogin } }, { return: { userLogin } }],
8695
},
8796
...OPENING_INCLUDE_BEFORE_FORMATTING,
8897
});
@@ -108,3 +117,5 @@ export function setLastTimeChestWasAlive(time: number) {
108117
export function getLastTimeChestWasAlive() {
109118
return lastTimeChestWasAlive;
110119
}
120+
121+
export const CODE_LIFETIME = (Number(process.env.CODE_LIFETIME) || 300) * 1000;

0 commit comments

Comments
 (0)