Files
baya-monorepo/client/src/services/nurse/apis/mockApi.ts
T
hamid 4b4243c451 frontend phase 2: onboarding & profiles — customer/patient, nurse profile & bank
Turns a logged-in user into a usable account, consuming the b3 identity-profiles
contract behind the services/{domain} seam.

Services (mock default true; real HTTP clients wired for a one-line flip):
- services/patients: rewritten to b3 PatientDto + client-augmented relation/conditions;
  full CRUD, optimistic soft-archive, cache-splice on create, age<->birthDate helper.
- services/profiles: customer + nurse profile get/upsert + avatar (404->null mapping).
- services/nurse: payout bank accounts + IBAN(Sheba) util + pending-only polling.

Screens: A3->A4 onboarding wizard, E1 patients list/CRUD, A5 home (first-login gate +
nudge), customer profile (no national-ID), nurse profile bootstrap (unverified
placeholder), nurse bank settings (pending/verified/mismatch + make-primary).

Shared composites (each tested): GenderToggle, ConditionChips, RelationSelect,
PatientForm, PatientCard, BankStatusPanel; reuses f0 StepperHeader/StatusChip/PhoneField.
Adds onboarding/home/profile/nurseProfile/bank i18n namespaces (both locales, in sync),
the --bal-primary-soft token, and nurse sidebar Profile + Bank entries.

Contract gaps filed: REQ-005 (patient relation/conditions), REQ-006 (avatar route),
REQ-007 (customer name/language). Gate: check + 112 tests + build all green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 22:04:38 +03:30

82 lines
3.2 KiB
TypeScript

import { sleep } from '@/utils';
import { ApiError } from '@/lib/api/errors';
import { KNOWN_MISMATCH_IBAN } from '../constants';
import { normalizeSheba, shebaBankName } from '../iban';
import type { AddBankAccountInput, NurseBankAccountDto, NurseBankAccountsApi } from '../types';
const MOCK_LATENCY_MS = 400;
// The number of list reads the inquiry stays pending before resolving. 2 lets the pending
// panel show on the invalidation-triggered read, then flip on the next poll — so the
// pending→verified/mismatch transition is visible without a manual reload.
const POLLS_BEFORE_RESOLVE = 2;
interface StoredAccount {
dto: NurseBankAccountDto;
iban: string; // normalized full value — mock-only; the real value is encrypted server-side
pollsLeft: number;
}
let store: StoredAccount[] = [];
let nextId = 1;
function maskIban(normalized: string): string {
return `••••${normalized.slice(-4)}`;
}
// Deterministic fake استعلام شبا: every IBAN matches except the configured mismatch IBAN.
function resolveIfDue(entry: StoredAccount): void {
if (entry.dto.matchedNationalId !== null || entry.pollsLeft <= 0) return;
entry.pollsLeft -= 1;
if (entry.pollsLeft > 0) return;
const matched = normalizeSheba(entry.iban) !== normalizeSheba(KNOWN_MISMATCH_IBAN);
entry.dto = { ...entry.dto, matchedNationalId: matched, isVerified: matched };
}
/**
* In-memory mock behind the NurseBankAccountsApi seam. Drives the pending→verified/mismatch
* transition and single-primary enforcement so all three UI states are demonstrable. Mirrors
* the real shapes (masked IBAN, `matchedNationalId` gate) for a one-line swap.
*/
export const nurseBankMockApi: NurseBankAccountsApi = {
list: async (): Promise<NurseBankAccountDto[]> => {
await sleep(MOCK_LATENCY_MS);
store.forEach(resolveIfDue);
return store.map((entry) => entry.dto);
},
add: async (input: AddBankAccountInput): Promise<NurseBankAccountDto> => {
await sleep(MOCK_LATENCY_MS);
const normalized = normalizeSheba(input.iban);
if (store.some((entry) => entry.iban === normalized)) {
throw new ApiError(400, 'Duplicate IBAN', 'iban_duplicate');
}
const dto: NurseBankAccountDto = {
id: nextId++,
bankName: shebaBankName(normalized),
ibanMasked: maskIban(normalized),
isPrimary: store.length === 0,
isVerified: false,
matchedNationalId: null,
};
store = [...store, { dto, iban: normalized, pollsLeft: POLLS_BEFORE_RESOLVE }];
return dto;
},
setPrimary: async (id: number): Promise<void> => {
await sleep(MOCK_LATENCY_MS);
if (!store.some((entry) => entry.dto.id === id)) throw new ApiError(404, 'Account not found');
store = store.map((entry) => ({ ...entry, dto: { ...entry.dto, isPrimary: entry.dto.id === id } }));
},
verifyOwnership: async (id: number): Promise<NurseBankAccountDto> => {
await sleep(MOCK_LATENCY_MS);
const entry = store.find((item) => item.dto.id === id);
if (!entry) throw new ApiError(404, 'Account not found');
const matched = normalizeSheba(entry.iban) !== normalizeSheba(KNOWN_MISMATCH_IBAN);
entry.dto = { ...entry.dto, matchedNationalId: matched, isVerified: matched };
entry.pollsLeft = 0;
return entry.dto;
},
};