mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 10:28:59 +00:00
fix(vendors): correct roster validation and editing
This commit is contained in:
parent
19a54e38c6
commit
35bfb9b7c8
10 changed files with 206 additions and 10 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 134 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 142 KiB After Width: | Height: | Size: 146 KiB |
|
|
@ -211,7 +211,7 @@ function DrawerBody({
|
||||||
<Divider />
|
<Divider />
|
||||||
<Box>
|
<Box>
|
||||||
<SectionTitle>Notes</SectionTitle>
|
<SectionTitle>Notes</SectionTitle>
|
||||||
<Text variant="description" sx={{ whiteSpace: "pre-wrap" }}>
|
<Text variant="description" sx={{ overflowWrap: "anywhere", whiteSpace: "pre-wrap" }}>
|
||||||
{roster.notes}
|
{roster.notes}
|
||||||
</Text>
|
</Text>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
@ -341,7 +341,7 @@ function DrawerEditor({
|
||||||
<Button onClick={onClose} disabled={form.isSaving}>
|
<Button onClick={onClose} disabled={form.isSaving}>
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="submit" variant="contained" disabled={form.isSaving || !form.isValid}>
|
<Button type="submit" variant="contained" disabled={form.isSaving}>
|
||||||
{form.isSaving ? "Saving..." : "Save changes"}
|
{form.isSaving ? "Saving..." : "Save changes"}
|
||||||
</Button>
|
</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,10 @@
|
||||||
import { Controller, useFieldArray, type Control, type FieldErrors } from "react-hook-form";
|
import {
|
||||||
|
Controller,
|
||||||
|
useFieldArray,
|
||||||
|
useWatch,
|
||||||
|
type Control,
|
||||||
|
type FieldErrors,
|
||||||
|
} from "react-hook-form";
|
||||||
import AddIcon from "@mui/icons-material/Add";
|
import AddIcon from "@mui/icons-material/Add";
|
||||||
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutlined";
|
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutlined";
|
||||||
import {
|
import {
|
||||||
|
|
@ -16,8 +22,11 @@ import {
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { VendorTradeSpecialtiesField } from "./vendor-trade-specialties-field";
|
import { VendorTradeSpecialtiesField } from "./vendor-trade-specialties-field";
|
||||||
import { formatNorthAmericanPhone } from "@/lib/format/na-phone";
|
import { formatNorthAmericanPhone } from "@/lib/format/na-phone";
|
||||||
import type { VendorCompanyRosterFormValues } from "@/domain/vendors/schemas/vendor-roster-schema";
|
import {
|
||||||
import type { RosterTechnicianValues } from "@/domain/vendors/schemas/vendor-roster-schema";
|
VENDOR_NOTES_MAX_LENGTH,
|
||||||
|
type RosterTechnicianValues,
|
||||||
|
type VendorCompanyRosterFormValues,
|
||||||
|
} from "@/domain/vendors/schemas/vendor-roster-schema";
|
||||||
import type { VendorFacetCompany } from "@/domain/vendors/types/vendor";
|
import type { VendorFacetCompany } from "@/domain/vendors/types/vendor";
|
||||||
|
|
||||||
interface VendorRosterFormFieldsProps {
|
interface VendorRosterFormFieldsProps {
|
||||||
|
|
@ -121,6 +130,8 @@ function CompanyFields({
|
||||||
onSelectCompany,
|
onSelectCompany,
|
||||||
onClearSelectedCompany,
|
onClearSelectedCompany,
|
||||||
}: VendorRosterFormFieldsProps) {
|
}: VendorRosterFormFieldsProps) {
|
||||||
|
const notes = useWatch({ control, name: "notes" }) ?? "";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<Stack spacing={2}>
|
<Stack spacing={2}>
|
||||||
|
|
@ -222,6 +233,19 @@ function CompanyFields({
|
||||||
multiline
|
multiline
|
||||||
minRows={2}
|
minRows={2}
|
||||||
fullWidth
|
fullWidth
|
||||||
|
error={Boolean(errors.notes)}
|
||||||
|
helperText={`${notes.length}/${VENDOR_NOTES_MAX_LENGTH} characters · ${
|
||||||
|
errors.notes?.message ?? `Maximum ${VENDOR_NOTES_MAX_LENGTH} characters`
|
||||||
|
}`}
|
||||||
|
slotProps={{
|
||||||
|
htmlInput: {
|
||||||
|
maxLength: VENDOR_NOTES_MAX_LENGTH,
|
||||||
|
style: {
|
||||||
|
overflowWrap: "anywhere",
|
||||||
|
whiteSpace: "pre-wrap",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ export default function VendorRosterPage({ vendorId, companyId }: VendorRosterPa
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="submit" variant="contained" disabled={form.isSaving || !form.isValid}>
|
<Button type="submit" variant="contained" disabled={form.isSaving}>
|
||||||
{submitLabel(form.isSaving, isEdit)}
|
{submitLabel(form.isSaving, isEdit)}
|
||||||
</Button>
|
</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,8 @@ const northAmericanPhone = z
|
||||||
|
|
||||||
const optionalEmail = z.union([z.string().email("Invalid email"), z.literal("")]);
|
const optionalEmail = z.union([z.string().email("Invalid email"), z.literal("")]);
|
||||||
|
|
||||||
|
export const VENDOR_NOTES_MAX_LENGTH = 500;
|
||||||
|
|
||||||
export const rosterTechnicianSchema = z.object({
|
export const rosterTechnicianSchema = z.object({
|
||||||
id: z.union([z.string(), z.number()]).optional(),
|
id: z.union([z.string(), z.number()]).optional(),
|
||||||
contactName: z.string(),
|
contactName: z.string(),
|
||||||
|
|
@ -56,7 +58,9 @@ const baseCompanyFields = {
|
||||||
state: z.string(),
|
state: z.string(),
|
||||||
zip: z.string(),
|
zip: z.string(),
|
||||||
googleMapsUrl: httpsUrl,
|
googleMapsUrl: httpsUrl,
|
||||||
notes: z.string(),
|
notes: z
|
||||||
|
.string()
|
||||||
|
.max(VENDOR_NOTES_MAX_LENGTH, `Notes must be ${VENDOR_NOTES_MAX_LENGTH} characters or fewer`),
|
||||||
technicians: z.array(rosterTechnicianSchema),
|
technicians: z.array(rosterTechnicianSchema),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -67,7 +71,7 @@ export const vendorCompanyRosterSchema = z.object(baseCompanyFields).superRefine
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: z.ZodIssueCode.custom,
|
code: z.ZodIssueCode.custom,
|
||||||
path: ["companyPhone"],
|
path: ["companyPhone"],
|
||||||
message: "Company phone or email is required",
|
message: "Provide a company phone or email (at least one required)",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
import { useController } from "react-hook-form";
|
import { useController, useFieldArray } from "react-hook-form";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const rosterGet = vi.fn();
|
const rosterGet = vi.fn();
|
||||||
|
|
@ -321,6 +321,31 @@ describe("useVendorRosterForm prototype defaults", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("useVendorRosterForm technician removal", () => {
|
||||||
|
it("keeps a valid roster saveable after an existing technician is removed", async () => {
|
||||||
|
rosterQueryResult = {
|
||||||
|
data: rosterWithTechnicians,
|
||||||
|
isLoading: false,
|
||||||
|
isError: false,
|
||||||
|
error: null,
|
||||||
|
refetch: vi.fn(),
|
||||||
|
};
|
||||||
|
const { result } = renderHook(
|
||||||
|
() => {
|
||||||
|
const form = useVendorRosterForm({ mode: "update", companyId: 5 });
|
||||||
|
const technicians = useFieldArray({ control: form.control, name: "technicians" });
|
||||||
|
return { form, technicians };
|
||||||
|
},
|
||||||
|
{ wrapper: makeWrapper(createClient()) },
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => expect(result.current.form.isValid).toBe(true));
|
||||||
|
act(() => result.current.technicians.remove(1));
|
||||||
|
|
||||||
|
await waitFor(() => expect(result.current.form.isValid).toBe(true));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("useVendorRosterForm stale-selection handling", () => {
|
describe("useVendorRosterForm stale-selection handling", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
rosterGet.mockReset();
|
rosterGet.mockReset();
|
||||||
|
|
|
||||||
52
src/test/app/(protected)/vendors/vendor-create-modal.test.tsx
vendored
Normal file
52
src/test/app/(protected)/vendors/vendor-create-modal.test.tsx
vendored
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { VendorCreateModal } from "@/app/(protected)/vendors/_components/vendor-create-modal";
|
||||||
|
import { renderWithProviders } from "@/test/test-utils";
|
||||||
|
|
||||||
|
vi.mock("@/domain/vendors/use-cases/use-vendor-facets", () => ({
|
||||||
|
useVendorFacets: () => ({ data: { companies: [], trades: [] }, isLoading: false }),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@/domain/vendors/use-cases/use-save-vendor-company-roster", async () => {
|
||||||
|
const actual = await vi.importActual<
|
||||||
|
typeof import("@/domain/vendors/use-cases/use-save-vendor-company-roster")
|
||||||
|
>("@/domain/vendors/use-cases/use-save-vendor-company-roster");
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
useSaveVendorCompanyRoster: () => ({ mutate: vi.fn(), isPending: false }),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("VendorCreateModal validation", () => {
|
||||||
|
it("explains the company phone-or-email requirement after submission", async () => {
|
||||||
|
renderWithProviders(<VendorCreateModal open={true} onClose={vi.fn()} />, {
|
||||||
|
route: "/vendors",
|
||||||
|
withAuth: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
fireEvent.change(screen.getByRole("combobox", { name: /Company/ }), {
|
||||||
|
target: { value: "Gateway Plumbing" },
|
||||||
|
});
|
||||||
|
await userEvent.click(screen.getByRole("button", { name: "Add Vendor" }));
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await screen.findByText("Provide a company phone or email (at least one required)"),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows the notes limit and live character counter", async () => {
|
||||||
|
renderWithProviders(<VendorCreateModal open={true} onClose={vi.fn()} />, {
|
||||||
|
route: "/vendors",
|
||||||
|
withAuth: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const notes = screen.getByRole("textbox", { name: "Notes (optional)" });
|
||||||
|
fireEvent.change(notes, { target: { value: "abc" } });
|
||||||
|
|
||||||
|
expect(notes).toHaveAttribute("maxlength", "500");
|
||||||
|
await waitFor(() =>
|
||||||
|
expect(notes).toHaveAccessibleDescription(/3\/500 characters · Maximum 500 characters/),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -51,6 +51,48 @@ function rosterWith(technicians: Array<Record<string, unknown>>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("VendorDetailDrawer selected-technician display", () => {
|
describe("VendorDetailDrawer selected-technician display", () => {
|
||||||
|
it("keeps Save changes available after removing a technician from a legacy roster", async () => {
|
||||||
|
const roster = rosterWith([
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
contactName: "Adam",
|
||||||
|
phone: "314-555-0198",
|
||||||
|
email: "",
|
||||||
|
tradeSpecialties: "",
|
||||||
|
isActive: true,
|
||||||
|
totalJobs: 5,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
contactName: "Beth",
|
||||||
|
phone: "314-555-0199",
|
||||||
|
email: "",
|
||||||
|
tradeSpecialties: "",
|
||||||
|
isActive: true,
|
||||||
|
totalJobs: 2,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
roster.data.companyPhone = "";
|
||||||
|
roster.data.email = "";
|
||||||
|
useVendorCompanyRoster.mockReturnValue(roster);
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<VendorDetailDrawer open={true} vendor={vendor} startInEdit={true} onClose={vi.fn()} />,
|
||||||
|
{ route: "/vendors", withAuth: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
await userEvent.click(screen.getByRole("button", { name: "Remove technician 2" }));
|
||||||
|
|
||||||
|
const save = screen.getByRole("button", { name: "Save changes" });
|
||||||
|
expect(save).toBeEnabled();
|
||||||
|
|
||||||
|
await userEvent.click(save);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await screen.findByText("Provide a company phone or email (at least one required)"),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
it("renders no preference label when preferredContact is absent", () => {
|
it("renders no preference label when preferredContact is absent", () => {
|
||||||
useVendorCompanyRoster.mockReturnValue(
|
useVendorCompanyRoster.mockReturnValue(
|
||||||
rosterWith([
|
rosterWith([
|
||||||
|
|
@ -137,6 +179,38 @@ describe("VendorDetailDrawer selected-technician display", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("VendorDetailDrawer design parity", () => {
|
describe("VendorDetailDrawer design parity", () => {
|
||||||
|
it("wraps long unbroken company notes inside the drawer", () => {
|
||||||
|
const roster = rosterWith([]);
|
||||||
|
roster.data.notes = "x".repeat(5000);
|
||||||
|
useVendorCompanyRoster.mockReturnValue(roster);
|
||||||
|
|
||||||
|
renderWithProviders(<VendorDetailDrawer open={true} vendor={vendor} onClose={vi.fn()} />, {
|
||||||
|
route: "/vendors",
|
||||||
|
withAuth: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const notes = screen.getByText("x".repeat(5000));
|
||||||
|
expect(notes).toHaveStyle({ overflowWrap: "anywhere", whiteSpace: "pre-wrap" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves an oversized legacy note in edit mode while showing the new limit", async () => {
|
||||||
|
const roster = rosterWith([]);
|
||||||
|
const legacyNotes = "x".repeat(501);
|
||||||
|
roster.data.notes = legacyNotes;
|
||||||
|
useVendorCompanyRoster.mockReturnValue(roster);
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<VendorDetailDrawer open={true} vendor={vendor} startInEdit={true} onClose={vi.fn()} />,
|
||||||
|
{ route: "/vendors", withAuth: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
const notes = await screen.findByRole("textbox", { name: "Notes (optional)" });
|
||||||
|
expect(notes).toHaveValue(legacyNotes);
|
||||||
|
expect(notes).toHaveAttribute("maxlength", "500");
|
||||||
|
expect(notes).toHaveStyle({ overflowWrap: "anywhere", whiteSpace: "pre-wrap" });
|
||||||
|
expect(screen.getByText(/501\/500 characters/)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
it("renders the company once when there is no technician name to head the panel", () => {
|
it("renders the company once when there is no technician name to head the panel", () => {
|
||||||
useVendorCompanyRoster.mockReturnValue(rosterWith([]));
|
useVendorCompanyRoster.mockReturnValue(rosterWith([]));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import {
|
||||||
emptyRosterTechnician,
|
emptyRosterTechnician,
|
||||||
emptyVendorCompanyRosterForm,
|
emptyVendorCompanyRosterForm,
|
||||||
isAbsoluteHttpsUrl,
|
isAbsoluteHttpsUrl,
|
||||||
|
VENDOR_NOTES_MAX_LENGTH,
|
||||||
vendorCompanyRosterSchema,
|
vendorCompanyRosterSchema,
|
||||||
vendorCompanyRosterUpdateSchema,
|
vendorCompanyRosterUpdateSchema,
|
||||||
} from "@/domain/vendors/schemas/vendor-roster-schema";
|
} from "@/domain/vendors/schemas/vendor-roster-schema";
|
||||||
|
|
@ -28,7 +29,9 @@ describe("vendorCompanyRosterSchema", () => {
|
||||||
expect(result.success).toBe(false);
|
expect(result.success).toBe(false);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
expect(
|
expect(
|
||||||
result.error.issues.some((issue) => issue.message === "Company phone or email is required"),
|
result.error.issues.some(
|
||||||
|
(issue) => issue.message === "Provide a company phone or email (at least one required)",
|
||||||
|
),
|
||||||
).toBe(true);
|
).toBe(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -54,6 +57,20 @@ describe("vendorCompanyRosterSchema", () => {
|
||||||
expect(result.success).toBe(true);
|
expect(result.success).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("accepts exactly 500 note characters and rejects 501", () => {
|
||||||
|
const atLimit = vendorCompanyRosterSchema.safeParse({
|
||||||
|
...baseCompany,
|
||||||
|
notes: "x".repeat(VENDOR_NOTES_MAX_LENGTH),
|
||||||
|
});
|
||||||
|
const overLimit = vendorCompanyRosterSchema.safeParse({
|
||||||
|
...baseCompany,
|
||||||
|
notes: "x".repeat(VENDOR_NOTES_MAX_LENGTH + 1),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(atLimit.success).toBe(true);
|
||||||
|
expect(overLimit.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it("accepts a technician row without a name or phone", () => {
|
it("accepts a technician row without a name or phone", () => {
|
||||||
const result = vendorCompanyRosterSchema.safeParse({
|
const result = vendorCompanyRosterSchema.safeParse({
|
||||||
...baseCompany,
|
...baseCompany,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue