mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: refactor the entire learnhouse project
This commit is contained in:
parent
f556e41dda
commit
4c215e91d5
247 changed files with 7716 additions and 1013 deletions
103
apps/web/services/auth/auth.ts
Normal file
103
apps/web/services/auth/auth.ts
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { NextApiRequestCookies } from "next/dist/server/api-utils";
|
||||
|
||||
interface LoginAndGetTokenResponse {
|
||||
access_token: "string";
|
||||
token_type: "string";
|
||||
}
|
||||
|
||||
// ⚠️ mvp phase code
|
||||
// TODO : everything in this file need to be refactored including security issues fix
|
||||
|
||||
export async function loginAndGetToken(username: string, password: string): Promise<any> {
|
||||
// Request Config
|
||||
|
||||
// get origin
|
||||
const HeadersConfig = new Headers({ "Content-Type": "application/x-www-form-urlencoded" });
|
||||
const urlencoded = new URLSearchParams({ username: username, password: password });
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "POST",
|
||||
headers: HeadersConfig,
|
||||
body: urlencoded,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
// fetch using await and async
|
||||
const response = await fetch(`${getAPIUrl()}auth/login`, requestOptions);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function getUserInfo(token: string): Promise<any> {
|
||||
const origin = window.location.origin;
|
||||
const HeadersConfig = new Headers({ Authorization: `Bearer ${token}`, Origin: origin });
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "GET",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
return fetch(`${getAPIUrl()}users/profile_metadata`, requestOptions)
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getNewAccessTokenUsingRefreshToken(): Promise<any> {
|
||||
const requestOptions: any = {
|
||||
method: "POST",
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
return fetch(`${getAPIUrl()}auth/refresh`, requestOptions)
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getNewAccessTokenUsingRefreshTokenServer(refresh_token_cookie: any): Promise<any> {
|
||||
const requestOptions: any = {
|
||||
method: "POST",
|
||||
redirect: "follow",
|
||||
headers: {
|
||||
Cookie: `refresh_token_cookie=${refresh_token_cookie}`,
|
||||
},
|
||||
credentials: "include",
|
||||
};
|
||||
return fetch(`${getAPIUrl()}auth/refresh`, requestOptions)
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
// cookies
|
||||
|
||||
export async function getAccessTokenFromRefreshTokenCookie(cookieStore: any) {
|
||||
const refresh_token_cookie: any = cookieStore.get("refresh_token_cookie");
|
||||
const access_token_cookie: any = await getNewAccessTokenUsingRefreshTokenServer(refresh_token_cookie?.value);
|
||||
return access_token_cookie && refresh_token_cookie ? access_token_cookie.access_token : null;
|
||||
}
|
||||
|
||||
// signup
|
||||
|
||||
interface NewAccountBody {
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
org_slug: string;
|
||||
}
|
||||
|
||||
export async function signup(body: NewAccountBody): Promise<any> {
|
||||
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "POST",
|
||||
headers: HeadersConfig,
|
||||
body: JSON.stringify(body),
|
||||
redirect: "follow",
|
||||
};
|
||||
|
||||
const res = await fetch(`${getAPIUrl()}users/?org_slug=${body.org_slug}`, requestOptions);
|
||||
return res;
|
||||
}
|
||||
20
apps/web/services/blocks/Image/images.ts
Normal file
20
apps/web/services/blocks/Image/images.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
|
||||
|
||||
export async function uploadNewImageFile(file: any, activity_id: string) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
formData.append("file_object", file);
|
||||
formData.append("activity_id", activity_id);
|
||||
|
||||
return fetch(`${getAPIUrl()}blocks/image`, RequestBodyForm("POST", formData, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getImageFile(file_id: string) {
|
||||
// todo : add course id to url
|
||||
return fetch(`${getAPIUrl()}blocks/image?file_id=${file_id}`, RequestBody("GET", null, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
20
apps/web/services/blocks/Pdf/pdf.ts
Normal file
20
apps/web/services/blocks/Pdf/pdf.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
|
||||
|
||||
export async function uploadNewPDFFile(file: any, activity_id: string) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
formData.append("file_object", file);
|
||||
formData.append("activity_id", activity_id);
|
||||
|
||||
return fetch(`${getAPIUrl()}blocks/pdf`, RequestBodyForm("POST", formData, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getPDFFile(file_id: string) {
|
||||
// todo : add course id to url
|
||||
return fetch(`${getAPIUrl()}blocks/pdf?file_id=${file_id}`, RequestBody("GET", null, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
10
apps/web/services/blocks/Quiz/quiz.ts
Normal file
10
apps/web/services/blocks/Quiz/quiz.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
|
||||
|
||||
|
||||
export async function submitQuizBlock(activity_id: string, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}blocks/quiz/${activity_id}"`, RequestBody("POST", data, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
return result;
|
||||
}
|
||||
19
apps/web/services/blocks/Video/video.ts
Normal file
19
apps/web/services/blocks/Video/video.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
|
||||
|
||||
export async function uploadNewVideoFile(file: any, activity_id: string) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
formData.append("file_object", file);
|
||||
formData.append("activity_id", activity_id);
|
||||
|
||||
return fetch(`${getAPIUrl()}blocks/video`, RequestBodyForm("POST", formData, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getVideoFile(file_id: string) {
|
||||
return fetch(`${getAPIUrl()}blocks/video?file_id=${file_id}`, RequestBody("GET", null, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
35
apps/web/services/config/config.ts
Normal file
35
apps/web/services/config/config.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
export const LEARNHOUSE_HTTP_PROTOCOL = process.env.NEXT_PUBLIC_LEARNHOUSE_HTTPS === "true" ? "https://" : "http://";
|
||||
const LEARNHOUSE_API_URL = `${process.env.NEXT_PUBLIC_LEARNHOUSE_API_URL}`;
|
||||
export const LEARNHOUSE_BACKEND_URL = `${process.env.NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL}`;
|
||||
export const LEARNHOUSE_DOMAIN = process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN;
|
||||
|
||||
export const getAPIUrl = () => LEARNHOUSE_API_URL;
|
||||
export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL;
|
||||
|
||||
// Multi Organization Mode
|
||||
export const isMultiOrgModeEnabled = () => (process.env.NEXT_PUBLIC_LEARNHOUSE_MULTI_ORG === "true" ? true : false);
|
||||
|
||||
export const getUriWithOrg = (orgslug: string, path: string) => {
|
||||
const multi_org = isMultiOrgModeEnabled();
|
||||
if (multi_org) {
|
||||
return `${LEARNHOUSE_HTTP_PROTOCOL}${orgslug}.${LEARNHOUSE_DOMAIN}${path}`;
|
||||
}
|
||||
return `${LEARNHOUSE_HTTP_PROTOCOL}${LEARNHOUSE_DOMAIN}${path}`;
|
||||
};
|
||||
|
||||
export const getOrgFromUri = () => {
|
||||
const multi_org = isMultiOrgModeEnabled();
|
||||
if (multi_org) {
|
||||
getDefaultOrg();
|
||||
} else {
|
||||
if (typeof window !== "undefined") {
|
||||
const hostname = window.location.hostname;
|
||||
|
||||
return hostname.replace(`.${LEARNHOUSE_DOMAIN}`, "");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const getDefaultOrg = () => {
|
||||
return process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG;
|
||||
};
|
||||
71
apps/web/services/courses/activities.ts
Normal file
71
apps/web/services/courses/activities.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyForm, RequestBodyWithAuthHeader } from "@services/utils/ts/requests";
|
||||
|
||||
export async function createActivity(data: any, chapter_id: any, org_id: any) {
|
||||
data.content = {};
|
||||
// remove chapter_id from data
|
||||
delete data.chapterId;
|
||||
|
||||
const result = await fetch(`${getAPIUrl()}activities/?coursechapter_id=${chapter_id}&org_id=${org_id}`, RequestBody("POST", data, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createFileActivity(file: File, type: string, data: any, chapter_id: any) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
formData.append("coursechapter_id", chapter_id);
|
||||
|
||||
let org_id = "test";
|
||||
let endpoint = "";
|
||||
|
||||
if (type === "video") {
|
||||
formData.append("name", data.name);
|
||||
formData.append("video_file", file);
|
||||
endpoint = `${getAPIUrl()}activities/video?org_id=${org_id}`;
|
||||
} else if (type === "documentpdf") {
|
||||
formData.append("pdf_file", file);
|
||||
formData.append("name", data.name);
|
||||
endpoint = `${getAPIUrl()}activities/documentpdf?org_id=${org_id}`;
|
||||
} else {
|
||||
// Handle other file types here
|
||||
}
|
||||
|
||||
const result: any = await fetch(endpoint, RequestBodyForm("POST", formData, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createExternalVideoActivity(data: any, activity: any, chapter_id: any) {
|
||||
// add coursechapter_id to data
|
||||
data.coursechapter_id = chapter_id;
|
||||
data.activity_id = activity.id;
|
||||
|
||||
const result = await fetch(`${getAPIUrl()}activities/external_video?coursechapter_id=${chapter_id}`, RequestBody("POST", data, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getActivity(activity_id: any, next: any) {
|
||||
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("GET", null, next));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function deleteActivity(activity_id: any) {
|
||||
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getActivityWithAuthHeader(activity_id: any, next: any, access_token: string) {
|
||||
const result = await fetch(`${getAPIUrl()}activities/activity_${activity_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function updateActivity(data: any, activity_id: any) {
|
||||
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("PUT", data, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
25
apps/web/services/courses/activity.ts
Normal file
25
apps/web/services/courses/activity.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
||||
import { getAPIUrl } from "@services/config/config";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function startCourse(course_id: string, org_slug: string) {
|
||||
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/add_course/${course_id}`, RequestBody("POST", null, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function removeCourse(course_id: string, org_slug: string) {
|
||||
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/remove_course/${course_id}`, RequestBody("POST", null, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function markActivityAsComplete(org_slug: string, course_id: string, activity_id: string) {
|
||||
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/add_activity/course_id/${course_id}/activity_id/${activity_id}`, RequestBody("POST", null, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
41
apps/web/services/courses/chapters.ts
Normal file
41
apps/web/services/courses/chapters.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyWithAuthHeader, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
//TODO : depreciate this function
|
||||
export async function getCourseChaptersMetadata(course_id: any, next: any) {
|
||||
const result = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null, next));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function updateChaptersMetadata(course_id: any, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function updateChapter(coursechapter_id: any, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("PUT", data, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createChapter(data: any, course_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, RequestBody("POST", data, null));
|
||||
const res = await errorHandling(result);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function deleteChapter(coursechapter_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
47
apps/web/services/courses/collections.ts
Normal file
47
apps/web/services/courses/collections.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { getAPIUrl } from "../config/config";
|
||||
import { RequestBody, RequestBodyWithAuthHeader, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function deleteCollection(collection_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/${collection_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Create a new collection
|
||||
export async function createCollection(collection: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/`, RequestBody("POST", collection, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Get a colletion by id
|
||||
export async function getCollectionById(collection_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/${collection_id}`, { next: { revalidate: 10 } });
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getCollectionByIdWithAuthHeader(collection_id: any, access_token: string, next: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/collection_${collection_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Get collections
|
||||
// TODO : add per org filter
|
||||
export async function getOrgCollections() {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/page/1/limit/10`, { next: { revalidate: 10 } });
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getOrgCollectionsWithAuthHeader(org_id: string, access_token: string, next: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/org_id/${org_id}/page/1/limit/10`, RequestBodyWithAuthHeader("GET", null, next, access_token));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
57
apps/web/services/courses/courses.ts
Normal file
57
apps/web/services/courses/courses.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, RequestBodyForm, RequestBodyWithAuthHeader, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function getOrgCourses(org_id: number, next: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`, RequestBody("GET", null, next));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getOrgCoursesWithAuthHeader(org_id: number, next: any, access_token: string) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`, RequestBodyWithAuthHeader("GET", null, next, access_token));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getCourseMetadataWithAuthHeader(course_id: any, next: any, access_token: string) {
|
||||
const result = await fetch(`${getAPIUrl()}courses/meta/course_${course_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function updateCourse(course_id: any, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/course_${course_id}`, RequestBody("PUT", data, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getCourse(course_id: string, next: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("GET", null, next));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createNewCourse(org_id: string, course_body: any, thumbnail: any) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
formData.append("thumbnail", thumbnail);
|
||||
formData.append("name", course_body.name);
|
||||
formData.append("description", course_body.description);
|
||||
formData.append("mini_description", "course_body.mini_description");
|
||||
formData.append("public", "true");
|
||||
|
||||
const result = await fetch(`${getAPIUrl()}courses/?org_id=${org_id}`, RequestBodyForm("POST", formData, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function deleteCourseFromBackend(course_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
42
apps/web/services/install/install.ts
Normal file
42
apps/web/services/install/install.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
export async function updateInstall(body: any, step: number) {
|
||||
const result = await fetch(`${getAPIUrl()}install/update?step=${step}`, RequestBody("POST", body, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createNewOrgInstall(body: any) {
|
||||
const result = await fetch(`${getAPIUrl()}install/org`, RequestBody("POST", body, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createNewUserInstall(body: any) {
|
||||
const result = await fetch(`${getAPIUrl()}install/user?org_slug=${body.org_slug}`, RequestBody("POST", body, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createSampleDataInstall(username: string, org_slug: string) {
|
||||
const result = await fetch(`${getAPIUrl()}install/sample?username=${username}&org_slug=${org_slug}`, RequestBody("POST", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function createDefaultElements() {
|
||||
const result = await fetch(`${getAPIUrl()}install/default_elements`, RequestBody("POST", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function isInstallModeEnabled() {
|
||||
const result = await fetch(`${getAPIUrl()}install/latest`, RequestBody("GET", null, null));
|
||||
if (result.status === 200 || result.status === 404) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
46
apps/web/services/media/media.ts
Normal file
46
apps/web/services/media/media.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { getBackendUrl } from "@services/config/config";
|
||||
const LEARNHOUSE_MEDIA_URL = process.env.NEXT_PUBLIC_LEARNHOUSE_MEDIA_URL;
|
||||
|
||||
function getMediaUrl() {
|
||||
if (LEARNHOUSE_MEDIA_URL) {
|
||||
return LEARNHOUSE_MEDIA_URL;
|
||||
} else {
|
||||
return getBackendUrl();
|
||||
}
|
||||
}
|
||||
|
||||
export function getCourseThumbnailMediaDirectory(orgId: string, courseId: string, fileId: string) {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/courses/${courseId}/thumbnails/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
|
||||
export function getActivityBlockMediaDirectory(orgId: string, courseId: string, activityId: string, blockId: any, fileId: any, type: string) {
|
||||
if (type == "pdfBlock") {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/courses/${courseId}/activities/${activityId}/dynamic/blocks/pdfBlock/${blockId}/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
if (type == "videoBlock") {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/courses/${courseId}/activities/${activityId}/dynamic/blocks/videoBlock/${blockId}/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
if (type == "imageBlock") {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/courses/${courseId}/activities/${activityId}/dynamic/blocks/imageBlock/${blockId}/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
|
||||
export function getActivityMediaDirectory(orgId: string, courseId: string, activityId: string, fileId: string, activityType: string) {
|
||||
if (activityType == "video") {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/courses/${courseId}/activities/${activityId}/video/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
if (activityType == "documentpdf") {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/courses/${courseId}/activities/${activityId}/documentpdf/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
|
||||
export function getOrgLogoMediaDirectory(orgId: string, fileId: string) {
|
||||
let uri = `${getMediaUrl()}content/${orgId}/logos/${fileId}`;
|
||||
return uri;
|
||||
}
|
||||
45
apps/web/services/organizations/orgs.ts
Normal file
45
apps/web/services/organizations/orgs.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function createNewOrganization(body: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/`, RequestBody("POST", body, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function deleteOrganizationFromBackend(org_id: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/${org_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getOrganizationContextInfo(org_slug: any, next: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, RequestBody("GET", null, next));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getOrganizationContextInfoWithoutCredentials(org_slug: any, next: any) {
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: 'GET',
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
|
||||
const result = await fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, options);
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export function getOrganizationContextInfoNoAsync(org_slug: any, next: any) {
|
||||
const result = fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, RequestBody("GET", null, next));
|
||||
return result;
|
||||
}
|
||||
22
apps/web/services/settings/org.ts
Normal file
22
apps/web/services/settings/org.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, errorHandling, RequestBodyForm } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function updateOrganization(org_id: string, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}orgs/` + org_id, RequestBody("PUT", data, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function uploadOrganizationLogo(org_id: string, logo_file: any) {
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
formData.append("logo_file", logo_file);
|
||||
const result: any = await fetch(`${getAPIUrl()}orgs/` + org_id + "/logo", RequestBodyForm("PUT", formData, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
13
apps/web/services/settings/password.ts
Normal file
13
apps/web/services/settings/password.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function updatePassword(user_id : string, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}users/password/user_id/` + user_id, RequestBody("PUT", data, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
13
apps/web/services/settings/profile.ts
Normal file
13
apps/web/services/settings/profile.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function updateProfile(data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}users/user_id/` + data.user_id, RequestBody("PUT", data, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
8
apps/web/services/users/users.ts
Normal file
8
apps/web/services/users/users.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { getAPIUrl } from "@services/config/config";
|
||||
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
||||
|
||||
export async function getUser(user_id: string) {
|
||||
const result = await fetch(`${getAPIUrl()}users/user_id/${user_id}`, RequestBody("GET", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
13
apps/web/services/utils/react/middlewares/views.ts
Normal file
13
apps/web/services/utils/react/middlewares/views.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";
|
||||
import { NextRouter } from "next/router";
|
||||
|
||||
export const denyAccessToUser = (error: any, router: AppRouterInstance) => {
|
||||
if (error.status === 401) {
|
||||
router.push("/login");
|
||||
}
|
||||
|
||||
if (error.status === 403) {
|
||||
router.push("/login");
|
||||
// TODO : add a message to the user to tell him he is not allowed to access this page, route to /error
|
||||
}
|
||||
};
|
||||
83
apps/web/services/utils/ts/requests.ts
Normal file
83
apps/web/services/utils/ts/requests.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import { getUriWithOrg } from "@services/config/config";
|
||||
|
||||
export const RequestBody = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
if (data) {
|
||||
options.body = JSON.stringify(data);
|
||||
}
|
||||
return options;
|
||||
};
|
||||
|
||||
export const RequestBodyWithAuthHeader = (method: string, data: any, next: any, token: string) => {
|
||||
let HeadersConfig = new Headers(token ? { "Content-Type": "application/json", Authorization: `Bearer ${token}` } : { "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
body: data,
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
|
||||
export const RequestBodyForm = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({});
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
body: data,
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
|
||||
export const swrFetcher = async (url: string) => {
|
||||
// Create the request options
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: "GET",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
try {
|
||||
// Fetch the data
|
||||
const request = await fetch(url, options);
|
||||
let res = errorHandling(request);
|
||||
|
||||
// Return the data
|
||||
return res;
|
||||
} catch (error: any) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const errorHandling = (res: any) => {
|
||||
if (!res.ok) {
|
||||
const error: any = new Error(`${res.statusText}`);
|
||||
error.status = res.status;
|
||||
throw error;
|
||||
}
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export const revalidateTags = async (tags: string[], orgslug: string) => {
|
||||
const url = getUriWithOrg(orgslug, "");
|
||||
tags.forEach((tag) => {
|
||||
fetch(`${url}/api/revalidate?tag=${tag}`);
|
||||
});
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue