113 lines
4.0 KiB
JavaScript
113 lines
4.0 KiB
JavaScript
import { apiClient } from '../shared/api-client.js';
|
|
import { weekStart, datesBetween } from './schedule-view-service.js';
|
|
import { addDays, parseIsoDate, toIsoDate } from '../shared/dates.js';
|
|
export function groupMatrixColumns(groups, departments) {
|
|
const names = new Map(
|
|
departments.map((d) => [String(d.id), d.departmentName || d.name]),
|
|
);
|
|
const columns = [...groups].sort(
|
|
(a, b) =>
|
|
(names.get(String(a.departmentId)) || '').localeCompare(
|
|
names.get(String(b.departmentId)) || '',
|
|
'ru',
|
|
) || a.name.localeCompare(b.name, 'ru', { numeric: true }),
|
|
);
|
|
const sections = [];
|
|
for (const group of columns) {
|
|
let section = sections.at(-1);
|
|
if (!section || String(section.id) !== String(group.departmentId)) {
|
|
section = {
|
|
id: group.departmentId,
|
|
name:
|
|
names.get(String(group.departmentId)) ||
|
|
'Кафедра не указана',
|
|
groups: [],
|
|
};
|
|
sections.push(section);
|
|
}
|
|
section.groups.push(group);
|
|
}
|
|
return { columns, sections };
|
|
}
|
|
export function groupMatrixDates(date) {
|
|
const start = weekStart(date);
|
|
return datesBetween(start, toIsoDate(addDays(parseIsoDate(start), 6)));
|
|
}
|
|
export function groupMatrixRows(dates, columns, slotsByDate, lessonsByGroup) {
|
|
return dates.flatMap((date) => {
|
|
const slots = new Map(
|
|
(slotsByDate.get(date) || []).map((s) => [
|
|
Number(s.orderNumber),
|
|
{
|
|
order: Number(s.orderNumber),
|
|
startTime: s.startTime,
|
|
endTime: s.endTime,
|
|
},
|
|
]),
|
|
);
|
|
for (const group of columns)
|
|
for (const lesson of lessonsByGroup.get(String(group.id)) || [])
|
|
if (lesson.date === date) {
|
|
const order = Number(
|
|
lesson.timeSlotOrder || lesson.timeSlotId,
|
|
);
|
|
if (!slots.has(order))
|
|
slots.set(order, {
|
|
order,
|
|
startTime: lesson.startTime,
|
|
endTime: lesson.endTime,
|
|
});
|
|
}
|
|
return [...slots.values()]
|
|
.sort((a, b) => a.order - b.order)
|
|
.map((slot) => ({
|
|
date,
|
|
...slot,
|
|
cells: columns.map((group) =>
|
|
(lessonsByGroup.get(String(group.id)) || []).filter(
|
|
(lesson) =>
|
|
lesson.date === date &&
|
|
Number(
|
|
lesson.timeSlotOrder || lesson.timeSlotId,
|
|
) === slot.order,
|
|
),
|
|
),
|
|
}));
|
|
});
|
|
}
|
|
export async function loadGroupMatrix(
|
|
groups,
|
|
departments,
|
|
date,
|
|
client = apiClient,
|
|
) {
|
|
if (!groups.length || groups.length > 20)
|
|
throw new Error('Выберите от 1 до 20 групп');
|
|
const dates = groupMatrixDates(date),
|
|
{ columns, sections } = groupMatrixColumns(groups, departments);
|
|
const [groupLessons, daySlots] = await Promise.all([
|
|
Promise.all(
|
|
columns.map((group) =>
|
|
client.get(
|
|
`/api/schedule?${new URLSearchParams({ groupId: group.id, startDate: dates[0], endDate: dates.at(-1) })}`,
|
|
),
|
|
),
|
|
),
|
|
Promise.all(
|
|
dates.map((day) =>
|
|
client.get(`/api/admin/time-slots/effective?date=${day}`),
|
|
),
|
|
),
|
|
]);
|
|
const slotsByDate = new Map(dates.map((day, i) => [day, daySlots[i]]));
|
|
const lessonsByGroup = new Map(
|
|
columns.map((group, i) => [String(group.id), groupLessons[i]]),
|
|
);
|
|
return {
|
|
columns,
|
|
sections,
|
|
dates,
|
|
rows: groupMatrixRows(dates, columns, slotsByDate, lessonsByGroup),
|
|
};
|
|
}
|