From 297fe74d7c3acf85189ae2164b7a47fb9c814080 Mon Sep 17 00:00:00 2001 From: Alex Yang <32620988+DatProJack@users.noreply.github.com> Date: Sat, 20 Jul 2024 18:09:51 -0400 Subject: [PATCH] corporate --- src/database.ts | 9 ++++++++ src/services/auth/auth-router.ts | 30 +++++++++++++++++++++++++++ src/services/auth/auth-utils.ts | 6 ++++++ src/services/auth/corporate-schema.ts | 16 ++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 src/services/auth/corporate-schema.ts diff --git a/src/database.ts b/src/database.ts index c68d14f..9959fca 100644 --- a/src/database.ts +++ b/src/database.ts @@ -32,6 +32,10 @@ import { SpeakerSchema, SpeakerValidator, } from "./services/speakers/speakers-schema"; +import { + CorporateSchema, + CorporateValidator, +} from "./services/auth/corporate-schema"; mongoose.set("toObject", { versionKey: false }); @@ -94,4 +98,9 @@ export const Database = { NotificationsValidator ), SPEAKERS: initializeModel("speakers", SpeakerSchema, SpeakerValidator), + CORPORATE: initializeModel( + "corporate", + CorporateSchema, + CorporateValidator + ), }; diff --git a/src/services/auth/auth-router.ts b/src/services/auth/auth-router.ts index 5f84a83..8f77671 100644 --- a/src/services/auth/auth-router.ts +++ b/src/services/auth/auth-router.ts @@ -162,4 +162,34 @@ authRouter.get( } ); +authRouter.post( + "/corporate/:email", + RoleChecker([Role.Enum.ADMIN], true), + async (req, res, next) => { + try { + const email = req.params.email; + const corporate = new Database.CORPORATE({ email: email }); + await corporate.save(); + return res.status(StatusCodes.CREATED).json(email); + } catch (error) { + next(error); + } + } +); + +authRouter.delete( + "/corporate/:email", + RoleChecker([Role.Enum.ADMIN], true), + async (req, res, next) => { + try { + const email = req.params.email; + await Database.CORPORATE.findOneAndDelete({ email: email }); + + return res.sendStatus(StatusCodes.NO_CONTENT); + } catch (error) { + next(error); + } + } +); + export default authRouter; diff --git a/src/services/auth/auth-utils.ts b/src/services/auth/auth-utils.ts index 1c7cf92..82d4f1e 100644 --- a/src/services/auth/auth-utils.ts +++ b/src/services/auth/auth-utils.ts @@ -59,3 +59,9 @@ export function isStaff(payload?: JwtPayloadType) { export function isAdmin(payload?: JwtPayloadType) { return payload?.roles.includes(Role.Enum.ADMIN); } + +export async function sponsorExists(email: string) { + const response = await Database.CORPORATE.findOne({ email: email }); + if (!response) return false; + return true; +} diff --git a/src/services/auth/corporate-schema.ts b/src/services/auth/corporate-schema.ts new file mode 100644 index 0000000..1824469 --- /dev/null +++ b/src/services/auth/corporate-schema.ts @@ -0,0 +1,16 @@ +import { Schema } from "mongoose"; +import { z } from "zod"; + +// Zod schema +export const CorporateValidator = z.object({ + email: z.string(), +}); + +// Mongoose schema +export const CorporateSchema = new Schema({ + email: { + type: String, + required: true, + unique: true, + }, +});