import { apiGet, apiPost } from "@/app/lib/clientApi"; import type { AdminSponsorHistoryPage, AdminTreeNode, } from "@/app/lib/types"; // Client over the BFF route handlers (app/api/admin/network**, app/api/admin/members**): // the real network tree, upline chain, placement history and placement correction. /** Tree rooted at {@code memberCode}, populated down to {@code level} (large = whole tree). */ export async function getNetworkTree(memberCode: string, level = 99): Promise { return apiGet(`/api/admin/network/${encodeURIComponent(memberCode)}?level=${level}`); } /** The member's upline chain (ancestors up to just below the ROOT). */ export async function getMemberUpline(memberCode: string): Promise { return apiGet(`/api/admin/network/${encodeURIComponent(memberCode)}/upline`); } /** Dated placement (sponsor) history for the member. */ export async function getPlacementHistory(memberCode: string): Promise { return apiGet( `/api/admin/members/${encodeURIComponent(memberCode)}/placement-history`, ); } /** * Referral (placement) correction: reassign the member's sponsor, with the admin's reason * (BABYBOO-43). The server cycle-guards the move (checkIsDownline: the new sponsor may not sit * in the member's own downline; a move up to an ancestor is allowed) and derives the actor from * the JWT — this client never sends one. A rejected move surfaces as ApiError status 400 with * the message. */ export async function changeSponsor(memberCode: string, newSponsorCode: string, reason: string): Promise { await apiPost(`/api/admin/members/${encodeURIComponent(memberCode)}/change-sponsor`, { newSponsorCode, reason, }); } /** Direct downline count of a node (its children). */ export function directDownline(node: AdminTreeNode): number { return node.children?.length ?? 0; } /** Total downline count of a node: everyone beneath it at any depth (derived from the tree). */ export function totalDownline(node: AdminTreeNode): number { const children = node.children ?? []; return children.reduce((acc, child) => acc + 1 + totalDownline(child), 0); }