mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-01 15:33:12 +00:00
fix(work-orders): keep Due Date on or after Schedule On
This commit is contained in:
parent
fc3e7591dc
commit
b4d2d69abe
15 changed files with 348 additions and 22 deletions
|
|
@ -19,6 +19,10 @@ import type { WizardWOStatus } from "@/domain/work-orders/types/work-order-wizar
|
|||
import { deriveDayFields } from "@/domain/work-orders/utils/table-day-utils";
|
||||
import { toVendorTablePatch } from "@/domain/work-orders/utils/vendor-assignment-patch";
|
||||
import { DocBadge } from "./slide-over-doc-badge";
|
||||
import {
|
||||
clearDueDateBeforeSchedule,
|
||||
resolveDueDateFloor,
|
||||
} from "@/domain/work-orders/utils/due-date-floor";
|
||||
|
||||
type SlideOverInfoTabEditViewProps = {
|
||||
draft: WorkOrderTableRow;
|
||||
|
|
@ -52,6 +56,7 @@ function SlideOverScheduleOnField({
|
|||
schedMode={draft.schedMode}
|
||||
onSave={(scheduledOn) =>
|
||||
onDraftChange({
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, scheduledOn),
|
||||
scheduledOn,
|
||||
scheduleWeekOnly: false,
|
||||
targetWeek: scheduledOn ? "" : draft.targetWeek,
|
||||
|
|
@ -63,6 +68,7 @@ function SlideOverScheduleOnField({
|
|||
}
|
||||
onSaveWeek={(weekStart) =>
|
||||
onDraftChange({
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, weekStart),
|
||||
scheduledOn: "",
|
||||
targetWeek: weekStart,
|
||||
scheduleWeekOnly: true,
|
||||
|
|
@ -74,6 +80,7 @@ function SlideOverScheduleOnField({
|
|||
}
|
||||
onSaveRange={(start, end) =>
|
||||
onDraftChange({
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, start),
|
||||
scheduledOn: start,
|
||||
scheduleEnd: end,
|
||||
schedMode: "range",
|
||||
|
|
@ -175,6 +182,7 @@ export function SlideOverInfoTabEditView({
|
|||
<DueDateCell
|
||||
type={draft.type}
|
||||
value={draft.dueDate}
|
||||
minDate={resolveDueDateFloor(draft)}
|
||||
onSave={(dueDate) => onDraftChange({ dueDate })}
|
||||
/>
|
||||
</SlideOverInfoRow>
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ type DateCellProps = {
|
|||
onClear?: () => void;
|
||||
/** Screen-reader-only status appended inside the button so the accessible name keeps the visible date and adds durable state. */
|
||||
srStatusSuffix?: string;
|
||||
/** Earliest selectable day in the date calendar (SH-356). */
|
||||
minDate?: string;
|
||||
};
|
||||
|
||||
export function DateCell({
|
||||
|
|
@ -41,6 +43,7 @@ export function DateCell({
|
|||
onSaveRange,
|
||||
onClear,
|
||||
srStatusSuffix,
|
||||
minDate,
|
||||
}: DateCellProps) {
|
||||
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
|
||||
const open = Boolean(anchorEl);
|
||||
|
|
@ -154,6 +157,7 @@ export function DateCell({
|
|||
) : (
|
||||
<MiniCalendar
|
||||
value={value}
|
||||
minDate={minDate}
|
||||
onChange={(d) => {
|
||||
onSave(d);
|
||||
setAnchorEl(null);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ type DueDateCalendarDialogProps = {
|
|||
onOpenChange: (open: boolean) => void;
|
||||
value: string;
|
||||
onSave: (iso: string) => void;
|
||||
minDate?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -19,6 +20,7 @@ export function DueDateCalendarDialog({
|
|||
onOpenChange,
|
||||
value,
|
||||
onSave,
|
||||
minDate,
|
||||
}: DueDateCalendarDialogProps) {
|
||||
return (
|
||||
<Dialog
|
||||
|
|
@ -44,6 +46,7 @@ export function DueDateCalendarDialog({
|
|||
<div className="flex justify-center">
|
||||
<MiniCalendar
|
||||
value={value}
|
||||
minDate={minDate}
|
||||
onChange={(iso) => {
|
||||
onSave(iso);
|
||||
onOpenChange(false);
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ type DueDateCellProps = {
|
|||
* read by colour alone.
|
||||
*/
|
||||
pastDue?: boolean;
|
||||
/** Schedule On floor: earlier days are not selectable (SH-356). */
|
||||
minDate?: string;
|
||||
onSave: (date: string) => void;
|
||||
};
|
||||
|
||||
|
|
@ -29,6 +31,7 @@ export function DueDateCell({
|
|||
q,
|
||||
highlight,
|
||||
pastDue = false,
|
||||
minDate,
|
||||
onSave,
|
||||
}: DueDateCellProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
|
@ -40,6 +43,7 @@ export function DueDateCell({
|
|||
q={q}
|
||||
highlight={highlight}
|
||||
onSave={onSave}
|
||||
minDate={minDate}
|
||||
srStatusSuffix={pastDue ? "Past due" : undefined}
|
||||
/>
|
||||
);
|
||||
|
|
@ -87,7 +91,13 @@ export function DueDateCell({
|
|||
style={{ color: "var(--muted-foreground)" }}
|
||||
/>
|
||||
</button>
|
||||
<DueDateCalendarDialog open={open} onOpenChange={setOpen} value={value} onSave={onSave} />
|
||||
<DueDateCalendarDialog
|
||||
minDate={minDate}
|
||||
open={open}
|
||||
onOpenChange={setOpen}
|
||||
value={value}
|
||||
onSave={onSave}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import { shouldNotifyCrossWeek } from "@/domain/work-orders/utils/should-notify-
|
|||
import type { ReturnTypeOfUseWorkOrdersTableData } from "./wo-table-types";
|
||||
import { rescheduleNotify, weekMoveNotify } from "./reschedule-notify";
|
||||
import type { WoTableRowHandlers } from "./wo-table-row";
|
||||
import { clearDueDateBeforeSchedule } from "@/domain/work-orders/utils/due-date-floor";
|
||||
|
||||
function revealOrNotifyScheduledDay(args: {
|
||||
id: string | number;
|
||||
|
|
@ -101,8 +102,11 @@ export function useWoTableRowHandlers({
|
|||
onPatch: (id, patch, options) => {
|
||||
onPatch(id, patch, options);
|
||||
},
|
||||
onScheduleDate: (id: string | number, date: string) => {
|
||||
onPatch(id, applySchedulePatch(date, false));
|
||||
onScheduleDate: (id: string | number, date: string, dueDate?: string) => {
|
||||
onPatch(id, {
|
||||
...applySchedulePatch(date, false),
|
||||
...clearDueDateBeforeSchedule(dueDate, date),
|
||||
});
|
||||
revealOrNotifyScheduledDay({
|
||||
id,
|
||||
date,
|
||||
|
|
@ -111,8 +115,11 @@ export function useWoTableRowHandlers({
|
|||
revealScheduledRow,
|
||||
});
|
||||
},
|
||||
onScheduleRange: (id: string | number, start: string, end: string) => {
|
||||
onPatch(id, applyScheduleRangePatch(start, end));
|
||||
onScheduleRange: (id: string | number, start: string, end: string, dueDate?: string) => {
|
||||
onPatch(id, {
|
||||
...applyScheduleRangePatch(start, end),
|
||||
...clearDueDateBeforeSchedule(dueDate, start),
|
||||
});
|
||||
revealOrNotifyScheduledDay({
|
||||
id,
|
||||
date: start,
|
||||
|
|
@ -121,7 +128,7 @@ export function useWoTableRowHandlers({
|
|||
revealScheduledRow,
|
||||
});
|
||||
},
|
||||
onScheduleWeek: (id: string | number, week: string) => {
|
||||
onScheduleWeek: (id: string | number, week: string, dueDate?: string) => {
|
||||
const isClear = week === "";
|
||||
onPatch(
|
||||
id,
|
||||
|
|
@ -135,6 +142,7 @@ export function useWoTableRowHandlers({
|
|||
schedMode: "week",
|
||||
dayGroup: "",
|
||||
dayLabel: "",
|
||||
...clearDueDateBeforeSchedule(dueDate, week),
|
||||
},
|
||||
);
|
||||
weekMoveNotify(week, {
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ import { VendorCell } from "./cells/vendor-cell";
|
|||
import { toVendorTablePatch } from "@/domain/work-orders/utils/vendor-assignment-patch";
|
||||
import { toInlineDraftRow } from "./to-inline-draft-row";
|
||||
import { WoTableInlineIdentityCells } from "./wo-table-inline-identity-cells";
|
||||
import {
|
||||
clearDueDateBeforeSchedule,
|
||||
resolveDueDateFloor,
|
||||
} from "@/domain/work-orders/utils/due-date-floor";
|
||||
|
||||
type WoTableInlineRowCellsProps = {
|
||||
draft: WorkOrderTableDraft;
|
||||
|
|
@ -93,6 +97,7 @@ function InlineScheduleOnCell({
|
|||
onSave={(date) =>
|
||||
onPatch(
|
||||
withAutoScheduleStatus(draft, {
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, date),
|
||||
scheduledOn: date,
|
||||
scheduleWeekOnly: false,
|
||||
targetWeek: "",
|
||||
|
|
@ -105,6 +110,7 @@ function InlineScheduleOnCell({
|
|||
onSaveWeek={(week) =>
|
||||
onPatch(
|
||||
withAutoScheduleStatus(draft, {
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, week),
|
||||
scheduledOn: "",
|
||||
targetWeek: week,
|
||||
scheduleWeekOnly: true,
|
||||
|
|
@ -117,6 +123,7 @@ function InlineScheduleOnCell({
|
|||
onSaveRange={(start, end) =>
|
||||
onPatch(
|
||||
withAutoScheduleStatus(draft, {
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, start),
|
||||
scheduledOn: start,
|
||||
scheduleEnd: end,
|
||||
schedMode: "range",
|
||||
|
|
@ -200,6 +207,7 @@ export function WoTableInlineRowCells({
|
|||
<DueDateCell
|
||||
type={draft.type}
|
||||
value={draft.dueDate}
|
||||
minDate={resolveDueDateFloor(draft)}
|
||||
onSave={(date) => onPatch({ dueDate: date })}
|
||||
/>
|
||||
</td>
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import { EditableWONumberCell } from "./cells/editable-wo-number-cell";
|
|||
import { SiteCell } from "./cells/site-cell";
|
||||
import { TypeCell } from "./cells/type-cell";
|
||||
import type { WoTableRowHandlers } from "./wo-table-row";
|
||||
import { resolveDueDateFloor } from "@/domain/work-orders/utils/due-date-floor";
|
||||
|
||||
type WoTableRowCellsProps = {
|
||||
row: WorkOrderTableRow;
|
||||
|
|
@ -146,9 +147,9 @@ export function WoTableRowCells({
|
|||
targetWeek={row.targetWeek}
|
||||
scheduleEnd={row.scheduleEnd}
|
||||
schedMode={row.schedMode}
|
||||
onSave={(date) => handlers.onScheduleDate(row.id, date)}
|
||||
onSaveWeek={(week) => handlers.onScheduleWeek(row.id, week)}
|
||||
onSaveRange={(start, end) => handlers.onScheduleRange(row.id, start, end)}
|
||||
onSave={(date) => handlers.onScheduleDate(row.id, date, row.dueDate)}
|
||||
onSaveWeek={(week) => handlers.onScheduleWeek(row.id, week, row.dueDate)}
|
||||
onSaveRange={(start, end) => handlers.onScheduleRange(row.id, start, end, row.dueDate)}
|
||||
onClear={hasSchedule && !viewOnly ? () => handlers.onScheduleWeek(row.id, "") : undefined}
|
||||
/>
|
||||
</td>
|
||||
|
|
@ -163,6 +164,7 @@ export function WoTableRowCells({
|
|||
q={search}
|
||||
highlight={forceDueDateHighlight || row.isPastDue}
|
||||
pastDue={row.isPastDue}
|
||||
minDate={resolveDueDateFloor(row)}
|
||||
onSave={(date) => onPatchRow({ dueDate: date })}
|
||||
/>
|
||||
</td>
|
||||
|
|
|
|||
|
|
@ -36,9 +36,10 @@ export type WoTableRowHandlers = {
|
|||
patch: Partial<WorkOrderTableRow>,
|
||||
options?: WorkOrderTablePatchOptions,
|
||||
) => void;
|
||||
onScheduleDate: (id: string | number, date: string) => void;
|
||||
onScheduleWeek: (id: string | number, week: string) => void;
|
||||
onScheduleRange: (id: string | number, start: string, end: string) => void;
|
||||
/** `dueDate` is the row's current Due Date, cleared when the new schedule passes it (SH-356). */
|
||||
onScheduleDate: (id: string | number, date: string, dueDate?: string) => void;
|
||||
onScheduleWeek: (id: string | number, week: string, dueDate?: string) => void;
|
||||
onScheduleRange: (id: string | number, start: string, end: string, dueDate?: string) => void;
|
||||
onDuplicateFound?: (existing: WorkOrderTableRow) => void;
|
||||
onOpenSlideOver: (row: WorkOrderTableRow) => void;
|
||||
onRequestComplete?: (row: WorkOrderTableRow) => void;
|
||||
|
|
@ -77,17 +78,17 @@ function guardScheduleHandlers(
|
|||
if (!canApplyWorkOrderTablePatch(row.status, patch)) return;
|
||||
handlers.onPatch(id, patch, options);
|
||||
},
|
||||
onScheduleDate: (id, date) => {
|
||||
onScheduleDate: (id, date, dueDate) => {
|
||||
if (!canApplyWorkOrderTablePatch(row.status, { scheduledOn: date })) return;
|
||||
handlers.onScheduleDate(id, date);
|
||||
handlers.onScheduleDate(id, date, dueDate);
|
||||
},
|
||||
onScheduleWeek: (id, week) => {
|
||||
onScheduleWeek: (id, week, dueDate) => {
|
||||
if (!canApplyWorkOrderTablePatch(row.status, { targetWeek: week })) return;
|
||||
handlers.onScheduleWeek(id, week);
|
||||
handlers.onScheduleWeek(id, week, dueDate);
|
||||
},
|
||||
onScheduleRange: (id, start, end) => {
|
||||
onScheduleRange: (id, start, end, dueDate) => {
|
||||
if (!canApplyWorkOrderTablePatch(row.status, { scheduledOn: start })) return;
|
||||
handlers.onScheduleRange(id, start, end);
|
||||
handlers.onScheduleRange(id, start, end, dueDate);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ type MiniCalendarProps = {
|
|||
range?: boolean;
|
||||
rangeStart?: string;
|
||||
rangeEnd?: string;
|
||||
/** Days before this ISO date are disabled (SH-356 Due Date floor). */
|
||||
minDate?: string;
|
||||
};
|
||||
|
||||
export function MiniCalendar({
|
||||
|
|
@ -20,9 +22,11 @@ export function MiniCalendar({
|
|||
range,
|
||||
rangeStart = "",
|
||||
rangeEnd = "",
|
||||
minDate = "",
|
||||
}: MiniCalendarProps) {
|
||||
const initRaw = range ? rangeStart || value : value;
|
||||
const init = /^\d{4}-\d{2}-\d{2}$/.test(initRaw) ? initRaw : todayIso();
|
||||
const fallback = minDate > todayIso() ? minDate : todayIso();
|
||||
const init = /^\d{4}-\d{2}-\d{2}$/.test(initRaw) ? initRaw : fallback;
|
||||
const [iy, im] = init.split("-").map(Number);
|
||||
const [viewY, setViewY] = useState(iy);
|
||||
const [viewM, setViewM] = useState(im);
|
||||
|
|
@ -94,6 +98,7 @@ export function MiniCalendar({
|
|||
}
|
||||
const iso = `${viewY}-${pad2(viewM)}-${pad2(day)}`;
|
||||
const isToday = iso === today;
|
||||
const beforeMin = Boolean(minDate) && iso < minDate;
|
||||
const { bg, fg, radius, filled } = getMiniCalendarCellStyle({
|
||||
iso,
|
||||
value,
|
||||
|
|
@ -107,8 +112,9 @@ export function MiniCalendar({
|
|||
<button
|
||||
key={i}
|
||||
type="button"
|
||||
disabled={beforeMin}
|
||||
onClick={() => onChange(iso)}
|
||||
className="text-center text-[12px] transition-colors hover:bg-[var(--color-primary-tint)]"
|
||||
className="text-center text-[12px] transition-colors hover:bg-[var(--color-primary-tint)] disabled:cursor-not-allowed disabled:bg-transparent disabled:opacity-40"
|
||||
style={{
|
||||
height: 26,
|
||||
borderRadius: radius,
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ type WizardDateFieldProps = {
|
|||
scheduleEnd?: string;
|
||||
schedMode?: WizardSchedMode;
|
||||
onChangeRange?: (start: string, end: string) => void;
|
||||
/** Earliest selectable day in the date calendar (SH-356). */
|
||||
minDate?: string;
|
||||
};
|
||||
|
||||
function fieldLabel(args: {
|
||||
|
|
@ -49,6 +51,7 @@ export function WizardDateField({
|
|||
scheduleEnd = "",
|
||||
schedMode,
|
||||
onChangeRange,
|
||||
minDate,
|
||||
}: WizardDateFieldProps) {
|
||||
const { anchorEl, open, setAnchor, close } = useMenuAnchor();
|
||||
const allowRange = Boolean(allowWeekMode && onChangeRange);
|
||||
|
|
@ -127,6 +130,7 @@ export function WizardDateField({
|
|||
) : (
|
||||
<MiniCalendar
|
||||
value={value}
|
||||
minDate={minDate}
|
||||
onChange={(iso) => {
|
||||
onChange(iso);
|
||||
close();
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ type WizardDueDateFieldProps = {
|
|||
value: string;
|
||||
placeholder: string;
|
||||
onChange: (iso: string) => void;
|
||||
/** Schedule On floor: earlier days are not selectable (SH-356). */
|
||||
minDate?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -21,11 +23,19 @@ export function WizardDueDateField({
|
|||
value,
|
||||
placeholder,
|
||||
onChange,
|
||||
minDate,
|
||||
}: WizardDueDateFieldProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
if (type !== "Emergency") {
|
||||
return <WizardDateField value={value} placeholder={placeholder} onChange={onChange} />;
|
||||
return (
|
||||
<WizardDateField
|
||||
value={value}
|
||||
placeholder={placeholder}
|
||||
onChange={onChange}
|
||||
minDate={minDate}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
@ -63,7 +73,13 @@ export function WizardDueDateField({
|
|||
</span>
|
||||
<ChevronDown size={13} className="shrink-0" style={{ color: "var(--muted-foreground)" }} />
|
||||
</button>
|
||||
<DueDateCalendarDialog open={open} onOpenChange={setOpen} value={value} onSave={onChange} />
|
||||
<DueDateCalendarDialog
|
||||
open={open}
|
||||
onOpenChange={setOpen}
|
||||
value={value}
|
||||
onSave={onChange}
|
||||
minDate={minDate}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ import {
|
|||
import { WizardSeverityPicker } from "@/app/(protected)/workorders/_components/wizard/wizard-severity-picker";
|
||||
import { CheckCircle2, Clock, Flame, Users, Wrench, Zap } from "lucide-react";
|
||||
import type { ElementType } from "react";
|
||||
import {
|
||||
clearDueDateBeforeSchedule,
|
||||
resolveDueDateFloor,
|
||||
} from "@/domain/work-orders/utils/due-date-floor";
|
||||
|
||||
const WO_TYPE_ICONS: Record<WOType, ElementType> = {
|
||||
PM: Wrench,
|
||||
|
|
@ -165,6 +169,7 @@ function WizardTypeScheduleDates({ draft, onUpdate, onPatch }: WizardTypeSchedul
|
|||
onChange={(iso) =>
|
||||
onPatch(
|
||||
withAutoScheduleStatus(draft, {
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, iso),
|
||||
scheduledOn: iso,
|
||||
scheduleWeekOnly: false,
|
||||
scheduleEnd: "",
|
||||
|
|
@ -177,6 +182,7 @@ function WizardTypeScheduleDates({ draft, onUpdate, onPatch }: WizardTypeSchedul
|
|||
onChangeWeek={(w) =>
|
||||
onPatch(
|
||||
withAutoScheduleStatus(draft, {
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, w),
|
||||
scheduledOn: w,
|
||||
scheduleWeekOnly: Boolean(w),
|
||||
scheduleEnd: "",
|
||||
|
|
@ -189,6 +195,7 @@ function WizardTypeScheduleDates({ draft, onUpdate, onPatch }: WizardTypeSchedul
|
|||
onChangeRange={(s, e) =>
|
||||
onPatch(
|
||||
withAutoScheduleStatus(draft, {
|
||||
...clearDueDateBeforeSchedule(draft.dueDate, s),
|
||||
scheduledOn: s,
|
||||
scheduleEnd: e,
|
||||
schedMode: "range",
|
||||
|
|
@ -211,6 +218,7 @@ function WizardTypeScheduleDates({ draft, onUpdate, onPatch }: WizardTypeSchedul
|
|||
<WizardDueDateField
|
||||
type={draft.type}
|
||||
value={draft.dueDate}
|
||||
minDate={resolveDueDateFloor(draft)}
|
||||
placeholder="No due date"
|
||||
onChange={(iso) => onUpdate("dueDate", iso)}
|
||||
/>
|
||||
|
|
|
|||
27
src/domain/work-orders/utils/due-date-floor.ts
Normal file
27
src/domain/work-orders/utils/due-date-floor.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* SH-356: a Due Date can never be earlier than Schedule On.
|
||||
*
|
||||
* The floor is the concrete Schedule On day, the start of a date range, or — for a
|
||||
* week-only schedule (SH-110) — the first day of the target week.
|
||||
*/
|
||||
export function resolveDueDateFloor(schedule: {
|
||||
scheduledOn?: string | null;
|
||||
targetWeek?: string | null;
|
||||
}): string {
|
||||
return schedule.scheduledOn?.trim() || schedule.targetWeek?.trim() || "";
|
||||
}
|
||||
|
||||
export function isDueDateBeforeFloor(dueDate: string | null | undefined, floor: string): boolean {
|
||||
return Boolean(dueDate && floor && dueDate < floor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch fragment for a Schedule On change: clears an existing Due Date that the new
|
||||
* schedule would leave in the past. Never bumps it to a guessed value.
|
||||
*/
|
||||
export function clearDueDateBeforeSchedule(
|
||||
dueDate: string | null | undefined,
|
||||
nextFloor: string,
|
||||
): { dueDate?: string } {
|
||||
return isDueDateBeforeFloor(dueDate, nextFloor) ? { dueDate: "" } : {};
|
||||
}
|
||||
|
|
@ -0,0 +1,192 @@
|
|||
import { fireEvent, render, renderHook, screen, within } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { SlideOverInfoTabEditView } from "@/app/(protected)/workorders/_components/detail/tabs/slide-over-info-tab-edit-view";
|
||||
import { WoTableInlineRow } from "@/app/(protected)/workorders/_components/list/table/wo-table-inline-row";
|
||||
import { useWoTableRowHandlers } from "@/app/(protected)/workorders/_components/list/table/use-wo-table-row-handlers";
|
||||
import { MiniCalendar } from "@/app/(protected)/workorders/_components/wizard/mini-calendar";
|
||||
import { WizardStepTypeSchedule } from "@/app/(protected)/workorders/_components/wizard/wizard-step-type-schedule";
|
||||
import { EMPTY_TABLE_DRAFT } from "@/domain/work-orders/types/work-order-table-row";
|
||||
import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row";
|
||||
import { EMPTY_WIZARD_DRAFT } from "@/domain/work-orders/types/work-order-wizard";
|
||||
import { pad2, todayIso } from "@/domain/work-orders/utils/wizard-date-utils";
|
||||
import { renderWithProviders } from "@/test/test-utils";
|
||||
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/site-cell", () => ({
|
||||
SiteCell: () => null,
|
||||
}));
|
||||
vi.mock(
|
||||
"@/app/(protected)/workorders/_components/list/table/cells/editable-wo-number-cell",
|
||||
() => ({ EditableWONumberCell: () => null }),
|
||||
);
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/type-cell", () => ({
|
||||
TypeCell: () => null,
|
||||
}));
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/assigned-to-cell", () => ({
|
||||
AssignedToCell: () => null,
|
||||
}));
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/appt-time-cell", () => ({
|
||||
ApptTimeCell: () => null,
|
||||
}));
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/pm-type-cell", () => ({
|
||||
PMTypeCell: () => null,
|
||||
}));
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/vendor-cell", () => ({
|
||||
VendorCell: () => null,
|
||||
}));
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/cells/status-cell", () => ({
|
||||
StatusCell: () => null,
|
||||
}));
|
||||
vi.mock("@/app/(protected)/workorders/_components/list/table/reschedule-notify", () => ({
|
||||
rescheduleNotify: vi.fn(),
|
||||
weekMoveNotify: vi.fn(),
|
||||
}));
|
||||
|
||||
function day(n: number): string {
|
||||
const [y, m] = todayIso().split("-").map(Number);
|
||||
return `${y}-${pad2(m)}-${pad2(n)}`;
|
||||
}
|
||||
|
||||
function rowByLabel(label: string) {
|
||||
return screen.getByText(label, { selector: "span,label,div" }).closest("div")!;
|
||||
}
|
||||
|
||||
describe("Due Date cannot be before Schedule On (SH-356)", () => {
|
||||
it("disables calendar days before the floor", () => {
|
||||
render(<MiniCalendar value={day(12)} minDate={day(10)} onChange={vi.fn()} />);
|
||||
|
||||
expect(screen.getByRole("button", { name: "9" })).toBeDisabled();
|
||||
expect(screen.getByRole("button", { name: "10" })).toBeEnabled();
|
||||
expect(screen.getByRole("button", { name: "11" })).toBeEnabled();
|
||||
});
|
||||
|
||||
describe("slide-over edit", () => {
|
||||
const draft = { scheduledOn: day(10), dueDate: day(12) } as Partial<WorkOrderTableRow>;
|
||||
|
||||
function renderSlideOver(onDraftChange = vi.fn()) {
|
||||
renderWithProviders(
|
||||
<SlideOverInfoTabEditView
|
||||
draft={{ ...EMPTY_TABLE_DRAFT, id: 42, targetWeek: "", ...draft } as WorkOrderTableRow}
|
||||
users={[]}
|
||||
sites={[]}
|
||||
vendors={[]}
|
||||
pmTypes={[]}
|
||||
allOrders={[]}
|
||||
onDraftChange={onDraftChange}
|
||||
/>,
|
||||
);
|
||||
return onDraftChange;
|
||||
}
|
||||
|
||||
it("disables Due Date days before Schedule On", async () => {
|
||||
renderSlideOver();
|
||||
fireEvent.click(within(rowByLabel("Due Date")).getByRole("button"));
|
||||
|
||||
expect(await screen.findByRole("button", { name: "9" })).toBeDisabled();
|
||||
expect(screen.getByRole("button", { name: "10" })).toBeEnabled();
|
||||
});
|
||||
|
||||
it("clears the Due Date when Schedule On moves past it", async () => {
|
||||
const onDraftChange = renderSlideOver();
|
||||
fireEvent.click(within(rowByLabel("Schedule On")).getAllByRole("button")[0]);
|
||||
fireEvent.click(await screen.findByRole("button", { name: "15" }));
|
||||
|
||||
expect(onDraftChange).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ scheduledOn: day(15), dueDate: "" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("wizard: clears Due Date on a later Schedule On and floors the Due Date picker", async () => {
|
||||
const onPatch = vi.fn();
|
||||
render(
|
||||
<WizardStepTypeSchedule
|
||||
draft={{ ...EMPTY_WIZARD_DRAFT, scheduledOn: day(10), schedMode: "date", dueDate: day(12) }}
|
||||
users={[]}
|
||||
onUpdate={vi.fn()}
|
||||
onPatch={onPatch}
|
||||
/>,
|
||||
);
|
||||
|
||||
fireEvent.click(within(rowByLabel("Due Date").parentElement!).getAllByRole("button").at(-1)!);
|
||||
expect(await screen.findByRole("button", { name: "9" })).toBeDisabled();
|
||||
fireEvent.keyDown(document.activeElement ?? document.body, { key: "Escape" });
|
||||
|
||||
const scheduleBlock = rowByLabel("Schedule On").parentElement!;
|
||||
fireEvent.click(within(scheduleBlock).getAllByRole("button").at(-1)!);
|
||||
const calendars = await screen.findAllByRole("button", { name: "15" });
|
||||
fireEvent.click(calendars.at(-1)!);
|
||||
|
||||
expect(onPatch).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ scheduledOn: day(15), dueDate: "" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("inline creation row: clears Due Date on a later Schedule On", async () => {
|
||||
const onChange = vi.fn();
|
||||
renderWithProviders(
|
||||
<table>
|
||||
<tbody>
|
||||
<WoTableInlineRow
|
||||
draft={{ ...EMPTY_TABLE_DRAFT, scheduledOn: day(10), dueDate: day(12) }}
|
||||
draftDayKey={day(10)}
|
||||
users={[]}
|
||||
sites={[]}
|
||||
vendors={[]}
|
||||
pmTypes={[]}
|
||||
allRows={[]}
|
||||
onChange={onChange}
|
||||
onSave={vi.fn()}
|
||||
onCancel={vi.fn()}
|
||||
/>
|
||||
</tbody>
|
||||
</table>,
|
||||
{ withAuth: false },
|
||||
);
|
||||
|
||||
const cells = screen.getAllByRole("cell");
|
||||
const dateButtons = cells.flatMap((cell) => within(cell).queryAllByRole("button"));
|
||||
const scheduleButton = dateButtons.find(
|
||||
(b) => b.textContent?.includes(day(10).slice(5).replace("-", "/")) ?? false,
|
||||
);
|
||||
fireEvent.click(scheduleButton ?? dateButtons[0]);
|
||||
fireEvent.click(await screen.findByRole("button", { name: "15" }));
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ scheduledOn: day(15), dueDate: "" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("board row: sends the Due Date clear in the same schedule patch", () => {
|
||||
const onPatch = vi.fn();
|
||||
const { result } = renderHook(() =>
|
||||
useWoTableRowHandlers({
|
||||
tableData: {
|
||||
dragState: { current: { dragId: null, overId: null } },
|
||||
setDragViz: vi.fn(),
|
||||
reorderRows: vi.fn(),
|
||||
endDrag: vi.fn(),
|
||||
revealScheduledRow: vi.fn(),
|
||||
setSelectedId: vi.fn(),
|
||||
} as never,
|
||||
weekMonday: "2026-05-18",
|
||||
onNavigateToDay: vi.fn(),
|
||||
onOpenSlideOver: vi.fn(),
|
||||
onOpenMedia: vi.fn(),
|
||||
onPatch,
|
||||
onScheduleHighlight: vi.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
result.current.onScheduleDate("wo-1", "2026-05-21", "2026-05-20");
|
||||
expect(onPatch).toHaveBeenLastCalledWith("wo-1", expect.objectContaining({ dueDate: "" }));
|
||||
|
||||
result.current.onScheduleRange("wo-1", "2026-05-19", "2026-05-22", "2026-05-20");
|
||||
expect(onPatch.mock.lastCall?.[1]).not.toHaveProperty("dueDate");
|
||||
|
||||
result.current.onScheduleWeek("wo-1", "2026-05-25", "2026-05-20");
|
||||
expect(onPatch).toHaveBeenLastCalledWith(
|
||||
"wo-1",
|
||||
expect.objectContaining({ targetWeek: "2026-05-25", dueDate: "" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
29
src/test/domain/work-orders/utils/due-date-floor.test.ts
Normal file
29
src/test/domain/work-orders/utils/due-date-floor.test.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
clearDueDateBeforeSchedule,
|
||||
isDueDateBeforeFloor,
|
||||
resolveDueDateFloor,
|
||||
} from "@/domain/work-orders/utils/due-date-floor";
|
||||
|
||||
describe("due date floor (SH-356)", () => {
|
||||
it("uses the concrete or range-start Schedule On, else the target week's first day", () => {
|
||||
expect(resolveDueDateFloor({ scheduledOn: "2026-09-10", targetWeek: "" })).toBe("2026-09-10");
|
||||
expect(resolveDueDateFloor({ scheduledOn: "", targetWeek: "2026-09-14" })).toBe("2026-09-14");
|
||||
expect(resolveDueDateFloor({ scheduledOn: "", targetWeek: "" })).toBe("");
|
||||
expect(resolveDueDateFloor({ scheduledOn: null, targetWeek: undefined })).toBe("");
|
||||
});
|
||||
|
||||
it("flags only a Due Date strictly before the floor", () => {
|
||||
expect(isDueDateBeforeFloor("2026-09-09", "2026-09-10")).toBe(true);
|
||||
expect(isDueDateBeforeFloor("2026-09-10", "2026-09-10")).toBe(false);
|
||||
expect(isDueDateBeforeFloor("", "2026-09-10")).toBe(false);
|
||||
expect(isDueDateBeforeFloor("2026-09-09", "")).toBe(false);
|
||||
});
|
||||
|
||||
it("clears (never bumps) a Due Date the new schedule passes", () => {
|
||||
expect(clearDueDateBeforeSchedule("2026-09-12", "2026-09-15")).toEqual({ dueDate: "" });
|
||||
expect(clearDueDateBeforeSchedule("2026-09-12", "2026-09-12")).toEqual({});
|
||||
expect(clearDueDateBeforeSchedule("2026-09-20", "2026-09-15")).toEqual({});
|
||||
expect(clearDueDateBeforeSchedule("2026-09-12", "")).toEqual({});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue