From 68dfb72384d39bc5deb13c30234a53886f3e91d0 Mon Sep 17 00:00:00 2001
From: Abhijit
Date: Mon, 23 Nov 2026 16:54:59 +0530
Subject: [PATCH] fixed wrong session creates and cohorts name add
---
backend/src/routes/cohorts.js | 4 +-
backend/src/services/programSessions.js | 29 ++-----
backend/src/services/schedule.js | 38 ++++++---
frontend/src/views/virtualpr/cohorts/form.jsx | 45 +++++++---
.../src/views/virtualpr/cohorts/prt-form.jsx | 83 +++++++++++++------
5 files changed, 124 insertions(+), 75 deletions(-)
diff --git a/backend/src/routes/cohorts.js b/backend/src/routes/cohorts.js
index 5958c9c..7cba022 100644
--- a/backend/src/routes/cohorts.js
+++ b/backend/src/routes/cohorts.js
@@ -207,7 +207,7 @@ export function cohortRoutes(server) {
} catch (err) {
throw Boom.badRequest(err.message);
}
- const name = buildCohortName({ slot, language, startDate });
+ const name = String(request.payload.name || '').trim() || buildCohortName({ slot, language, startDate });
let createdIds = [];
const cohort = await Cohort.create({
name,
@@ -328,7 +328,7 @@ export function cohortRoutes(server) {
} catch (err) {
throw Boom.badRequest(err.message);
}
- cohort.name = buildCohortName({ slot, language, startDate });
+ cohort.name = String(request.payload.name || '').trim() || buildCohortName({ slot, language, startDate });
cohort.startDate = ymdToDate(startDate);
cohort.slot = slot;
cohort.language = language;
diff --git a/backend/src/services/programSessions.js b/backend/src/services/programSessions.js
index 55cc263..9300d41 100644
--- a/backend/src/services/programSessions.js
+++ b/backend/src/services/programSessions.js
@@ -16,6 +16,7 @@ import {
toYmd,
todayYmd,
upcomingSessionDates,
+ intervalWeekdays,
scheduleSessionCount,
scheduleWeekdays,
weekdayOf,
@@ -296,15 +297,16 @@ export async function ensureSessionRecords(cohort) {
const ALL_WEEKDAYS = [1, 2, 3, 4, 5, 6];
-// Days this cohort meets. An extra schedule wins. All weekdays is Monday–Saturday. Otherwise the repeat interval.
+// Days this cohort meets. Extra schedule wins; all-weekdays is Mon–Sat; Normal uses fixed interval days from start.
export async function meetingWeekdays(cohort) {
const scheduled = await scheduleWeekdays(cohort?.scheduleId);
if (scheduled) return scheduled;
if (cohort?.sixDayWeek) return ALL_WEEKDAYS;
- return null;
+ const settings = await getAppSettings();
+ return intervalWeekdays(toYmd(cohort?.startDate), settings.repeatIntervalDays);
}
-// Next date for an extra schedule, skipping unticked days, Sunday, holiday, and leave.
+// Next meeting day on the cohort's weekday set, skipping Sunday, holiday, and leave.
async function nextWeekdayAppendDate(cohort, weekdays) {
const settings = await getAppSettings();
const allowed = new Set(weekdays.map(Number));
@@ -326,27 +328,10 @@ async function nextWeekdayAppendDate(cohort, weekdays) {
return cursor;
}
-// Next date after the last session. A schedule or all-weekdays uses those days. Otherwise the repeat interval.
+// Next date after the last session on this cohort's meeting days.
export async function nextAppendDate(cohort) {
const weekdays = await meetingWeekdays(cohort);
- if (weekdays) return nextWeekdayAppendDate(cohort, weekdays);
- const settings = await getAppSettings();
- const interval = Math.max(1, Number(settings.repeatIntervalDays) || 2);
- const last = await CohortSession.findOne({ cohort: cohort._id }).sort({ date: -1, index: -1 });
- let cursor = last?.date ? addDays(last.date, interval) : toYmd(cohort.startDate);
- const horizon = addDays(cursor, interval * 80);
- const holidays = settings.skipHolidays !== false
- ? await holidayDatesBetween(cursor, horizon, cohort.language)
- : new Set();
- const prtId = cohort.virtualPrt?._id || cohort.virtualPrt;
- for (let guard = 0; guard < 400; guard += 1) {
- const sundayBlocked = weekdayOf(cursor) === 0;
- const holidayBlocked = settings.skipHolidays !== false && holidays.has(cursor);
- const leaveBlocked = settings.skipPrtLeave !== false && await leaveCoversDate(prtId, cursor);
- if (!sundayBlocked && !holidayBlocked && !leaveBlocked) return cursor;
- cursor = addDays(cursor, interval);
- }
- return cursor;
+ return nextWeekdayAppendDate(cohort, weekdays.length ? weekdays : ALL_WEEKDAYS);
}
// True when the cohort may meet on this date: its meeting days if set, never Sunday, and not a holiday or PRT leave.
diff --git a/backend/src/services/schedule.js b/backend/src/services/schedule.js
index 088696a..b2a7d50 100644
--- a/backend/src/services/schedule.js
+++ b/backend/src/services/schedule.js
@@ -148,16 +148,28 @@ export function slotDateTimes(date, slot, durationMinutes = 60) {
return { startTime, endTime, start, end };
}
-// True when the date is on the Normal interval and is not Sunday, a holiday, or leave.
+// Fixed Mon–Sat meeting days for Normal interval. Start Tue + interval 2 → Tue/Thu/Sat every week.
+export function intervalWeekdays(startYmd, interval = 2) {
+ const startDow = weekdayOf(startYmd);
+ if (!startYmd || startDow === 0) return [];
+ const step = Math.max(1, Number(interval) || 2);
+ const days = [];
+ let day = startDow;
+ for (let guard = 0; guard < 6; guard += 1) {
+ days.push(day);
+ day = ((day - 1 + step) % 6) + 1; // step on Mon–Sat only, never Sunday
+ if (day === startDow) break;
+ }
+ return days.sort((a, b) => a - b);
+}
+
+// True when the date is on the Normal weekday set and is not Sunday, a holiday, or leave.
export async function isCohortSessionOn(startYmd, virtualPrtId, targetYmd, language) {
if (!startYmd || !targetYmd || targetYmd < startYmd) return false;
if (weekdayOf(targetYmd) === 0) return false;
const settings = await getAppSettings();
- const interval = Math.max(1, Number(settings.repeatIntervalDays) || 2);
- const start = parseYmd(startYmd);
- const target = parseYmd(targetYmd);
- const diff = Math.round((target - start) / 86400000);
- if (diff < 0 || diff % interval !== 0) return false;
+ const allowed = intervalWeekdays(startYmd, settings.repeatIntervalDays);
+ if (!allowed.includes(weekdayOf(targetYmd))) return false;
if (settings.skipHolidays !== false) {
const holidays = await holidayDatesBetween(targetYmd, targetYmd, language);
if (holidays.has(targetYmd)) return false;
@@ -168,24 +180,26 @@ export async function isCohortSessionOn(startYmd, virtualPrtId, targetYmd, langu
const RRULE_DAYS = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
-// Builds meeting dates. Normal uses the interval. An extra schedule uses its weekdays. Sunday, holiday, and leave are skipped.
+// Builds meeting dates from weekdays (or Normal interval days from start). Sunday, holiday, and leave are skipped.
export async function upcomingSessionDates(startYmd, virtualPrtId, count, language, weekdays = null) {
const settings = await getAppSettings();
const needed = Math.min(Math.max(Number(count) || settings.upcomingSessionCount || 30, 1), 60);
- const allowed = Array.isArray(weekdays) && weekdays.length ? new Set(weekdays.map(Number)) : null;
- const interval = allowed ? 1 : Math.max(1, Number(settings.repeatIntervalDays) || 2);
- const horizon = addDays(startYmd, needed * interval * 8);
+ const days = Array.isArray(weekdays) && weekdays.length
+ ? weekdays.map(Number)
+ : intervalWeekdays(startYmd, settings.repeatIntervalDays);
+ const allowed = new Set(days.filter((day) => day >= 1 && day <= 6));
+ const horizon = addDays(startYmd, needed * 8);
const holidays = settings.skipHolidays !== false ? await holidayDatesBetween(startYmd, horizon, language) : new Set();
const dates = [];
let cursor = startYmd;
let guard = 0;
while (dates.length < needed && guard < 400) {
const sundayBlocked = weekdayOf(cursor) === 0;
- const weekdayBlocked = allowed ? !allowed.has(weekdayOf(cursor)) : false;
+ const weekdayBlocked = allowed.size ? !allowed.has(weekdayOf(cursor)) : false;
const holidayBlocked = settings.skipHolidays !== false && holidays.has(cursor);
const leaveBlocked = settings.skipPrtLeave !== false && await leaveCoversDate(virtualPrtId, cursor);
if (!weekdayBlocked && !sundayBlocked && !holidayBlocked && !leaveBlocked) dates.push(cursor);
- cursor = addDays(cursor, interval);
+ cursor = addDays(cursor, 1);
guard += 1;
}
return dates;
diff --git a/frontend/src/views/virtualpr/cohorts/form.jsx b/frontend/src/views/virtualpr/cohorts/form.jsx
index 685c9fb..05b08e7 100644
--- a/frontend/src/views/virtualpr/cohorts/form.jsx
+++ b/frontend/src/views/virtualpr/cohorts/form.jsx
@@ -57,6 +57,7 @@ export default function CohortForm() {
const [saving, setSaving] = useState(false);
const [statusValue, setStatusValue] = useState(null);
const [statusWarnings, setStatusWarnings] = useState([]);
+ const [nameCustom, setNameCustom] = useState(false); // true after the user edits the auto name
const { settings } = useSettings();
const startWeekdays = settings?.cohortStartWeekdays || [1, 2];
const schedules = settings?.cohortSchedules || [];
@@ -83,9 +84,11 @@ export default function CohortForm() {
let active = true;
api(`/api/cohorts/${id}`).then((c) => {
if (!active) return;
+ const startDate = c.startDate ? String(c.startDate).slice(0, 10) : '';
+ const autoName = buildCohortName({ slot: c.slot || '', language: c.language || '', startDate });
setForm({
name: c.name || '',
- startDate: c.startDate ? String(c.startDate).slice(0, 10) : '',
+ startDate,
slot: c.slot || '',
virtualPrt: c.virtualPrt?.id || '',
language: c.language || '',
@@ -93,6 +96,7 @@ export default function CohortForm() {
scheduleId: c.scheduleId || '',
patients: (c.patients || []).map((p) => p.id),
});
+ setNameCustom(Boolean(c.name && c.name !== autoName));
setSelectedDetails((c.patients || []).map((p) => ({ id: p.id, name: p.name, mobile: p.mobile, whatsappOptOut: p.whatsappOptOut })));
setStatusValue(cohortStatusValue(c));
// setSessionSummary({ completed: c.sessionsCompleted || 0, planned: c.sessionsPlanned || 0 });
@@ -124,12 +128,12 @@ export default function CohortForm() {
language,
startDate: form.startDate,
});
- setForm((current) => (
- current.name === name && current.language === language
- ? current
- : { ...current, name, language }
- ));
- }, [form.slot, form.startDate, form.virtualPrt, form.language, prts]);
+ setForm((current) => {
+ const next = { ...current, language };
+ if (!nameCustom) next.name = name;
+ return current.name === next.name && current.language === next.language ? current : next;
+ });
+ }, [form.slot, form.startDate, form.virtualPrt, form.language, prts, nameCustom]);
const visiblePatients = useMemo(() => {
const seen = new Set();
@@ -218,10 +222,25 @@ export default function CohortForm() {