diff --git a/controllers/passports.ts b/controllers/passports.ts index 4e9d39e..6b17f6c 100644 --- a/controllers/passports.ts +++ b/controllers/passports.ts @@ -3,7 +3,8 @@ import { fetchEvents } from "../utils/fetchEvents"; import { fetchPassports } from "../utils/fetchPassports"; class PassportsController { - public path = "/passports"; + public getPath = "/passports"; + public postPath = "/passports/activate"; public router = Router(); constructor() { @@ -11,7 +12,8 @@ class PassportsController { } public intializeRoutes() { - this.router.get(this.path, this.getPassports); + this.router.get(this.getPath, this.getPassports); + this.router.post(this.postPath, this.postPassports); } public async getPassports(req: Request, res: Response) { @@ -32,6 +34,66 @@ class PassportsController { ); } } + + public async postPassports(req: Request, res: Response) { + try { + if (req.headers.authorization === `Bearer ${process.env.PASSPORTS_KEY}`) { + // Fetch passport given id and set activated to true + // Fetch all other passports by that user that are marked activated + // If there are any, mark all of them deactivated + + const passportId = Number(req.body.id); + + try { + const passport = await prisma.passport.update({ + where: { + id: passportId, + }, + data: { + activated: true, + }, + }); + const ownerId = passport.owner_id; + + const otherPassports = await prisma.passport.findMany({ + where: { + owner_id: ownerId, + activated: true, + NOT: { + id: passportId, + }, + }, + }); + if (otherPassports.length > 0) { + for (const otherPassport of otherPassports) { + await prisma.passport.update({ + where: { + id: otherPassport.id, + }, + data: { + activated: false, + }, + }); + } + } + + return res.json({ ok: true }); + } catch (err) { + return res.status(400).send(`Error updating passport: ${err}`); + } + } else { + return res + .status(401) + .send("Missing or incorrect key for this endpoint"); + } + } catch (err) { + return res + .status(err.statusCode || 500) + .send( + `Error fetching passports:\nError: ${err.error}\nMessage: ${err.message}` + ); + } + } } export default PassportsController;