refactor: centralize week calculation logic and standardize schedule boundary date normalization across client and server

This commit is contained in:
2026-07-28 18:12:10 -03:00
parent ece3879c8d
commit f39ba44f59
5 changed files with 113 additions and 42 deletions
@@ -25,11 +25,17 @@ import { joinStrings } from "../../helpers/String";
import getAvatar from "../../helpers/getAvatar";
import { NotificationsManager } from "../Notifications/Notifications";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import "dayjs/locale/es";
import { NotificationType } from "../../Models/SystemNotifications/SystemNotification.Interface";
import { validatePermissionsByCompany, validateSessionUser } from "../../helpers/check";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.locale("es");
const SCHEDULE_BOUNDARY_TIMEZONE = "America/Argentina/Buenos_Aires";
const NEAR_ENDING_THRESHOLD_DAYS = 14;
const ALLOWED_RESERVATION_PERIOD_DURATIONS = [
{ unit: "week", value: 1 },
@@ -87,8 +93,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const schedulesEnabledCheck = await this.schedulesEnabled.findOne({
employeeId: data.employeeId,
companyId: data.companyId,
startDate: dayjs(data.startDate).startOf("day").toDate(),
endDate: dayjs(data.endDate).endOf("day").toDate(),
startDate: this.normalizeScheduleStartDate(data.startDate),
endDate: this.normalizeScheduleEndDate(data.endDate),
});
if (schedulesEnabledCheck) {
@@ -98,8 +104,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const newEmplyeesEnabled = await this.schedulesEnabled.create({
employeeId: data.employeeId,
companyId: data.companyId,
startDate: dayjs(data.startDate).startOf("day").toDate(),
endDate: dayjs(data.endDate).endOf("day").toDate(),
startDate: this.normalizeScheduleStartDate(data.startDate),
endDate: this.normalizeScheduleEndDate(data.endDate),
sessionUser: data.sessionUser,
});
@@ -139,7 +145,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const employeeCheck = await EmployeesList.employees.findOne({
companyId: data.companyId,
_id: employeeId,
});
removed: { $ne: true },
} as any);
if (!employeeCheck) {
continue;
@@ -148,8 +155,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const schedulesEnabledCheck = await this.schedulesEnabled.findOne({
employeeId: employeeId,
companyId: data.companyId,
startDate: dayjs(data.startDate).startOf("day").toDate(),
endDate: dayjs(data.endDate).endOf("day").toDate(),
startDate: this.normalizeScheduleStartDate(data.startDate),
endDate: this.normalizeScheduleEndDate(data.endDate),
});
if (schedulesEnabledCheck) {
@@ -159,8 +166,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
await this.schedulesEnabled.create({
employeeId: employeeId,
companyId: data.companyId,
startDate: dayjs(data.startDate).startOf("day").toDate(),
endDate: dayjs(data.endDate).endOf("day").toDate(),
startDate: this.normalizeScheduleStartDate(data.startDate),
endDate: this.normalizeScheduleEndDate(data.endDate),
sessionUser: data.sessionUser,
});
@@ -199,7 +206,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const employeeCheck = await EmployeesList.employees.findOne({
companyId: data.companyId,
_id: employeeId,
});
removed: { $ne: true },
} as any);
if (!employeeCheck) {
continue;
@@ -208,8 +216,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const schedulesEnabledCheck = await this.schedulesEnabled.findOne({
employeeId: employeeId,
companyId: data.companyId,
startDate: dayjs(data.startDate).startOf("day").toDate(),
endDate: dayjs(data.endDate).endOf("day").toDate(),
startDate: this.normalizeScheduleStartDate(data.startDate),
endDate: this.normalizeScheduleEndDate(data.endDate),
});
if (schedulesEnabledCheck) {
@@ -243,7 +251,12 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
}
public async find(data: FindSchedulesEnabledParams): Promise<SchedulesEnabledView[]> {
let employeeFilter: any = { companyId: data.companyId };
const rangeStart = this.normalizeScheduleStartDate(data.startDate);
const rangeEnd = this.normalizeScheduleEndDate(data.endDate);
let employeeFilter: any = {
companyId: data.companyId,
removed: { $ne: true },
};
if (data.employeeId) {
employeeFilter = {
...employeeFilter,
@@ -258,8 +271,8 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const schedulesEnabled = await this.schedulesEnabled.schedulesEnabledList.findOne({
employeeId: employee.id,
companyId: data.companyId,
startDate: dayjs(data.startDate).startOf("day").toDate(),
endDate: dayjs(data.endDate).endOf("day").toDate(),
startDate: { $lte: rangeStart },
endDate: { $gte: rangeEnd },
});
const employUser = await UsersManager.users.findOne({
@@ -360,32 +373,32 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
const today = dayjs().startOf("day");
const summary = await this.buildReservationPeriodsSummary(data.companyId);
const operations = targetEmployees.map(employee => {
const operations = targetEmployees.flatMap(employee => {
const employeeSummary = summary.employees.find(item => String(item.employeeId) === String(employee.id));
const startDate = employeeSummary?.currentOpenUntil && dayjs(employeeSummary.currentOpenUntil).isAfter(today)
? dayjs(employeeSummary.currentOpenUntil).add(1, "day").startOf("day")
: today;
const endDate = startDate.add(Number(duration.value), duration.unit).subtract(1, "day").endOf("day");
const startDate = employeeSummary?.currentOpenUntil && !dayjs(employeeSummary.currentOpenUntil).endOf("day").isBefore(today)
? this.getNextWeekStartAfter(dayjs(employeeSummary.currentOpenUntil))
: this.getWeekStart(today);
const weekRanges = this.buildReservationPeriodWeekRanges(startDate, duration);
return {
return weekRanges.map(range => ({
updateOne: {
filter: {
employeeId: employee.id,
companyId: data.companyId,
startDate: startDate.toDate(),
endDate: endDate.toDate(),
startDate: this.normalizeScheduleStartDate(range.startDate),
endDate: this.normalizeScheduleEndDate(range.endDate),
},
update: {
$setOnInsert: {
employeeId: employee.id,
companyId: data.companyId,
startDate: startDate.toDate(),
endDate: endDate.toDate(),
startDate: this.normalizeScheduleStartDate(range.startDate),
endDate: this.normalizeScheduleEndDate(range.endDate),
},
},
upsert: true,
},
};
}));
});
await this.schedulesEnabled.schedulesEnabledList.bulkWrite(operations, { ordered: true });
@@ -393,6 +406,58 @@ class SchedulesEnabledManager implements ISchedulesEnabledManager {
return this.buildReservationPeriodsSummary(data.companyId);
}
private getWeekStart(date: dayjs.Dayjs): dayjs.Dayjs {
return date.startOf("day").subtract((date.day() + 6) % 7, "day");
}
private getNextWeekStartAfter(date: dayjs.Dayjs): dayjs.Dayjs {
const nextDay = date.add(1, "day").startOf("day");
const weekStart = this.getWeekStart(nextDay);
return weekStart.isBefore(nextDay) ? weekStart.add(1, "week") : weekStart;
}
private buildReservationPeriodWeekRanges(
startWeek: dayjs.Dayjs,
duration: { unit: "week" | "month"; value: number }
): { startDate: dayjs.Dayjs; endDate: dayjs.Dayjs }[] {
const ranges: { startDate: dayjs.Dayjs; endDate: dayjs.Dayjs }[] = [];
const targetEnd = duration.unit === "week"
? startWeek.add(Number(duration.value), "week").subtract(1, "day").endOf("day")
: startWeek.add(Number(duration.value), "month").subtract(1, "day").endOf("day");
let currentWeekStart = startWeek.startOf("day");
while (!currentWeekStart.isAfter(targetEnd)) {
ranges.push({
startDate: currentWeekStart,
endDate: currentWeekStart.add(6, "day").endOf("day"),
});
currentWeekStart = currentWeekStart.add(1, "week");
}
return ranges;
}
private normalizeScheduleStartDate(date: string | Date | dayjs.Dayjs): Date {
return dayjs.tz(this.getScheduleDateOnly(date), SCHEDULE_BOUNDARY_TIMEZONE).startOf("day").toDate();
}
private normalizeScheduleEndDate(date: string | Date | dayjs.Dayjs): Date {
return dayjs.tz(this.getScheduleDateOnly(date), SCHEDULE_BOUNDARY_TIMEZONE).endOf("day").toDate();
}
private getScheduleDateOnly(date: string | Date | dayjs.Dayjs): string {
if (typeof date === "string") {
return date.slice(0, 10);
}
if (dayjs.isDayjs(date)) {
return date.format("YYYY-MM-DD");
}
return date.toISOString().slice(0, 10);
}
private async buildReservationPeriodsSummary(companyId: string): Promise<ReservationPeriodsSummary> {
const today = dayjs().startOf("day");
const employees = await EmployeesList.findByCompanyId({ companyId });