Skip to content

Commit

Permalink
feat(tutorservice): add getMyServices
Browse files Browse the repository at this point in the history
  • Loading branch information
Famozzy committed Nov 19, 2024
1 parent 0afbb9d commit a2b4a6a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/controllers/tutorService.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,18 @@ export const getServices: Controller<GetServicesSchema> = async (req, res) => {
};

type GetServiceSchema = z.infer<typeof getServiceSchema>;
export const getService: Controller<GetServiceSchema> = async (req, res) => {
export const getService: Controller<GetServiceSchema> = async (
req,
res,
next,
) => {
const tutorServiceId = req.params.tutorServiceId;

if (tutorServiceId === "me") {
next();
return;
}

try {
const service =
await tutorServiceService.getTutorServiceDetail(tutorServiceId);
Expand All @@ -74,6 +83,28 @@ export const getService: Controller<GetServiceSchema> = async (req, res) => {
}
};

export const getMyServices: Controller = async (req, res) => {
const tutorId = req.tutor.id;

try {
const services = await tutorServiceService.getTutorServices({
tutorId,
});

res.json({
status: "success",
data: services,
});
} catch (error) {
logger.error(`Failed to get tutor services: ${error}`);

res.status(500).json({
status: "error",
message: `Failed to get tutor services`,
});
}
};

type CreateTutorServiceSchema = z.infer<typeof createTutorServiceSchema>;
export const createService: Controller<CreateTutorServiceSchema> = async (
req,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/v1/tutorService.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ tutorServiceRouter.get("/:tutorServiceId", tutorServiceController.getService);

tutorServiceRouter.use(verifyTutor);

tutorServiceRouter.get("/me", tutorServiceController.getMyServices);

tutorServiceRouter.post(
"/",
validator(createTutorServiceSchema),
Expand Down
9 changes: 9 additions & 0 deletions src/services/tutorService.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class TutorServiceService {
minHourlyRate = null,
maxHourlyRate = null,
typeLesson = null,
tutorId = null,
}: GetTutorServicesFilters = {}) {
try {
let query: FirebaseFirestore.Query<FirebaseFirestore.DocumentData> =
Expand All @@ -55,6 +56,14 @@ export class TutorServiceService {
);
}

if (tutorId) {
query = query.where(
"tutorId",
"==",
this.firestore.doc(`/tutors/${tutorId}`),
);
}

if (minHourlyRate !== null && maxHourlyRate !== null) {
query = query
.where("hourlyRate", ">=", minHourlyRate)
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { learnerSchema } from "@schemas/learner.schema";
import { z } from "zod";
import { tutorServiceSchema } from "@schemas/tutorService.schema";
import { ParsedQs } from "qs";
import { Request, Response } from "express";
import { NextFunction, Request, Response } from "express";

interface RequestData {
body?: unknown;
Expand All @@ -15,6 +15,7 @@ export interface Controller<T extends RequestData = RequestData> {
(
req: Request<T["params"], unknown, T["body"], T["query"]>,
res: Response,
next: NextFunction,
): Promise<void>;
}

Expand Down

0 comments on commit a2b4a6a

Please sign in to comment.