Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corporate - API Infra for SponsorRP #108

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
});