Skip to content

Commit

Permalink
refactor(recommendation): get learner id from current logged in learner
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Dec 10, 2024
1 parent 12ecc02 commit ea9d90d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 44 deletions.
11 changes: 3 additions & 8 deletions src/module/tutories/tutories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
createTutoriesSchema,
deleteTutoriesSchema,
getAverageRateSchema,
getRecommendationsSchema,
getServiceSchema,
getTutoriesSchema,
trackInteractionSchema,
Expand Down Expand Up @@ -280,12 +279,8 @@ export const deleteTutories: Controller<DeleteTutorServiceSchema> = async (
}
};

type GetRecommendationsSchema = z.infer<typeof getRecommendationsSchema>;
export const getRecommendations: Controller<GetRecommendationsSchema> = async (
req,
res,
) => {
const learnerId = req.params.learnerId;
export const getRecommendations: Controller = async (req, res) => {
const learnerId = req.learner.id;

try {
const recommendations = await tutoriesService.getRecommendations(learnerId);
Expand All @@ -309,7 +304,7 @@ export const trackInteraction: Controller<TrackInteractionSchema> = async (
req,
res,
) => {
const learnerId = req.params.learnerId;
const learnerId = req.learner.id;
const tutoriesId = req.params.tutoriesId;

try {
Expand Down
14 changes: 9 additions & 5 deletions src/module/tutories/tutories.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { jwtAuthMiddleware, verifyTutor } from "@/module/auth/auth.middleware";
import {
jwtAuthMiddleware,
verifyLearner,
verifyTutor,
} from "@/module/auth/auth.middleware";
import * as tutoriesController from "@/module/tutories/tutories.controller";
import {
createTutoriesSchema,
getAverageRateSchema,
getRecommendationsSchema,
getTutoriesSchema,
trackInteractionSchema,
updateTutoriesSchema,
Expand Down Expand Up @@ -36,14 +39,15 @@ tutoriesRouter.get(
);

tutoriesRouter.get(
"/recommendations/:learnerId",
"/recommendations",
// #swagger.tags = ['tutors/services']
validator(getRecommendationsSchema),
verifyLearner,
tutoriesController.getRecommendations,
);

tutoriesRouter.get(
"/interaction/:learnerId/:tutoriesId",
"/interaction/:tutoriesId",
verifyLearner,
validator(trackInteractionSchema),
tutoriesController.trackInteraction,
);
Expand Down
31 changes: 0 additions & 31 deletions src/module/tutories/tutories.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,39 +151,8 @@ export const deleteTutoriesSchema = z.object({
}),
});

export const getRecommendationsSchema = z.object({
params: z.object({
learnerId: z.string().superRefine(async (learnerId, ctx) => {
try {
const exists =
await container.learnerRepository.checkLearnerExists(learnerId);
if (!exists) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Learner does not exist",
});
}
} catch (error) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Failed to check if learner exists ",
});
}
}),
}),
});

export const trackInteractionSchema = z.object({
params: z.object({
learnerId: z.string().superRefine(async (learnerId, ctx) => {
const exists = await learnerRepository.checkLearnerExists(learnerId);
if (!exists) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Learner does not exist",
});
}
}),
tutoriesId: z.string().superRefine(async (tutoriesId, ctx) => {
const exists = await tutoriesRepository.checkTutoriesExists(tutoriesId);
if (!exists) {
Expand Down

0 comments on commit ea9d90d

Please sign in to comment.