feat(delivery-notes): filter list to the logged-in rep's own notes
This commit is contained in:
parent
29eb003cd6
commit
ed07617827
2 changed files with 21 additions and 4 deletions
|
|
@ -27,6 +27,8 @@ import {
|
||||||
getDeliveryNotes,
|
getDeliveryNotes,
|
||||||
getQuotesForDeliveryNotes,
|
getQuotesForDeliveryNotes,
|
||||||
} from "@/lib/pb-helpers";
|
} from "@/lib/pb-helpers";
|
||||||
|
import { useAuth } from "@/components/auth-provider";
|
||||||
|
import { isRepUser } from "@/lib/rep-visibility";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import {
|
import {
|
||||||
Banknote,
|
Banknote,
|
||||||
|
|
@ -110,6 +112,7 @@ const statusLabel: Record<DeliveryNoteStatus, string> = {
|
||||||
|
|
||||||
export default function DeliveryNotesPage() {
|
export default function DeliveryNotesPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { user } = useAuth();
|
||||||
const [notes, setNotes] = useState<DeliveryNote[]>([]);
|
const [notes, setNotes] = useState<DeliveryNote[]>([]);
|
||||||
// Map of delivery_note.id → the most-recent quote that prices
|
// 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
|
// it. Used by the Q icon on the list page so we can show
|
||||||
|
|
@ -319,13 +322,15 @@ export default function DeliveryNotesPage() {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError("");
|
setError("");
|
||||||
try {
|
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
|
// Fetch a wide page so the client-side tab filter sees the
|
||||||
// full set — the previous `perPage=20` made the active
|
// full set — the previous `perPage=20` made the active
|
||||||
// tab silently drop records past rank 20 (the operator
|
// tab silently drop records past rank 20 (the operator
|
||||||
// saw "no DN" when the row existed but was buried). 500
|
// saw "no DN" when the row existed but was buried). 500
|
||||||
// is well over the production DN count (≈30) and still
|
// is well over the production DN count (≈30) and still
|
||||||
// cheap for PB.
|
// cheap for PB.
|
||||||
const result = await getDeliveryNotes(page, 500, search);
|
const result = await getDeliveryNotes(page, 500, search, repId);
|
||||||
setNotes(result.items);
|
setNotes(result.items);
|
||||||
setTotalPages(result.totalPages);
|
setTotalPages(result.totalPages);
|
||||||
// Batch-fetch the related quotes in one query so the Q
|
// Batch-fetch the related quotes in one query so the Q
|
||||||
|
|
@ -349,7 +354,7 @@ export default function DeliveryNotesPage() {
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}, [page, search]);
|
}, [page, search, user]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchNotes();
|
fetchNotes();
|
||||||
|
|
|
||||||
|
|
@ -1065,9 +1065,21 @@ export async function updateCollectionPoolStatus(
|
||||||
return updated;
|
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 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<DeliveryNote>(page, perPage, {
|
return pb.collection("delivery_notes").getList<DeliveryNote>(page, perPage, {
|
||||||
filter,
|
filter,
|
||||||
sort: "-created",
|
sort: "-created",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue