All files / src/features/Settings/Team/hooks useTeamMemberEmailOptions.ts

100% Statements 7/7
72.72% Branches 8/11
100% Functions 4/4
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                      2x 146x     2x                       146x 9x 8x         146x        
import { useQuery } from "react-query";
import { customInstance } from "@services/api";
import { GENERAL_STALE_TIME } from "@features/Merchants/MerchantSidePanel/constants";
import { QKEY_TEAM_MEMBER_EMAIL_OPTIONS } from "@constants/queryKeys";
import { MemberSuggestion } from "../helpers/memberSuggestions";
 
/**
 * GB-21471 — fetch the merchant's team members as {name,email} options to
 * suggest while the PAH types a new owner email in the Change PAH modal. The
 * current owner is excluded (a PAH can't be reassigned to themselves).
 */
const useTeamMemberEmailOptions = (merchantId: number, enabled = true) => {
  const { data, isLoading } = useQuery(
    [QKEY_TEAM_MEMBER_EMAIL_OPTIONS, merchantId],
    () =>
      customInstance({
        url: `/accounts/${merchantId}/members`,
        method: "GET",
      }),
    {
      staleTime: GENERAL_STALE_TIME,
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      enabled: enabled && Boolean(merchantId),
    },
  );
 
  const options: MemberSuggestion[] = (data?.data ?? [])
    .filter((m: any) => m?.roleName !== "owner" && m?.user?.email)
    .map((m: any) => ({
      email: m.user.email,
      name: `${m.user.firstName ?? ""} ${m.user.lastName ?? ""}`.trim(),
    }));
 
  return { options, isLoading };
};
 
export default useTeamMemberEmailOptions;