All files / services BeneficiaryDetailController.ts

0% Statements 0/34
0% Branches 0/32
0% Functions 0/6
0% Lines 0/30

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                                                                                                                                                           
import type { Beneficiary } from '@/types';
import { isSubscriptionActive, isSubscriptionExpiringSoon } from '@/services/subscription';
 
export type BeneficiarySetupState =
  | 'loading'
  | 'awaiting_equipment'
  | 'no_devices'
  | 'no_subscription'
  | 'ready';
 
export const hasBeneficiaryDevices = (beneficiary: Beneficiary): boolean => {
  return Boolean(
    beneficiary.hasDevices ||
    (beneficiary.devices && beneficiary.devices.length > 0) ||
    beneficiary.device_id
  );
};
 
export const hasActiveSubscription = (beneficiary: Beneficiary): boolean => {
  return isSubscriptionActive(beneficiary.subscription);
};
 
export const getBeneficiarySetupState = (
  beneficiary: Beneficiary | null,
  isLoading: boolean
): BeneficiarySetupState => {
  if (isLoading || !beneficiary) return 'loading';
 
  const hasDevices = hasBeneficiaryDevices(beneficiary);
  const equipmentStatus = beneficiary.equipmentStatus;
 
  // Waterfall priority:
  // 1. First check devices (highest priority)
  if (!hasDevices) {
    // Equipment is on the way - waiting for delivery
    if (equipmentStatus && ['ordered', 'shipped', 'delivered'].includes(equipmentStatus)) {
      return 'awaiting_equipment';
    }
    // No devices and no order - need to buy equipment
    return 'no_devices';
  }
 
  // 2. Then check subscription (only if devices exist)
  if (!isSubscriptionActive(beneficiary.subscription)) {
    return 'no_subscription';
  }
 
  // 3. All good → ready
  return 'ready';
};
 
export const shouldShowSubscriptionWarning = (beneficiary: Beneficiary | null): boolean => {
  return beneficiary ? isSubscriptionExpiringSoon(beneficiary.subscription) : false;
};
 
export type BeneficiaryRouteKey = 'index' | 'subscription' | 'equipment' | 'share';
 
export const getDefaultBeneficiaryRouteKey = (
  beneficiary: Beneficiary | null,
  isLoading: boolean
): BeneficiaryRouteKey | null => {
  const state = getBeneficiarySetupState(beneficiary, isLoading);
  if (state === 'loading') return null;
  if (state === 'no_subscription') return 'subscription';
  return 'index';
};
 
export const getRedirectRouteKey = (
  currentRouteKey: BeneficiaryRouteKey,
  beneficiary: Beneficiary | null,
  isLoading: boolean
): BeneficiaryRouteKey | null => {
  const state = getBeneficiarySetupState(beneficiary, isLoading);
  if (state === 'loading') return null;
 
  if (currentRouteKey === 'index' && state === 'no_subscription') {
    return 'subscription';
  }
 
  if (currentRouteKey === 'subscription' && state !== 'no_subscription') {
    return 'index';
  }
 
  return null;
};