This commit is contained in:
Shubhankar45
2026-10-27 12:03:31 +00:00
parent 2ed3945b5d
commit 4e5e95775d
104 changed files with 5210 additions and 606 deletions
+17 -2
View File
@@ -1,11 +1,26 @@
import { isField } from "./format";
import type { AreaManagerMapping, Headquarters, User } from "../types";
/** Headquarters a person works in: an MR has one, an AM has any number, GM and Admin cover all. */
export function headquartersOf(user: User, hqs: Headquarters[], mappings: AreaManagerMapping[]): { all: boolean; items: Headquarters[] } {
if (user.role === "GM" || user.role === "ADMIN") return { all: true, items: [] };
const ids = user.role === "MR"
? (user.headquarters_id ? [user.headquarters_id] : [])
const ids = isField(user.role)
? [...(user.headquarters_id ? [user.headquarters_id] : []), ...(user.extra_headquarter_ids ?? [])]
: mappings.filter((m) => m.area_manager_user_id === user.id).map((m) => m.headquarters_id);
const wanted = new Set(ids);
return { all: false, items: hqs.filter((h) => wanted.has(h.id)) };
}
/**
* The Area Managers of each headquarters, by name ("Asha Rao, Ravi Das"). A headquarters may
* have several. `except` leaves one Area Manager out, e.g. the one whose headquarters are being edited.
*/
export function areaManagersOf(mappings: AreaManagerMapping[], users: User[], except?: string): Record<string, string> {
const name = new Map(users.map((u) => [u.id, u.full_name]));
const byHq = new Map<string, string[]>();
for (const m of mappings) {
if (m.area_manager_user_id === except) continue;
byHq.set(m.headquarters_id, [...(byHq.get(m.headquarters_id) ?? []), name.get(m.area_manager_user_id) ?? "an Area Manager"]);
}
return Object.fromEntries([...byHq].map(([hq, names]) => [hq, names.sort().join(", ")]));
}