Skip to content

Commit

Permalink
debug: enrollment action
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamPalriwala committed Mar 5, 2024
1 parent 6b59427 commit 9021341
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/(dashboard)/enroll/[repositoryId]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const enrollCurrentUserAction = async (repositoryId: string) => {
}

const fullEnrollmentData = { repositoryId: repositoryId, userId: user.id };
console.log("full enroll data", fullEnrollmentData);

const enrollment = await createEnrollment(fullEnrollmentData);
return enrollment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ export default function EnrollmentStatusBar({ repositoryId }) {
async (isEnrolling) => {
setIsLoading(true);
try {
console.log("came in try for handleenrolmchange");

if (isEnrolling) {
console.log("is enrolling tru");

await enrollCurrentUserAction(repositoryId);
console.log("await over enrol current action");

toast({ title: "Let the games begin 🕹️", description: "You're successfully enrolled." });
} else {
await disenrollCurrentUserAction(repositoryId);
Expand All @@ -51,7 +57,7 @@ export default function EnrollmentStatusBar({ repositoryId }) {
console.error("Error changing enrollment status", error);
toast({
title: "Error",
description: "Failed to change enrollment status. Please try again.",
description: `Failed to change enrollment status due to: ${error.message}`,
});
} finally {
setIsLoading(false);
Expand Down
11 changes: 9 additions & 2 deletions lib/enrollment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TEnrollment, TEnrollmentInput, ZEnrollmentInput } from "@/types/enrollm
import { DatabaseError } from "@/types/errors";
import { TRepository } from "@/types/repository";
import { Prisma } from "@prisma/client";

import { validateInputs } from "../utils/validate";

/**
Expand All @@ -12,7 +13,6 @@ import { validateInputs } from "../utils/validate";
*/

export const createEnrollment = async (enrollmentData: TEnrollmentInput): Promise<TEnrollment> => {

validateInputs([enrollmentData, ZEnrollmentInput]);

try {
Expand All @@ -24,13 +24,20 @@ export const createEnrollment = async (enrollmentData: TEnrollmentInput): Promis
},
});

console.log("existing enrollment", existingEnrollment);

if (existingEnrollment) {
throw new Error("Enrollment already exists.");
}

console.log("creating new enrollment");

const enrollment = await db.enrollment.create({
data: enrollmentData,
});

console.log("new enrollment created", enrollment);

return enrollment;
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
Expand Down Expand Up @@ -87,7 +94,7 @@ export const hasEnrollmentForRepository = async (userId: string, repositoryId: s
* @param userId - The ID of the user for whom enrolled repositories are being queried.
* @returns A Promise that resolves to an array of TRepository objects, each representing
* a repository the user is enrolled in. The array is empty if the user has no enrollments.
*/
*/

export const getEnrolledRepositories = async (userId: string): Promise<TRepository[]> => {
const enrolledRepositories = await db.repository.findMany({
Expand Down

0 comments on commit 9021341

Please sign in to comment.