--
This commit is contained in:
@@ -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(", ")]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user