feat: Implement multi-tenancy with organization ID in LMS tables and middleware, refactor web API calls, and update analytics and gamification features."
This commit is contained in:
@@ -127,86 +127,85 @@ export interface Module {
|
||||
lessons: Lesson[];
|
||||
}
|
||||
|
||||
const getToken = () => typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
||||
|
||||
const apiFetch = async (url: string, options: RequestInit = {}, isCMS: boolean = false) => {
|
||||
const token = getToken();
|
||||
const baseUrl = isCMS ? CMS_API_URL : API_BASE_URL;
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
||||
};
|
||||
|
||||
const response = await fetch(`${baseUrl}${url}`, { ...options, headers });
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: response.statusText }));
|
||||
throw new Error(error.message || 'An error occurred');
|
||||
}
|
||||
if (response.status === 204) return;
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const lmsApi = {
|
||||
async getCatalog(): Promise<Course[]> {
|
||||
// LMS service uses /catalog for the published courses list
|
||||
const response = await fetch(`${API_BASE_URL}/catalog`);
|
||||
if (!response.ok) throw new Error('Failed to fetch catalog');
|
||||
return response.json();
|
||||
async getCatalog(orgId?: string): Promise<Course[]> {
|
||||
const query = orgId ? `?organization_id=${orgId}` : '';
|
||||
return apiFetch(`/catalog${query}`);
|
||||
},
|
||||
|
||||
async getCourseOutline(courseId: string): Promise<Course & { modules: Module[], grading_categories: GradingCategory[] }> {
|
||||
const response = await fetch(`${API_BASE_URL}/courses/${courseId}/outline`);
|
||||
if (!response.ok) throw new Error('Failed to fetch course outline');
|
||||
return response.json();
|
||||
return apiFetch(`/courses/${courseId}/outline`);
|
||||
},
|
||||
|
||||
async getLesson(id: string): Promise<Lesson> {
|
||||
return fetch(`${API_BASE_URL}/lessons/${id}`).then(res => res.json());
|
||||
return apiFetch(`/lessons/${id}`);
|
||||
},
|
||||
|
||||
async register(payload: AuthPayload): Promise<AuthResponse> {
|
||||
return fetch(`${API_BASE_URL}/auth/register`, {
|
||||
return apiFetch('/auth/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
}).then(res => res.ok ? res.json() : res.json().then(e => Promise.reject(e)));
|
||||
});
|
||||
},
|
||||
|
||||
async login(payload: AuthPayload): Promise<AuthResponse> {
|
||||
return fetch(`${API_BASE_URL}/auth/login`, {
|
||||
return apiFetch('/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
}).then(res => res.ok ? res.json() : res.json().then(e => Promise.reject(e)));
|
||||
});
|
||||
},
|
||||
|
||||
async enroll(courseId: string, userId: string): Promise<void> {
|
||||
return fetch(`${API_BASE_URL}/enroll`, {
|
||||
return apiFetch('/enroll', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ course_id: courseId, user_id: userId })
|
||||
}).then(res => res.ok ? res.json() : res.json().then(e => Promise.reject(e)));
|
||||
});
|
||||
},
|
||||
|
||||
async getEnrollments(userId: string): Promise<Enrollment[]> {
|
||||
return fetch(`${API_BASE_URL}/enrollments/${userId}`).then(res => res.json());
|
||||
return apiFetch(`/enrollments/${userId}`);
|
||||
},
|
||||
|
||||
async submitScore(userId: string, courseId: string, lessonId: string, score: number): Promise<UserGrade> {
|
||||
const response = await fetch(`${API_BASE_URL}/grades`, {
|
||||
return apiFetch('/grades', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ user_id: userId, course_id: courseId, lesson_id: lessonId, score })
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to submit score');
|
||||
return response.json();
|
||||
},
|
||||
|
||||
async getUserGrades(userId: string, courseId: string): Promise<UserGrade[]> {
|
||||
const response = await fetch(`${API_BASE_URL}/users/${userId}/courses/${courseId}/grades`);
|
||||
if (!response.ok) throw new Error('Failed to fetch user grades');
|
||||
return response.json();
|
||||
return apiFetch(`/users/${userId}/courses/${courseId}/grades`);
|
||||
},
|
||||
|
||||
async getGamification(userId: string): Promise<{ points: number, level: number, badges: { id: string, name: string, description: string, earned_at: string }[] }> {
|
||||
const response = await fetch(`${API_BASE_URL}/users/${userId}/gamification`);
|
||||
if (!response.ok) throw new Error('Failed to fetch gamification data');
|
||||
return response.json();
|
||||
return apiFetch(`/users/${userId}/gamification`);
|
||||
},
|
||||
|
||||
async getLeaderboard(): Promise<User[]> {
|
||||
const token = localStorage.getItem('token');
|
||||
const response = await fetch(`${API_BASE_URL}/analytics/leaderboard`, {
|
||||
headers: { 'Authorization': `Bearer ${token}` }
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to fetch leaderboard');
|
||||
return response.json();
|
||||
return apiFetch('/analytics/leaderboard');
|
||||
},
|
||||
|
||||
async getBranding(orgId: string): Promise<Organization> {
|
||||
const response = await fetch(`${CMS_API_URL}/organizations/${orgId}/branding`);
|
||||
if (!response.ok) throw new Error('Failed to fetch branding');
|
||||
return response.json();
|
||||
return apiFetch(`/organizations/${orgId}/branding`, {}, true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,8 +6,10 @@ import { User } from '@/lib/api';
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
token: string | null;
|
||||
selectedOrgId: string | null;
|
||||
login: (user: User, token: string) => void;
|
||||
logout: () => void;
|
||||
setOrganizationId: (id: string | null) => void;
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
@@ -16,14 +18,19 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const [selectedOrgId, setSelectedOrgId] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const savedUser = localStorage.getItem('studio_user');
|
||||
const savedToken = localStorage.getItem('studio_token');
|
||||
const savedOrgId = localStorage.getItem('studio_selected_org_id');
|
||||
|
||||
if (savedUser && savedToken) {
|
||||
setUser(JSON.parse(savedUser));
|
||||
const u = JSON.parse(savedUser);
|
||||
setUser(u);
|
||||
setToken(savedToken);
|
||||
setSelectedOrgId(savedOrgId || u.organization_id || null);
|
||||
}
|
||||
setLoading(false);
|
||||
}, []);
|
||||
@@ -31,19 +38,34 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const login = (newUser: User, newToken: string) => {
|
||||
setUser(newUser);
|
||||
setToken(newToken);
|
||||
setSelectedOrgId(newUser.organization_id || null);
|
||||
localStorage.setItem('studio_user', JSON.stringify(newUser));
|
||||
localStorage.setItem('studio_token', newToken);
|
||||
if (newUser.organization_id) {
|
||||
localStorage.setItem('studio_selected_org_id', newUser.organization_id);
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
setUser(null);
|
||||
setToken(null);
|
||||
setSelectedOrgId(null);
|
||||
localStorage.removeItem('studio_user');
|
||||
localStorage.removeItem('studio_token');
|
||||
localStorage.removeItem('studio_selected_org_id');
|
||||
};
|
||||
|
||||
const setOrganizationId = (id: string | null) => {
|
||||
setSelectedOrgId(id);
|
||||
if (id) {
|
||||
localStorage.setItem('studio_selected_org_id', id);
|
||||
} else {
|
||||
localStorage.removeItem('studio_selected_org_id');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, token, login, logout, loading }}>
|
||||
<AuthContext.Provider value={{ user, token, selectedOrgId, login, logout, setOrganizationId, loading }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
@@ -191,13 +191,16 @@ export interface CreateWebhookPayload {
|
||||
}
|
||||
|
||||
const getToken = () => typeof window !== 'undefined' ? localStorage.getItem('studio_token') : null;
|
||||
const getSelectedOrgId = () => typeof window !== 'undefined' ? localStorage.getItem('studio_selected_org_id') : null;
|
||||
|
||||
const apiFetch = (url: string, options: RequestInit = {}) => {
|
||||
const token = getToken();
|
||||
const selectedOrgId = getSelectedOrgId();
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||
...(selectedOrgId ? { 'X-Organization-Id': selectedOrgId } : {})
|
||||
};
|
||||
|
||||
return fetch(`${API_BASE_URL}${url}`, { ...options, headers }).then(async res => {
|
||||
@@ -273,8 +276,10 @@ export const cmsApi = {
|
||||
formData.append('file', file);
|
||||
|
||||
const token = getToken();
|
||||
const selectedOrgId = getSelectedOrgId();
|
||||
const headers: Record<string, string> = {
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||
...(selectedOrgId ? { 'X-Organization-Id': selectedOrgId } : {})
|
||||
};
|
||||
|
||||
// Note: We don't set 'Content-Type' for multipart/form-data.
|
||||
@@ -295,8 +300,10 @@ export const cmsApi = {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
const token = getToken();
|
||||
const selectedOrgId = getSelectedOrgId();
|
||||
const headers: Record<string, string> = {
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||
...(selectedOrgId ? { 'X-Organization-Id': selectedOrgId } : {})
|
||||
};
|
||||
return fetch(`${API_BASE_URL}/organizations/${id}/logo`, {
|
||||
method: 'POST',
|
||||
|
||||
Reference in New Issue
Block a user