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
|
||||
};
|
||||
|
||||
@@ -6,6 +6,9 @@ const {
|
||||
formatCourseTime,
|
||||
formatClassDays,
|
||||
formatClassStartDate,
|
||||
formatClassDaysFromIndexes,
|
||||
normalizeWeekdays,
|
||||
normalizeClockTime,
|
||||
buildClassScheduleContext
|
||||
} = require('./classSchedule');
|
||||
|
||||
@@ -56,4 +59,40 @@ describe('class schedule SMS helpers', () => {
|
||||
|
||||
assert.equal(context.courseTime, 'از 10 تا 12');
|
||||
});
|
||||
|
||||
it('prefers stored class days and times over inferred sessions', () => {
|
||||
const atNoon = (year, month, day) => new Date(year, month - 1, day, 12, 0, 0);
|
||||
const context = buildClassScheduleContext(
|
||||
{
|
||||
startDate: atNoon(2026, 8, 15),
|
||||
days: [6, 1],
|
||||
startTime: '18:00',
|
||||
endTime: '20:00'
|
||||
},
|
||||
[
|
||||
{ day: atNoon(2026, 8, 16), startTime: '19:00', endTime: '21:00' }
|
||||
]
|
||||
);
|
||||
|
||||
assert.equal(context.classDays, 'شنبه و دوشنبه');
|
||||
assert.equal(context.courseTime, 'از 18 تا 20');
|
||||
});
|
||||
});
|
||||
|
||||
describe('class weekday and time normalization', () => {
|
||||
it('deduplicates weekdays and sorts them in Iran week order', () => {
|
||||
assert.deepEqual(normalizeWeekdays([1, 6, 1, 3, 9, '2']), [6, 1, 2, 3]);
|
||||
});
|
||||
|
||||
it('normalizes clock times to HH:mm', () => {
|
||||
assert.equal(normalizeClockTime('19'), '19:00');
|
||||
assert.equal(normalizeClockTime('9:5'), '09:05');
|
||||
assert.equal(normalizeClockTime('19:00'), '19:00');
|
||||
assert.equal(normalizeClockTime('25:00'), '');
|
||||
assert.equal(normalizeClockTime(''), '');
|
||||
});
|
||||
|
||||
it('formats stored weekday indexes as Persian day names', () => {
|
||||
assert.equal(formatClassDaysFromIndexes([1, 6]), 'شنبه و دوشنبه');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user