From 2bd1bc65cd1e03226e63480edc98b0840a363931 Mon Sep 17 00:00:00 2001 From: Potenz Date: Tue, 15 Jul 2025 20:57:57 +0530 Subject: [PATCH] date issue fixed --- apps/Frontend/src/pages/appointments-page.tsx | 38 +++--------- .../src/pages/insurance-eligibility-page.tsx | 3 +- apps/Frontend/src/utils/dateUtils.ts | 62 +++++++++++++++++++ .../selenium_eligibilityCheckWorker.py | 3 + 4 files changed, 75 insertions(+), 31 deletions(-) create mode 100644 apps/Frontend/src/utils/dateUtils.ts diff --git a/apps/Frontend/src/pages/appointments-page.tsx b/apps/Frontend/src/pages/appointments-page.tsx index 896b4bd..d8061d6 100644 --- a/apps/Frontend/src/pages/appointments-page.tsx +++ b/apps/Frontend/src/pages/appointments-page.tsx @@ -1,6 +1,11 @@ import { useState, useEffect } from "react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { format, addDays, startOfToday, addMinutes } from "date-fns"; +import { + parseLocalDateString, + formatLocalDate, + normalizeToISOString +} from "@/utils/dateUtils"; import { TopAppBar } from "@/components/layout/top-app-bar"; import { Sidebar } from "@/components/layout/sidebar"; import { AddAppointmentModal } from "@/components/appointments/add-appointment-modal"; @@ -372,30 +377,11 @@ export default function AppointmentsPage() { ) => { // Converts local date to exact UTC date with no offset issues - function parseLocalDate(dateInput: Date | string): Date { - if (dateInput instanceof Date) return dateInput; - - const parts = dateInput.split("-"); - if (parts.length !== 3) { - throw new Error(`Invalid date format: ${dateInput}`); - } - - const year = Number(parts[0]); - const month = Number(parts[1]); - const day = Number(parts[2]); - - if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(day)) { - throw new Error(`Invalid date parts in date string: ${dateInput}`); - } - - return new Date(year, month - 1, day); // month is 0-indexed - } - - const rawDate = parseLocalDate(appointmentData.date); + const rawDate = parseLocalDateString(appointmentData.date); const updatedData = { ...appointmentData, - date: rawDate.toLocaleDateString("en-CA"), + date: formatLocalDate(rawDate), }; // Check if we're editing an existing appointment with a valid ID @@ -458,15 +444,7 @@ export default function AppointmentsPage() { ? new Date(appointment.date) : appointment.date; - // Extract UTC year, month, day - const year = dateObj.getUTCFullYear(); - const month = dateObj.getUTCMonth(); // zero-based - const day = dateObj.getUTCDate(); - - // Create a date string in UTC format - const utcDateStr = `${year}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`; - - return utcDateStr === formattedDate; // formattedDate should be 'yyyy-MM-dd' string in UTC format as well + return formatLocalDate(dateObj) === formatLocalDate(selectedDate); // formattedDate should be 'yyyy-MM-dd' string in UTC format as well }); // Process appointments for the scheduler view diff --git a/apps/Frontend/src/pages/insurance-eligibility-page.tsx b/apps/Frontend/src/pages/insurance-eligibility-page.tsx index 01ddfa4..4c8ae50 100644 --- a/apps/Frontend/src/pages/insurance-eligibility-page.tsx +++ b/apps/Frontend/src/pages/insurance-eligibility-page.tsx @@ -33,6 +33,7 @@ import { clearTaskStatus, } from "@/redux/slices/seleniumEligibilityCheckTaskSlice"; import { SeleniumTaskBanner } from "@/components/claims/selenium-task-banner"; +import { formatLocalDate } from "@/utils/dateUtils"; const PatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject @@ -167,7 +168,7 @@ export default function InsuranceEligibilityPage() { // handle selenium const handleSelenium = async () => { - const formattedDob = dateOfBirth ? format(dateOfBirth, "yyyy-MM-dd") : ""; + const formattedDob = dateOfBirth ? formatLocalDate(dateOfBirth) : ""; const data = {memberId, dateOfBirth: formattedDob, insuranceSiteKey: "MH", }; try { diff --git a/apps/Frontend/src/utils/dateUtils.ts b/apps/Frontend/src/utils/dateUtils.ts new file mode 100644 index 0000000..00701dc --- /dev/null +++ b/apps/Frontend/src/utils/dateUtils.ts @@ -0,0 +1,62 @@ +import { format } from "date-fns"; + +/** + * Parse a date string in yyyy-MM-dd format (assumed local) into a JS Date object. + * No timezone conversion is applied. Returns a Date at midnight local time. + */ +export function parseLocalDateString(dateStr: string): Date { + const parts = dateStr.split("-"); + + // Destructure with fallback + const [yearStr, monthStr, dayStr] = parts; + + // Validate all parts are defined and valid strings + if (!yearStr || !monthStr || !dayStr) { + throw new Error("Invalid date string format. Expected yyyy-MM-dd."); + } + + const year = parseInt(yearStr, 10); + const month = parseInt(monthStr, 10) - 1; // JS Date months are 0-based + const day = parseInt(dayStr, 10); + + if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(day)) { + throw new Error("Invalid numeric values in date string."); + } + + return new Date(year, month, day); +} + + + +/** + * Format a JS Date object as a `yyyy-MM-dd` string (in local time). + * Useful for saving date-only data without time component. + */ +export function formatLocalDate(date: Date): string { + return format(date, "yyyy-MM-dd"); +} + +/** + * Get a Date object representing midnight UTC for a given local date. + * Useful for comparing or storing dates consistently across timezones. + */ +export function toUTCDate(date: Date): Date { + return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())); +} + +/** + * Converts a stored UTC date string (e.g. from DB) into a Date object + * and formats it as local yyyy-MM-dd string for UI use. + */ +export function formatUTCDateStringToLocal(dateStr: string): string { + const date = new Date(dateStr); // parsed as UTC + return formatLocalDate(date); +} + +/** + * Ensure any date (Date|string) is formatted to ISO string for consistent backend storage. + * If it's already a string, pass through. If it's a Date, convert to ISO. + */ +export function normalizeToISOString(date: Date | string): string { + return date instanceof Date ? date.toISOString() : date; +} diff --git a/apps/SeleniumService/selenium_eligibilityCheckWorker.py b/apps/SeleniumService/selenium_eligibilityCheckWorker.py index 4058db1..584a16f 100644 --- a/apps/SeleniumService/selenium_eligibilityCheckWorker.py +++ b/apps/SeleniumService/selenium_eligibilityCheckWorker.py @@ -182,3 +182,6 @@ class AutomationMassHealthEligibilityCheck: "status": "error", "message": e } + + finally: + self.driver.quit()