uchill/front_material/api/board.ts

54 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* API для работы с интерактивными досками
*/
import apiClient from '@/lib/api-client';
import { getLesson } from './schedule';
export interface Board {
board_id: string;
title: string;
description?: string;
access_type?: string;
mentor?: number;
student?: number;
is_active?: boolean;
created_at?: string;
updated_at?: string;
}
/**
* Одна доска на пару ментор–студент (сохраняется между уроками).
* Атомарно получить существующую или создать новую.
*/
export async function getOrCreateMentorStudentBoard(mentorId: number, studentId: number): Promise<Board> {
const res = await apiClient.get<Board>(
`/board/boards/get-or-create-mentor-student/?mentor=${mentorId}&student=${studentId}`
);
return res.data;
}
/**
* Получить или создать доску для занятия.
* Доска привязана к паре ментор–студент, а не к уроку: одна и та же доска для всех уроков этой пары.
*/
export async function getOrCreateLessonBoard(lessonId: number): Promise<Board> {
const lesson = await getLesson(String(lessonId));
const mentorId =
typeof lesson.mentor === 'object' && lesson.mentor ? Number(lesson.mentor.id) : Number(lesson.mentor);
let studentId = 0;
const c = lesson.client;
if (c && typeof c === 'object') {
if (c.user && typeof c.user === 'object') {
studentId = Number(c.user.id);
} else if ('user' in c && c.user) {
const user = c.user as any;
studentId = typeof user === 'object' ? Number(user.id) : Number(user);
} else {
studentId = Number((c as any).id ?? c);
}
}
if (!mentorId || !studentId) throw new Error('Не удалось получить mentor и student из урока');
return getOrCreateMentorStudentBoard(mentorId, studentId);
}