mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
fix: improve trail api
This commit is contained in:
parent
e14ba02f97
commit
83dbdfdc05
5 changed files with 53 additions and 22 deletions
|
|
@ -11,8 +11,8 @@ import { getCourse } from "@services/courses/courses";
|
||||||
import VideoActivity from "@components/ActivityViews/Video/Video";
|
import VideoActivity from "@components/ActivityViews/Video/Video";
|
||||||
import useSWR, { mutate } from "swr";
|
import useSWR, { mutate } from "swr";
|
||||||
import { Check } from "lucide-react";
|
import { Check } from "lucide-react";
|
||||||
import { maskActivityAsComplete } from "@services/courses/activity";
|
|
||||||
import { swrFetcher } from "@services/utils/requests";
|
import { swrFetcher } from "@services/utils/requests";
|
||||||
|
import { markActivityAsComplete } from "@services/courses/activity";
|
||||||
|
|
||||||
function ActivityPage(params: any) {
|
function ActivityPage(params: any) {
|
||||||
const activityid = params.params.activityid;
|
const activityid = params.params.activityid;
|
||||||
|
|
@ -20,12 +20,11 @@ function ActivityPage(params: any) {
|
||||||
const orgslug = params.params.orgslug;
|
const orgslug = params.params.orgslug;
|
||||||
|
|
||||||
const { data: course, error: error_course } = useSWR(`${getAPIUrl()}courses/meta/course_${courseid}`, swrFetcher);
|
const { data: course, error: error_course } = useSWR(`${getAPIUrl()}courses/meta/course_${courseid}`, swrFetcher);
|
||||||
const { data: activity, error: error_activity } = useSWR(`${getAPIUrl()}activities/activity_${activityid}`, swrFetcher);
|
const { data: activity, error: error_activity } = useSWR(`${getAPIUrl()}trail/org_slug/${orgslug}/trail`, swrFetcher);
|
||||||
|
|
||||||
console.log(course, activity);
|
|
||||||
|
|
||||||
async function markActivityAsCompleteFront() {
|
async function markActivityAsCompleteFront() {
|
||||||
const activity = await maskActivityAsComplete("" + activityid, courseid, activity.activity_id.replace("activity_", ""));
|
const trail = await markActivityAsComplete(orgslug, courseid, activityid);
|
||||||
mutate(`${getAPIUrl()}activities/activity_${activityid}`);
|
mutate(`${getAPIUrl()}activities/activity_${activityid}`);
|
||||||
mutate(`${getAPIUrl()}courses/meta/course_${courseid}`);
|
mutate(`${getAPIUrl()}courses/meta/course_${courseid}`);
|
||||||
}
|
}
|
||||||
|
|
@ -76,9 +75,10 @@ function ActivityPage(params: any) {
|
||||||
{activity.type == "video" && <VideoActivity course={course} activity={activity} />}
|
{activity.type == "video" && <VideoActivity course={course} activity={activity} />}
|
||||||
|
|
||||||
<ActivityMarkerWrapper>
|
<ActivityMarkerWrapper>
|
||||||
{course.activity.activities_marked_complete &&
|
|
||||||
course.activity.activities_marked_complete.includes("activity_" + activityid) &&
|
{course.trail.activities_marked_complete &&
|
||||||
course.activity.status == "ongoing" ? (
|
course.trail.activities_marked_complete.includes("activity_" + activityid) &&
|
||||||
|
course.trail.status == "ongoing" ? (
|
||||||
<button style={{ backgroundColor: "green" }}>
|
<button style={{ backgroundColor: "green" }}>
|
||||||
<i>
|
<i>
|
||||||
<Check size={20}></Check>
|
<Check size={20}></Check>
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ const CourseIdPage = (params: any) => {
|
||||||
|
|
||||||
async function quitCourse() {
|
async function quitCourse() {
|
||||||
|
|
||||||
|
|
||||||
// Close activity
|
// Close activity
|
||||||
let activity = await removeCourse("course_" + courseid, orgslug);
|
let activity = await removeCourse("course_" + courseid, orgslug);
|
||||||
console.log(activity);
|
console.log(activity);
|
||||||
|
|
|
||||||
|
|
@ -7,21 +7,21 @@ import { getAPIUrl } from "@services/config/config";
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export async function startCourse(course_id: string, org_slug: string) {
|
export async function startCourse(course_id: string, org_slug: string) {
|
||||||
const result: any = await fetch(`${getAPIUrl()}trail/${org_slug}/add_course/${course_id}`, RequestBody("POST", null))
|
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/add_course/${course_id}`, RequestBody("POST", null))
|
||||||
.then((result) => result.json())
|
.then((result) => result.json())
|
||||||
.catch((error) => console.log("error", error));
|
.catch((error) => console.log("error", error));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeCourse(course_id: string, org_slug: string) {
|
export async function removeCourse(course_id: string, org_slug: string) {
|
||||||
const result: any = await fetch(`${getAPIUrl()}trail/${org_slug}/remove_course/${course_id}`, RequestBody("POST", null))
|
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/remove_course/${course_id}`, RequestBody("POST", null))
|
||||||
.then((result) => result.json())
|
.then((result) => result.json())
|
||||||
.catch((error) => console.log("error", error));
|
.catch((error) => console.log("error", error));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function maskActivityAsComplete(org_id: string, course_id: string, activity_id: string) {
|
export async function markActivityAsComplete(org_slug: string, course_id: string, activity_id: string) {
|
||||||
const result: any = await fetch(`${getAPIUrl()}activity/${org_id}/add_activity/${course_id}/${activity_id}`, RequestBody("POST", null))
|
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/add_activity/course_id/${course_id}/activity_id/${activity_id}`, RequestBody("POST", null))
|
||||||
.then((result) => result.json())
|
.then((result) => result.json())
|
||||||
.catch((error) => console.log("error", error));
|
.catch((error) => console.log("error", error));
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,10 @@ async def api_get_trail_by_orgslug(request: Request, org_slug: str, user=Depends
|
||||||
"""
|
"""
|
||||||
return await get_user_trail_with_orgslug(request, user, org_slug=org_slug)
|
return await get_user_trail_with_orgslug(request, user, org_slug=org_slug)
|
||||||
|
|
||||||
|
# Courses in trail
|
||||||
|
|
||||||
@router.post("/{org_slug}/add_course/{course_id}")
|
|
||||||
|
@router.post("/org_slug/{org_slug}/add_course/{course_id}")
|
||||||
async def api_add_course_to_trail(request: Request, course_id: str, org_slug: str, user=Depends(get_current_user)):
|
async def api_add_course_to_trail(request: Request, course_id: str, org_slug: str, user=Depends(get_current_user)):
|
||||||
"""
|
"""
|
||||||
Add Course to trail
|
Add Course to trail
|
||||||
|
|
@ -39,9 +41,17 @@ async def api_add_course_to_trail(request: Request, course_id: str, org_slug: s
|
||||||
return await add_course_to_trail(request, user, org_slug, course_id)
|
return await add_course_to_trail(request, user, org_slug, course_id)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{org_slug}/remove_course/{course_id}")
|
@router.post("/org_slug/{org_slug}/remove_course/{course_id}")
|
||||||
async def api_remove_course_to_trail(request: Request, course_id: str, org_slug: str, user=Depends(get_current_user)):
|
async def api_remove_course_to_trail(request: Request, course_id: str, org_slug: str, user=Depends(get_current_user)):
|
||||||
"""
|
"""
|
||||||
Remove Course from trail
|
Remove Course from trail
|
||||||
"""
|
"""
|
||||||
return await remove_course_from_trail(request, user, org_slug, course_id)
|
return await remove_course_from_trail(request, user, org_slug, course_id)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/org_slug/{org_slug}/add_activity/course_id/{course_id}/activity_id/{activity_id}")
|
||||||
|
async def api_add_activity_to_trail(request: Request, activity_id: str, course_id: str, org_slug: str, user=Depends(get_current_user)):
|
||||||
|
"""
|
||||||
|
Add Course to trail
|
||||||
|
"""
|
||||||
|
return await add_activity_to_trail(request, user, course_id, org_slug, activity_id)
|
||||||
|
|
|
||||||
|
|
@ -130,18 +130,41 @@ async def get_user_trail_with_orgslug(request: Request, user: PublicUser, org_sl
|
||||||
return Trail(**trail)
|
return Trail(**trail)
|
||||||
|
|
||||||
|
|
||||||
async def add_activity_to_trail(request: Request, user: PublicUser, trail_id: str, course_id: str, activity_id: str) -> Trail:
|
async def add_activity_to_trail(request: Request, user: PublicUser, course_id: str, org_slug: str, activity_id: str) -> Trail:
|
||||||
trails = request.app.db["trails"]
|
trails = request.app.db["trails"]
|
||||||
trail = await trails.find_one({"trail_id": trail_id, "user_id": user.user_id})
|
orgs = request.app.db["organizations"]
|
||||||
|
courseid = "course_" + course_id
|
||||||
|
activityid = "activity_" + activity_id
|
||||||
|
|
||||||
|
# get org_id from orgslug
|
||||||
|
org = await orgs.find_one({"slug": org_slug})
|
||||||
|
org_id = org["org_id"]
|
||||||
|
|
||||||
|
|
||||||
|
# find a trail with the user_id and course_id in the courses array
|
||||||
|
trail = await trails.find_one({"user_id": user.user_id, "courses.course_id": courseid , "org_id": org_id})
|
||||||
|
|
||||||
if not trail:
|
if not trail:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND, detail="Trail not found")
|
status_code=status.HTTP_404_NOT_FOUND, detail="Trail not found")
|
||||||
|
|
||||||
|
# if a trail has course_id in the courses array, then add the activity_id to the activities_marked_complete array
|
||||||
for element in trail["courses"]:
|
for element in trail["courses"]:
|
||||||
if element["course_id"] == course_id:
|
if element["course_id"] == courseid:
|
||||||
if activity_id not in element["activities_marked_complete"]:
|
if "activities_marked_complete" in element:
|
||||||
element["activities_marked_complete"].append(activity_id)
|
|
||||||
break
|
# check if activity_id is already in the array
|
||||||
await trails.replace_one({"trail_id": trail_id}, trail)
|
if activityid not in element["activities_marked_complete"]:
|
||||||
|
element["activities_marked_complete"].append(activityid)
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST, detail="Activity already marked complete")
|
||||||
|
else:
|
||||||
|
element["activities_marked_complete"] = [activity_id]
|
||||||
|
|
||||||
|
# modify trail object
|
||||||
|
await trails.replace_one({"trail_id": trail["trail_id"]}, trail)
|
||||||
|
|
||||||
return Trail(**trail)
|
return Trail(**trail)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -164,7 +187,6 @@ async def add_course_to_trail(request: Request, user: PublicUser, orgslug: str,
|
||||||
status_code=status.HTTP_404_NOT_FOUND, detail="Trail not found")
|
status_code=status.HTTP_404_NOT_FOUND, detail="Trail not found")
|
||||||
|
|
||||||
# check if course is already present in the trail
|
# check if course is already present in the trail
|
||||||
|
|
||||||
for element in trail["courses"]:
|
for element in trail["courses"]:
|
||||||
if element["course_id"] == course_id:
|
if element["course_id"] == course_id:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue