diff --git a/src/app/(app)/delivery-notes/page.tsx b/src/app/(app)/delivery-notes/page.tsx index 460171b..ca87751 100644 --- a/src/app/(app)/delivery-notes/page.tsx +++ b/src/app/(app)/delivery-notes/page.tsx @@ -27,6 +27,8 @@ import { getDeliveryNotes, getQuotesForDeliveryNotes, } from "@/lib/pb-helpers"; +import { useAuth } from "@/components/auth-provider"; +import { isRepUser } from "@/lib/rep-visibility"; import { toast } from "sonner"; import { Banknote, @@ -110,6 +112,7 @@ const statusLabel: Record = { export default function DeliveryNotesPage() { const router = useRouter(); + const { user } = useAuth(); const [notes, setNotes] = useState([]); // Map of delivery_note.id → the most-recent quote that prices // it. Used by the Q icon on the list page so we can show @@ -319,13 +322,15 @@ export default function DeliveryNotesPage() { setLoading(true); setError(""); try { + const userType = (user as { user_type?: string[] } | null)?.user_type; + const repId = user && isRepUser(userType) ? user.id : null; // Fetch a wide page so the client-side tab filter sees the // full set — the previous `perPage=20` made the active // tab silently drop records past rank 20 (the operator // saw "no DN" when the row existed but was buried). 500 // is well over the production DN count (≈30) and still // cheap for PB. - const result = await getDeliveryNotes(page, 500, search); + const result = await getDeliveryNotes(page, 500, search, repId); setNotes(result.items); setTotalPages(result.totalPages); // Batch-fetch the related quotes in one query so the Q @@ -349,7 +354,7 @@ export default function DeliveryNotesPage() { } finally { setLoading(false); } - }, [page, search]); + }, [page, search, user]); useEffect(() => { fetchNotes(); diff --git a/src/lib/pb-helpers.ts b/src/lib/pb-helpers.ts index ef46007..c6a3586 100644 --- a/src/lib/pb-helpers.ts +++ b/src/lib/pb-helpers.ts @@ -1065,9 +1065,21 @@ export async function updateCollectionPoolStatus( return updated; } -export async function getDeliveryNotes(page = 1, perPage = 50, search = "") { +export async function getDeliveryNotes( + page = 1, + perPage = 50, + search = "", + repId: string | null = null, +) { const pb = getPocketBase(); - const filter = search ? `delivery_note_number ~ "${search}" || notes ~ "${search}"` : ""; + const clauses: string[] = []; + if (search) { + clauses.push(`(delivery_note_number ~ "${search}" || notes ~ "${search}")`); + } + if (repId) { + clauses.push(`customer.rep = "${repId.replace(/"/g, '\\"')}"`); + } + const filter = clauses.join(" && "); return pb.collection("delivery_notes").getList(page, perPage, { filter, sort: "-created",