From d23c811b9104363b5157b4a436be0f295d8f5514 Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 31 Aug 2026 23:24:25 +0200 Subject: [PATCH] feat: add isRepUser helper for rep-scoped list filtering --- src/lib/rep-visibility.test.ts | 31 +++++++++++++++++++++++++++++++ src/lib/rep-visibility.ts | 13 +++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/lib/rep-visibility.test.ts create mode 100644 src/lib/rep-visibility.ts diff --git a/src/lib/rep-visibility.test.ts b/src/lib/rep-visibility.test.ts new file mode 100644 index 0000000..58dcbef --- /dev/null +++ b/src/lib/rep-visibility.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "vitest"; +import { isRepUser } from "./rep-visibility"; + +describe("isRepUser", () => { + it("returns true when user_type includes REP", () => { + expect(isRepUser(["REP"])).toBe(true); + }); + + it("is case-insensitive", () => { + expect(isRepUser(["rep"])).toBe(true); + }); + + it("returns true when REP is combined with other roles", () => { + expect(isRepUser(["BUYER", "REP"])).toBe(true); + }); + + it("returns false for other roles", () => { + expect(isRepUser(["BUYER"])).toBe(false); + expect(isRepUser(["ADMIN", "QUOTER"])).toBe(false); + }); + + it("returns false for null, undefined, or non-array input", () => { + expect(isRepUser(null)).toBe(false); + expect(isRepUser(undefined)).toBe(false); + expect(isRepUser("REP")).toBe(false); + }); + + it("returns false for an empty array", () => { + expect(isRepUser([])).toBe(false); + }); +}); diff --git a/src/lib/rep-visibility.ts b/src/lib/rep-visibility.ts new file mode 100644 index 0000000..d3c86aa --- /dev/null +++ b/src/lib/rep-visibility.ts @@ -0,0 +1,13 @@ +/** + * `users.user_type` is a PB multi-select. "REP" is the existing + * convention for marking a sales rep — see the identical pattern in + * `src/lib/server/tv-dashboard-service.ts` (`TV_REP_ROLES` / `isRep`). + */ +export const REP_USER_TYPE = "REP"; + +export function isRepUser(userType: unknown): boolean { + if (!Array.isArray(userType)) return false; + return userType.some( + (value) => typeof value === "string" && value.toUpperCase() === REP_USER_TYPE, + ); +}