Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from purduehackers/passports-activate-route
Browse files Browse the repository at this point in the history
Add `/passports/activate` POST route
  • Loading branch information
MatthewStanciu authored Feb 27, 2024
2 parents 34cec3c + c0e1f3f commit fd9856f
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions controllers/passports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ 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() {
this.intializeRoutes();
}

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) {
Expand All @@ -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;

0 comments on commit fd9856f

Please sign in to comment.