180 lines
6.2 KiB
JavaScript
180 lines
6.2 KiB
JavaScript
import { apiClient } from '../shared/api-client.js';
|
|
|
|
export const LOAD_CATEGORIES = Object.freeze([
|
|
{ key: 'LECTURE', label: 'Лекции' },
|
|
{ key: 'PRACTICE', label: 'Практики' },
|
|
{ key: 'LABORATORY', label: 'Лабораторные' },
|
|
]);
|
|
export function emptyPlanForm() {
|
|
return {
|
|
title: '',
|
|
specialtyProfileId: '',
|
|
educationFormId: '',
|
|
semesterNumber: 1,
|
|
};
|
|
}
|
|
export function loadDraft(subject) {
|
|
return LOAD_CATEGORIES.map(({ key }) => {
|
|
const existing = subject?.loads?.find((load) => load.category === key);
|
|
return {
|
|
category: key,
|
|
academicHours: existing?.academicHours ?? 0,
|
|
splitSubgroups: existing?.splitSubgroups ?? false,
|
|
teacherId: existing?.teacherId ?? null,
|
|
teacherName: existing?.teacherName ?? '',
|
|
};
|
|
});
|
|
}
|
|
export function workloadHours(load) {
|
|
return (
|
|
Number(load.academicHours || 0) *
|
|
(load.category === 'LABORATORY' && load.splitSubgroups ? 2 : 1)
|
|
);
|
|
}
|
|
export function subjectPayload(loads, expectedRevision) {
|
|
const payload = loads.map((load) => ({
|
|
category: load.category,
|
|
academicHours: Number(load.academicHours),
|
|
splitSubgroups:
|
|
load.category === 'LABORATORY' && Boolean(load.splitSubgroups),
|
|
teacherId: load.teacherId ? Number(load.teacherId) : null,
|
|
}));
|
|
if (
|
|
payload.some(
|
|
(load) =>
|
|
!Number.isInteger(load.academicHours) ||
|
|
load.academicHours < 0 ||
|
|
load.academicHours > 100000 ||
|
|
load.academicHours % 2,
|
|
)
|
|
) {
|
|
throw new Error(
|
|
'Академические часы должны быть чётными, от 0 до 100000',
|
|
);
|
|
}
|
|
if (!payload.some((load) => load.academicHours > 0))
|
|
throw new Error('Укажите часы хотя бы одного вида нагрузки');
|
|
if (
|
|
payload.some(
|
|
(load) =>
|
|
!load.academicHours && (load.teacherId || load.splitSubgroups),
|
|
)
|
|
) {
|
|
throw new Error(
|
|
'Для нулевой нагрузки снимите деление на подгруппы и назначение преподавателя',
|
|
);
|
|
}
|
|
return { expectedRevision, loads: payload };
|
|
}
|
|
export function compatibleGroups(metadata, plan, semesterId) {
|
|
const semester = metadata?.semesters.find(
|
|
(item) => String(item.id) === String(semesterId),
|
|
);
|
|
if (!semester || !plan) return [];
|
|
return metadata.groups.filter(
|
|
(group) =>
|
|
group.specialtyProfileId === plan.specialtyProfileId &&
|
|
group.educationFormId === plan.educationFormId &&
|
|
(semester.startYear - group.yearStartStudy) * 2 +
|
|
(semester.semesterType === 'autumn' ? 1 : 2) ===
|
|
plan.semesterNumber,
|
|
);
|
|
}
|
|
export const fetchPlans = () =>
|
|
apiClient.get('/api/study-plans', {
|
|
errorMessage: 'Не удалось загрузить учебные планы',
|
|
});
|
|
export const fetchPlanMetadata = () =>
|
|
apiClient.get('/api/study-plans/metadata', {
|
|
errorMessage: 'Не удалось загрузить справочники учебных планов',
|
|
});
|
|
export const fetchPlan = (id) =>
|
|
apiClient.get(`/api/study-plans/${id}`, {
|
|
errorMessage: 'Не удалось загрузить учебный план',
|
|
});
|
|
export const savePlan = (id, form, revision) => {
|
|
const payload = {
|
|
title: form.title.trim(),
|
|
specialtyProfileId: Number(form.specialtyProfileId),
|
|
educationFormId: Number(form.educationFormId),
|
|
semesterNumber: Number(form.semesterNumber),
|
|
expectedRevision: revision,
|
|
};
|
|
return id
|
|
? apiClient.put(`/api/study-plans/${id}`, payload)
|
|
: apiClient.post('/api/study-plans', payload);
|
|
};
|
|
export const savePlanSubject = (id, subjectId, loads, revision) =>
|
|
apiClient.put(
|
|
`/api/study-plans/${id}/subjects/${subjectId}`,
|
|
subjectPayload(loads, revision),
|
|
);
|
|
export const removePlanSubject = (id, subjectId, revision) =>
|
|
apiClient.delete(
|
|
`/api/study-plans/${id}/subjects/${subjectId}?expectedRevision=${revision}`,
|
|
);
|
|
export const assignPlan = (id, groupId, semesterId) =>
|
|
apiClient.post(`/api/study-plans/${id}/assignments`, {
|
|
groupId: Number(groupId),
|
|
semesterId: Number(semesterId),
|
|
});
|
|
export const unassignPlan = (id, groupId, semesterId) =>
|
|
apiClient.delete(
|
|
`/api/study-plans/${id}/assignments?groupId=${groupId}&semesterId=${semesterId}`,
|
|
);
|
|
export const removePlan = (id, revision) =>
|
|
apiClient.delete(`/api/study-plans/${id}?expectedRevision=${revision}`);
|
|
export function planTeacherEndpoint(
|
|
subjectId,
|
|
query = '',
|
|
groupIds = [],
|
|
semesterId,
|
|
lessonTypeId,
|
|
) {
|
|
const params = new URLSearchParams({ subjectId, query });
|
|
groupIds.forEach((id) => params.append('groupId', id));
|
|
if (semesterId) params.set('semesterId', semesterId);
|
|
if (lessonTypeId) params.set('lessonTypeId', lessonTypeId);
|
|
return `/api/study-plans/teacher-options?${params}`;
|
|
}
|
|
export const fetchPlanTeachers = (
|
|
subjectId,
|
|
query,
|
|
groupIds,
|
|
semesterId,
|
|
lessonTypeId,
|
|
) =>
|
|
apiClient.get(
|
|
planTeacherEndpoint(
|
|
subjectId,
|
|
query,
|
|
groupIds,
|
|
semesterId,
|
|
lessonTypeId,
|
|
),
|
|
{
|
|
errorMessage:
|
|
'Не удалось загрузить преподавателей кафедры дисциплины',
|
|
},
|
|
);
|
|
|
|
// Состояние конструктора хранит часы внутри объектов видов занятий.
|
|
export function plannedTypeLimits(loads, current) {
|
|
return Object.fromEntries(
|
|
[
|
|
['lecture', 'LECTURE'],
|
|
['practice', 'PRACTICE'],
|
|
['laboratory', 'LABORATORY'],
|
|
].map(([key, category]) => [
|
|
key,
|
|
{
|
|
...current[key],
|
|
hours: String(
|
|
loads.find((load) => load.category === category)
|
|
?.academicHours || 0,
|
|
),
|
|
},
|
|
]),
|
|
);
|
|
}
|