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
@@ -12,14 +12,12 @@ import { disableSchedule, enableSchedule, loadSchedulesEnabled } from "../Org.Se
import WeekInput from "@core/app/components/WeekInput/WeekInput";
import dayjs from "dayjs";
import "dayjs/locale/es";
import weekday from "dayjs/plugin/weekday";
import "dayjs/locale/es";
import { SchedulesEnabledView } from "@models/SchedulesEnabled.type";
import Avatar from "@components/Avatar/Avatar";
import Button from "@core/app/components/Button/Button";
import { useConfirmStore } from "@core/Store/Confirm.Store";
import { getMondayWeekStart } from "@core/helpers/week";
dayjs.extend(weekday);
dayjs.locale("es");
type UrlData = {
@@ -34,7 +32,7 @@ export default function Schedules() {
const SessionInfo = useSessionStore();
const alert = useAlert();
const confirm = useConfirmStore();
const [startDate, setStartDate] = useState<dayjs.Dayjs>(dayjs().weekday(0));
const [startDate, setStartDate] = useState<dayjs.Dayjs>(getMondayWeekStart());
const [collaborators, setCollaborators] = useState<SchedulesEnabledView[]>([]);
useEffect(() => {
@@ -12,9 +12,8 @@ import ReactAvatar from "react-avatar";
import API from "@services/Api.Service";
import dayjs from "dayjs";
import "dayjs/locale/es";
import weekday from "dayjs/plugin/weekday";
import { getMondayWeekStart, getSundayWeekEnd } from "@core/helpers/week";
dayjs.extend(weekday);
dayjs.locale("es");
import categories from "@models/Categories.type";
@@ -2002,8 +2001,9 @@ export default function StepEngine({ flowId, action, initialCompanyId, initialEm
try {
// Hacemos un loop de 8 semanas, habilitando bloques exactos de lunes a domingo
for (let i = 0; i < 8; i++) {
const weekStart = dayjs().weekday(i * 7).format("YYYY-MM-DD");
const weekEnd = dayjs().weekday(i * 7 + 6).format("YYYY-MM-DD");
const weekStartDate = getMondayWeekStart().add(i, "week");
const weekStart = weekStartDate.format("YYYY-MM-DD");
const weekEnd = getSundayWeekEnd(weekStartDate).format("YYYY-MM-DD");
await API.post<any>("schedules-enabled/enable", {
companyId: createdCompanyId,
@@ -5,10 +5,9 @@ import ArrowForwardIcon from "@mui/icons-material/ArrowForwardIos";
import ArrowBackwardIcon from "@mui/icons-material/ArrowBackIos";
import HouseIcon from "@mui/icons-material/House";
import dayjs from "dayjs";
import weekday from "dayjs/plugin/weekday";
import "dayjs/locale/es";
import { getMondayWeekStart, getSundayWeekEnd } from "@core/helpers/week";
dayjs.extend(weekday);
dayjs.locale("es");
export interface WeekInputProps {
@@ -25,15 +24,15 @@ const WeekInput = (props: WeekInputProps) => {
const { setStartDate } = props;
const [fechas, setFechas] = useState<WeekInputData>({
currentDate: dayjs().clone(),
lunes: dayjs().clone().weekday(0),
domingo: dayjs().clone().weekday(6),
lunes: getMondayWeekStart(),
domingo: getSundayWeekEnd(),
});
const updateFechas = (fecha: dayjs.Dayjs) => {
setFechas({
currentDate: dayjs().clone(),
lunes: fecha.clone().weekday(0),
domingo: fecha.clone().weekday(6),
lunes: getMondayWeekStart(fecha),
domingo: getSundayWeekEnd(fecha),
});
//setStartDate(fechas.lunes.clone());
};
@@ -53,8 +52,8 @@ const WeekInput = (props: WeekInputProps) => {
const fecha = dayjs().clone();
setFechas({
currentDate: fecha,
lunes: fecha.clone().weekday(0),
domingo: fecha.clone().weekday(6),
lunes: getMondayWeekStart(fecha),
domingo: getSundayWeekEnd(fecha),
});
};
+9
View File
@@ -0,0 +1,9 @@
import dayjs from "dayjs";
export const getMondayWeekStart = (date: dayjs.Dayjs = dayjs()): dayjs.Dayjs => {
return date.startOf("day").subtract((date.day() + 6) % 7, "day");
};
export const getSundayWeekEnd = (date: dayjs.Dayjs = dayjs()): dayjs.Dayjs => {
return getMondayWeekStart(date).add(6, "day");
};