-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcc9cad
commit 5236810
Showing
5 changed files
with
233 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {Request, Response, Router} from "express"; | ||
import {prisma} from "./prisma"; | ||
import logger from "./logger"; | ||
import {generateCode} from "./utils"; | ||
|
||
const apiRouter = Router(); | ||
|
||
apiRouter.post("/sesame", async (request: Request, response: Response) => { | ||
const sesame: string | undefined = request.body.code; | ||
if (!sesame) return response.status(400).send("Missing code"); | ||
if ( | ||
typeof sesame !== "string" || | ||
sesame.length !== (Number(process.env.SESAME_LENGTH) || 4) | ||
) | ||
return response.status(400).send("Invalid code"); | ||
|
||
const [opening] = await prisma.opening.findMany({ | ||
where: { | ||
code: sesame, | ||
date: null, | ||
codeGeneratedAt: new Date(Date.now() - 1000 * 60 * 5), | ||
}, | ||
select: { | ||
return: { | ||
select: { | ||
returnOpeningId: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!opening) { | ||
logger.info(`Invalid Sesame provided : ${sesame}`); | ||
return response.status(403).send("Invalid sesame"); | ||
} | ||
|
||
if (opening.return) { | ||
const returnCode = await generateCode(); | ||
await prisma.opening.update({ | ||
where: { | ||
id: opening.return.returnOpeningId, | ||
}, | ||
data: { | ||
code: returnCode, | ||
codeGeneratedAt: new Date(), | ||
}, | ||
}); | ||
} | ||
|
||
return response.status(200).send("Sésame ouvre toi"); | ||
}); | ||
|
||
export default apiRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {PrismaClient} from "@prisma/client"; | ||
|
||
export const prisma = new PrismaClient(); | ||
|
||
const validOpening = { | ||
codeGeneratedAt: { gte: new Date(Date.now() - 1000 * 60 * 5) }, | ||
} | ||
|
||
const currentlyBorrowing = { | ||
borrowOpening: { date: { not: null } }, | ||
returnOpening: { date: null }, | ||
} | ||
|
||
export const prismaUtils = { | ||
validOpening, | ||
currentlyBorrowing, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {Request} from "express"; | ||
import jwt from "jsonwebtoken"; | ||
import {prisma, prismaUtils} from "./prisma"; | ||
|
||
export function authenticate(request: Request) { | ||
if (!request.cookies["token"]) { | ||
return null; | ||
} | ||
try { | ||
return ( | ||
jwt.verify(request.cookies["token"], process.env.JWT_SECRET) as { | ||
login: string; | ||
} | ||
).login; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
export async function generateCode() { | ||
let code: string; | ||
let found; | ||
const ciffers = "0123456789"; | ||
do { | ||
code = | ||
ciffers[Math.floor(Math.random() * 10)] + | ||
ciffers[Math.floor(Math.random() * 10)] + | ||
ciffers[Math.floor(Math.random() * 10)] + | ||
ciffers[Math.floor(Math.random() * 10)]; | ||
found = await prisma.opening.findFirst({ where: { code } }); | ||
} while (found); | ||
return code; | ||
} | ||
|
||
export async function getJoyconsLeft() { | ||
const joyconBorrows = await prisma.borrow.findMany({where: {borrowOpening: {OR: [prismaUtils.validOpening, {date: {not: null}}]}, returnOpening: {date: {not: null}}}}); | ||
return Number.parseInt(process.env.TOTAL_JOYCONS) - joyconBorrows.reduce((acc, borrow) => acc + borrow.joyconsTaken, 0); | ||
} |
Oops, something went wrong.