Skip to content
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: 8 additions & 1 deletion common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ export type AutoPopulatedDoc<PopulatedType> = PopulatedType & { _id: mongoose.Re
* All the types of actions that can be performed on the system. Used for @casl/ability
* for managing permissions.
*/
export type AbilityAction = "read" | "create" | "update" | "delete" | "manage" | "aggregate";
export type AbilityAction =
| "read"
| "create"
| "update"
| "delete"
| "manage"
| "aggregate"
| "refer";
67 changes: 67 additions & 0 deletions services/registration/src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,73 @@ export const validateApplicationData = async (data: any, branchId: any, branchFo
throw new BadRequestError(JSON.stringify(validate.errors, null, 4));
};

const referralJsonSchema = {
type: "object",
required: [
"firstName",
"lastName",
"email",
"school",
"referForReimbursement",
"referForEarlyApplication",
"resume",
"essay",
],
properties: {
firstName: {
type: "string",
minLength: 1,
},
lastName: {
type: "string",
minLength: 1,
},
email: {
type: "string",
format: "email",
},
phoneNumber: {
type: "string",
},
school: {
type: "string",
minLength: 1,
},
schoolYear: {
type: "string",
},
referForReimbursement: {
type: "boolean",
},
referForEarlyApplication: {
type: "boolean",
},
resume: {
type: "object",
required: ["name"],
properties: {
name: {
type: "string",
minLength: 1,
},
},
},
essay: {
type: "string",
minLength: 10,
},
},
};

export const validateReferralData = async (data: any) => {
const validate = ajv.compile(referralJsonSchema);
const valid = validate(data);
if (valid || validate.errors?.length === 0) {
return;
}
throw new BadRequestError(JSON.stringify(validate.errors, null, 4));
};

/**
* Gets the correct branch (application or confirmation) based on the request body
* @param existingApplication the existing application
Expand Down
112 changes: 112 additions & 0 deletions services/registration/src/models/referral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { AccessibleRecordModel, accessibleRecordsPlugin } from "@casl/mongoose";
import mongoose, { model, Schema, Types } from "mongoose";

export enum ReferralStatusType {
DRAFT = "DRAFT",
SUBMITTED = "SUBMITTED",
}

export interface Referral extends mongoose.Document {
referrerId: string;
referrerName: string;
referrerEmail: string;
hexathon: Types.ObjectId;
referralData: {
firstName?: string;
lastName?: string;
email?: string;
phoneNumber?: string;
school?: string;
schoolYear?: string;
referForReimbursement?: boolean;
referForEarlyApplication?: boolean;
resume?: Types.ObjectId;
essay?: string;
};
referralStartTime: Date;
referralSubmitTime?: Date;
status: ReferralStatusType;
createdAt: Date;
updatedAt: Date;
}

const referralSchema = new Schema<Referral>(
{
referrerId: {
type: String,
required: true,
index: true,
},
referrerName: {
type: String,
required: true,
index: true,
},
referrerEmail: {
type: String,
required: true,
index: true,
},
hexathon: {
type: Schema.Types.ObjectId,
required: true,
index: true,
},
referralData: {
firstName: {
type: String,
},
lastName: {
type: String,
},
email: {
type: String,
},
phoneNumber: {
type: String,
},
school: {
type: String,
},
schoolYear: {
type: String,
},
referForReimbursement: {
type: Boolean,
},
referForEarlyApplication: {
type: Boolean,
},
resume: {
type: Schema.Types.ObjectId,
},
essay: {
type: String,
},
},
referralStartTime: {
type: Date,
required: true,
},
referralSubmitTime: {
type: Date,
},
status: {
type: String,
required: true,
default: ReferralStatusType.DRAFT,
enum: ReferralStatusType,
index: true,
},
},
{
timestamps: true,
}
);

referralSchema.plugin(accessibleRecordsPlugin);

export const ReferralModel = model<Referral, AccessibleRecordModel<Referral>>(
"Referral",
referralSchema
);
4 changes: 4 additions & 0 deletions services/registration/src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const addAbilities = (): RequestHandler => (req, res, next) => {
can("manage", "Branch");
can("manage", "Grader");
can("manage", "Review");
can("manage", "Referral");
}

if (req.user.roles.member) {
Expand All @@ -23,6 +24,9 @@ export const addAbilities = (): RequestHandler => (req, res, next) => {
can("read", "Review");
can("manage", "Review", { reviewerId: req.user.uid });
can("manage", "Email");
can("read", "Referral");
can("create", "Referral");
can("update", "Referral", { referrerId: req.user.uid });
}

if (req.user.roles.admin || req.user.roles.member) {
Expand Down
2 changes: 2 additions & 0 deletions services/registration/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { applicationRouter } from "./application";
import { branchRouter } from "./branch";
import { emailRouter } from "./email";
import { gradingRouter } from "./grading";
import { referralRouter } from "./referrals";
import { statisticsRouter } from "./statistics";

export const defaultRouter = express.Router();
Expand All @@ -13,3 +14,4 @@ defaultRouter.use("/applications", applicationRouter);
defaultRouter.use("/statistics", statisticsRouter);
defaultRouter.use("/grading", gradingRouter);
defaultRouter.use("/email", emailRouter);
defaultRouter.use("/referrals", referralRouter);
Loading