From a2b4a6acb6ec230264c114411c9486037482260a Mon Sep 17 00:00:00 2001 From: Faidil Date: Wed, 20 Nov 2024 00:49:10 +0800 Subject: [PATCH] feat(tutorservice): add getMyServices --- src/controllers/tutorService.controller.ts | 33 +++++++++++++++++++++- src/routes/v1/tutorService.route.ts | 2 ++ src/services/tutorService.service.ts | 9 ++++++ src/types.ts | 3 +- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/controllers/tutorService.controller.ts b/src/controllers/tutorService.controller.ts index 4617e25..ea122be 100644 --- a/src/controllers/tutorService.controller.ts +++ b/src/controllers/tutorService.controller.ts @@ -45,9 +45,18 @@ export const getServices: Controller = async (req, res) => { }; type GetServiceSchema = z.infer; -export const getService: Controller = async (req, res) => { +export const getService: Controller = async ( + req, + res, + next, +) => { const tutorServiceId = req.params.tutorServiceId; + if (tutorServiceId === "me") { + next(); + return; + } + try { const service = await tutorServiceService.getTutorServiceDetail(tutorServiceId); @@ -74,6 +83,28 @@ export const getService: Controller = 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; export const createService: Controller = async ( req, diff --git a/src/routes/v1/tutorService.route.ts b/src/routes/v1/tutorService.route.ts index 22ae704..5dfafcf 100644 --- a/src/routes/v1/tutorService.route.ts +++ b/src/routes/v1/tutorService.route.ts @@ -19,6 +19,8 @@ tutorServiceRouter.get("/:tutorServiceId", tutorServiceController.getService); tutorServiceRouter.use(verifyTutor); +tutorServiceRouter.get("/me", tutorServiceController.getMyServices); + tutorServiceRouter.post( "/", validator(createTutorServiceSchema), diff --git a/src/services/tutorService.service.ts b/src/services/tutorService.service.ts index ce3483f..d5c345b 100644 --- a/src/services/tutorService.service.ts +++ b/src/services/tutorService.service.ts @@ -42,6 +42,7 @@ export class TutorServiceService { minHourlyRate = null, maxHourlyRate = null, typeLesson = null, + tutorId = null, }: GetTutorServicesFilters = {}) { try { let query: FirebaseFirestore.Query = @@ -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) diff --git a/src/types.ts b/src/types.ts index 376d1d1..a668646 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; @@ -15,6 +15,7 @@ export interface Controller { ( req: Request, res: Response, + next: NextFunction, ): Promise; }