import AsyncStorage from '@react-native-async-storage/async-storage'; import type { Beneficiary } from '../../../types/app'; import type { CbsBeneficiary } from '../../../types/cbs'; const FAVORITES_KEY = 'bankcbs.beneficiaryFavorites'; /** * The API has no favourite flag — starring a payee is a local preference. Kept * as a set of beneficiary ids in AsyncStorage: losing it costs the customer a * tap, so it does not warrant the Keystore. */ export const favoriteStore = { async read(): Promise { try { const raw = await AsyncStorage.getItem(FAVORITES_KEY); const parsed = raw ? JSON.parse(raw) : []; return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === 'string') : []; } catch { return []; } }, async write(ids: string[]) { try { await AsyncStorage.setItem(FAVORITES_KEY, JSON.stringify(ids)); } catch { // A failed preference write is not worth surfacing to the customer. } }, }; /** * Maps a payee as the bank returned it onto the shape the payments screens * already read. `validate` leaves `beneficiaryId` null because nothing has been * saved yet, so an unsaved payee gets a stable synthetic key from its routing * details rather than a fresh one on every keystroke. */ export const toBeneficiary = ( source: CbsBeneficiary, favorites: ReadonlySet = new Set(), ): Beneficiary => { const id = source.beneficiaryId ?? `draft:${source.mode}:${source.accountNumberMasked}:${source.mmid ?? source.ifsc ?? ''}`; return { id, name: source.name, bankName: source.bankName, accountMasked: source.accountNumberMasked, nickname: source.nickname ?? undefined, favorite: favorites.has(id), mode: source.mode, ifsc: source.ifsc, mobileNumber: source.mobileNumber, mmid: source.mmid, status: source.status, }; };