Skip to content

Commit

Permalink
feat(schema): add tutor and service schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 14, 2024
1 parent b5cf89b commit 4400819
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 11 deletions.
99 changes: 99 additions & 0 deletions src/schemas/tutor.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { firestore } from "@/config";
import { z } from "zod";

export const tutorSchema = z.object({
id: z.string(),
name: z.string().min(3, "Name must be at least 3 characters"),
phoneNum: z
.string()
.min(10, "Phone number must be at least 10 characters")
.optional(),
location: z
.object({
latitude: z.number({ message: "Latitude must be a number" }),
longitude: z.number({ message: "Longitude must be a number" }),
})
.optional(),
services: z
.array(z.string())
.superRefine(async (services, ctx) => {
// Validate that the services are valid by checking the tutor_services collection
try {
const servicesSnapshot = await firestore
.collection("tutor_services")
.get();
const validServices = servicesSnapshot.docs.map((doc) => doc.id);
const invalidServices = services.filter(
(service) => !validServices.includes(service),
);

if (invalidServices.length > 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Invalid services found: ${invalidServices.join(", ")}`,
});
}
} catch (error) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Failed to validate services due to an internal error",
});
}
})
.optional(),
coverageRange: z.number().optional(),
createdAt: z.date(),
updatedAt: z.date().optional(),
lastSeen: z.date().optional(),
});

export const tutorServiceSchema = z.object({
id: z.string(),
tutorId: z.string().superRefine(async (tutorId, ctx) => {
// Validate that the tutor exists by checking the tutors collection
try {
const tutorSnapshot = await firestore
.collection("tutors")
.doc(tutorId)
.get();
if (!tutorSnapshot.exists) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Tutor does not exist",
});
}
} catch (error) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Failed to validate tutor due to an internal error",
});
}
}),
subjectId: z.string().superRefine(async (subjectId, ctx) => {
// Validate that the subject exists by checking the subjects collection
try {
const subjectSnapshot = await firestore
.collection("subjects")
.doc(subjectId)
.get();
if (!subjectSnapshot.exists) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Subject does not exist",
});
}
} catch (error) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Failed to validate subject due to an internal error",
});
}
}),
// About the tutor's experience with the subject
aboutYou: z.string().optional(),
// Tutor's teaching methodology
teachingMethodology: z.string().optional(),
hourlyRate: z.number().optional(),
createdAt: z.date(),
updatedAt: z.date().optional(),
});
2 changes: 1 addition & 1 deletion src/schemas/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const userSchema = z.object({
})
.optional(),
createdAt: z.date(),
updatedAt: z.date(),
updatedAt: z.date().optional(),
lastSeen: z.date().optional(),
interests: z
.array(z.string())
Expand Down
13 changes: 3 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tutorSchema, tutorServiceSchema } from "@schemas/tutor.schema";
import { userSchema } from "@schemas/user.schema";
import type { RequestHandler } from "express";
import { z } from "zod";
Expand All @@ -12,16 +13,8 @@ export interface Controller<T extends RequestData = RequestData>
extends RequestHandler<T["params"], {}, T["body"], T["query"]> {}

export type User = z.infer<typeof userSchema>;

export interface Tutor {
id: string;
phoneNum?: string;
location?: unknown; // not sure now
coverageRange: number;
createdAt: Date;
updatedAt: Date;
lastSeen?: Date;
}
export type Tutor = z.infer<typeof tutorSchema>;
export type Service = z.infer<typeof tutorServiceSchema>;

export interface Subject {
name: string;
Expand Down

0 comments on commit 4400819

Please sign in to comment.