mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-05-18 04:05:51 +00:00
Fix course status hook
This commit is contained in:
parent
2df2724d83
commit
fe295d29f4
@ -1,7 +1,6 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useSession } from 'next-auth/react';
|
import { useSession } from 'next-auth/react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { checkCourseCompletion } from '@/db/models/userCourseModels';
|
|
||||||
|
|
||||||
const useCheckCourseProgress = () => {
|
const useCheckCourseProgress = () => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
@ -15,20 +14,23 @@ const useCheckCourseProgress = () => {
|
|||||||
|
|
||||||
for (const userCourse of userCourses) {
|
for (const userCourse of userCourses) {
|
||||||
const courseId = userCourse.courseId;
|
const courseId = userCourse.courseId;
|
||||||
const isCompleted = await checkCourseCompletion(userId, courseId);
|
|
||||||
|
|
||||||
if (isCompleted) {
|
|
||||||
try {
|
try {
|
||||||
|
const response = await axios.get(`/api/users/${userId}/courses/${courseId}`);
|
||||||
|
const isCompleted = response.data.completed;
|
||||||
|
console.log("IS COMPLETED", isCompleted);
|
||||||
|
|
||||||
|
if (isCompleted && !userCourse.completed) {
|
||||||
await axios.put(`/api/users/${userId}/courses/${courseId}`, {
|
await axios.put(`/api/users/${userId}/courses/${courseId}`, {
|
||||||
completed: true,
|
completed: true,
|
||||||
completedAt: new Date().toISOString(),
|
completedAt: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
console.log(`Course ${courseId} marked as completed for user ${userId}`);
|
console.log(`Course ${courseId} marked as completed for user ${userId}`);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to update course ${courseId} completion status:`, error);
|
console.error(`Failed to update course ${courseId} completion status:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
updateCourseCompletionStatus();
|
updateCourseCompletionStatus();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user