Skip to content

Commit

Permalink
Merge pull request #108 from ReflectionsProjections/dev/alex/corporate
Browse files Browse the repository at this point in the history
Corporate - API Infra for SponsorRP
  • Loading branch information
aletya authored Jul 20, 2024
2 parents 971dc28 + 297fe74 commit 3362888
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -94,4 +98,9 @@ export const Database = {
NotificationsValidator
),
SPEAKERS: initializeModel("speakers", SpeakerSchema, SpeakerValidator),
CORPORATE: initializeModel(
"corporate",
CorporateSchema,
CorporateValidator
),
};
30 changes: 30 additions & 0 deletions src/services/auth/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions src/services/auth/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 16 additions & 0 deletions src/services/auth/corporate-schema.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});

0 comments on commit 3362888

Please sign in to comment.