feat(classes): store weekdays and meeting times on the class
Keep the class schedule on the class itself so SMS and listings do not have to infer days and hours from sessions.
This commit is contained in:
+41
-4
@@ -39,6 +39,37 @@ const joinFaList = (items) => {
|
||||
return `${items.slice(0, -1).join('، ')} و ${items[items.length - 1]}`;
|
||||
};
|
||||
|
||||
const normalizeWeekdays = (days) => {
|
||||
if (!Array.isArray(days)) return [];
|
||||
const unique = [];
|
||||
const seen = new Set();
|
||||
for (const value of days) {
|
||||
const index = Number(value);
|
||||
if (!Number.isInteger(index) || index < 0 || index > 6 || seen.has(index)) continue;
|
||||
seen.add(index);
|
||||
unique.push(index);
|
||||
}
|
||||
unique.sort((a, b) => IRAN_WEEK_ORDER.indexOf(a) - IRAN_WEEK_ORDER.indexOf(b));
|
||||
return unique;
|
||||
};
|
||||
|
||||
const normalizeClockTime = (value) => {
|
||||
if (value == null) return '';
|
||||
const raw = String(value).trim();
|
||||
if (!raw) return '';
|
||||
const match = raw.match(/^(\d{1,2})(?::(\d{1,2}))?$/);
|
||||
if (!match) return '';
|
||||
const hours = Number(match[1]);
|
||||
const minutes = Number(match[2] || 0);
|
||||
if (hours > 23 || minutes > 59) return '';
|
||||
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
const formatClassDaysFromIndexes = (days = []) => {
|
||||
const indexes = normalizeWeekdays(days);
|
||||
return joinFaList(indexes.map((index) => PERSIAN_WEEKDAYS[index]).filter(Boolean));
|
||||
};
|
||||
|
||||
const formatClassDays = (sessions = []) => {
|
||||
const indexes = [];
|
||||
const seen = new Set();
|
||||
@@ -48,8 +79,7 @@ const formatClassDays = (sessions = []) => {
|
||||
seen.add(dayIndex);
|
||||
indexes.push(dayIndex);
|
||||
}
|
||||
indexes.sort((a, b) => IRAN_WEEK_ORDER.indexOf(a) - IRAN_WEEK_ORDER.indexOf(b));
|
||||
return joinFaList(indexes.map((index) => PERSIAN_WEEKDAYS[index]).filter(Boolean));
|
||||
return formatClassDaysFromIndexes(indexes);
|
||||
};
|
||||
|
||||
const formatClassStartDate = (value) => {
|
||||
@@ -69,17 +99,24 @@ const buildClassScheduleContext = (classDoc, sessions = [], currentSession = nul
|
||||
return left - right;
|
||||
});
|
||||
const sessionForTime = currentSession || sortedSessions[0] || null;
|
||||
const storedDays = formatClassDaysFromIndexes(classDoc?.days);
|
||||
const storedTime = formatCourseTime(classDoc?.startTime, classDoc?.endTime);
|
||||
|
||||
return {
|
||||
classStartDate: formatClassStartDate(classDoc?.startDate || sortedSessions[0]?.day),
|
||||
classDays: formatClassDays(sortedSessions),
|
||||
courseTime: formatCourseTime(sessionForTime?.startTime, sessionForTime?.endTime)
|
||||
classDays: storedDays || formatClassDays(sortedSessions),
|
||||
courseTime: storedTime || formatCourseTime(sessionForTime?.startTime, sessionForTime?.endTime)
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
IRAN_WEEK_ORDER,
|
||||
PERSIAN_WEEKDAYS,
|
||||
formatCourseTime,
|
||||
formatClassDays,
|
||||
formatClassDaysFromIndexes,
|
||||
formatClassStartDate,
|
||||
normalizeWeekdays,
|
||||
normalizeClockTime,
|
||||
buildClassScheduleContext
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user