Skip to content

Commit

Permalink
feat(tutorservice): get tutor's other service
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 21, 2024
1 parent 0c8b845 commit a723372
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/module/tutor-service/tutorService.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,31 @@ export class TutorServiceService {
return null;
}

// TODO: also teaches
// TODO: reviews

// Fetch all services by the same tutor, excluding the current serviceId
const tutorServicesQuery = await this.firestore
.collection("tutor_services")
.where("tutorId", "==", tutorDoc.ref)
.get();

const alsoTeaches = await Promise.all(
tutorServicesQuery.docs
.filter((doc) => doc.id !== serviceId)
.map(async (doc) => {
const serviceData = doc.data();

const subjectDoc = await serviceData.subjectId?.get();

return {
id: doc.id,
subjectName: subjectDoc.data()?.name,
hourlyRate: serviceData.hourlyRate,
typeLesson: serviceData.typeLesson,
};
}),
);

return {
id: tutorServiceDoc.id,
tutorName: tutorData.name,
Expand All @@ -245,6 +267,7 @@ export class TutorServiceService {
aboutYou: data.aboutYou,
teachingMethodology: data.teachingMethodology,
location: tutorData.city,
alsoTeaches,
};
} catch (error) {
throw new Error(`Failed to get tutor service detail: ${error}`);
Expand Down
58 changes: 55 additions & 3 deletions tests/services/tutorService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,66 @@ describe("TutorServiceService", () => {
data: () => ({ name: "Mathematics" }),
};

const mockServiceDoc = {
exists: true,
id: serviceId,
data: () => mockService,
};

const mockAlsoTeaches = [
{
id: "service2",
subjectName: "Physics",
hourlyRate: 120000,
typeLesson: "offline",
},
{
id: "service3",
subjectName: "Chemistry",
hourlyRate: 110000,
typeLesson: "hybrid",
},
];

mockService.tutorId.get.mockResolvedValue(mockTutor);
mockService.subjectId.get.mockResolvedValue(mockSubject);

firestore.collection.mockReturnValue({
doc: vi.fn().mockReturnValue({
get: vi.fn().mockResolvedValue(mockServiceDoc),
}),
where: vi.fn().mockReturnValue({
get: vi.fn().mockResolvedValue({
exists: true,
id: serviceId,
data: () => mockService,
docs: [
{
id: "service2",
data: () => ({
tutorId: mockTutor,
subjectId: {
get: vi.fn().mockResolvedValue({
exists: true,
data: () => ({ name: "Physics" }),
}),
},
hourlyRate: 120000,
typeLesson: "offline",
}),
},
{
id: "service3",
data: () => ({
tutorId: mockTutor,
subjectId: {
get: vi.fn().mockResolvedValue({
exists: true,
data: () => ({ name: "Chemistry" }),
}),
},
hourlyRate: 110000,
typeLesson: "hybrid",
}),
},
],
}),
}),
});
Expand All @@ -289,6 +340,7 @@ describe("TutorServiceService", () => {
typeLesson: "online",
aboutYou: "About me",
teachingMethodology: "My methodology",
alsoTeaches: mockAlsoTeaches,
});
});

Expand Down

0 comments on commit a723372

Please sign in to comment.