feat(patient): complete "اطلاعات پرونده" demographic form
Backend:
- Add profile columns field_of_study, province_id, city_id, postal_code,
referral_source (UserProfile + migration).
- Extend PATCH /api/v1/patient/{uuid} to persist all demographic fields
and return them in the patient profile payload.
- Support editable mobile (login identifier): validation, uniqueness,
User.setMobileNumber, new ERR_PROFILE_002.
- Update docs/api/patient.md.
Frontend:
- New reusable Input, Field, and PatientRecordInfoForm (RHF + Zod).
- usePatient/useUpdatePatient hooks and patientForm mapping helpers.
- Extend the existing "info" tab in MyPatientsPage to the full field set
via the shared form (province/city/insurance options, Jalali date).
Tests: Patient entity + PATCH integration (PHPUnit); form, hooks, and
mapping helpers (Vitest).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { waitFor } from '@testing-library/react';
|
||||
import { renderHookWithClient } from '@/test/utils';
|
||||
|
||||
vi.mock('@/lib/api', () => ({
|
||||
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
|
||||
ApiError: class extends Error {},
|
||||
}));
|
||||
|
||||
import { api } from '@/lib/api';
|
||||
import { usePatient, useUpdatePatient } from '@/hooks/usePatient';
|
||||
|
||||
const get = api.get as ReturnType<typeof vi.fn>;
|
||||
const patch = api.patch as ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
get.mockReset();
|
||||
patch.mockReset();
|
||||
});
|
||||
|
||||
describe('usePatient', () => {
|
||||
it('پروفایل را از data.data.profile استخراج میکند', async () => {
|
||||
get.mockResolvedValue({
|
||||
data: { uuid: 'u-1', profile: { name: 'ساغر', national_code: '0012345678', city_id: 42 } },
|
||||
});
|
||||
|
||||
const { result } = renderHookWithClient(() => usePatient('u-1'));
|
||||
await waitFor(() => expect(result.current.profile).not.toBeNull());
|
||||
|
||||
expect(get).toHaveBeenCalledWith('/api/v1/patient/u-1');
|
||||
expect(result.current.profile?.name).toBe('ساغر');
|
||||
expect(result.current.profile?.city_id).toBe(42);
|
||||
expect(result.current.record?.uuid).toBe('u-1');
|
||||
});
|
||||
|
||||
it('بدون uuid کوئری اجرا نمیشود', () => {
|
||||
const { result } = renderHookWithClient(() => usePatient(undefined));
|
||||
expect(get).not.toHaveBeenCalled();
|
||||
expect(result.current.profile).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('useUpdatePatient', () => {
|
||||
it('PATCH را با بدنه به آدرس بیمار میفرستد', async () => {
|
||||
patch.mockResolvedValue({ data: { uuid: 'u-1', profile: {} } });
|
||||
|
||||
const { result } = renderHookWithClient(() => useUpdatePatient('u-1'));
|
||||
result.current.mutate({ fathers_name: 'رضا', city_id: 42 });
|
||||
|
||||
await waitFor(() => expect(patch).toHaveBeenCalled());
|
||||
expect(patch).toHaveBeenCalledWith('/api/v1/patient/u-1', { fathers_name: 'رضا', city_id: 42 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { api } from '../lib/api';
|
||||
import type { ApiResponse } from '../lib/api';
|
||||
import type { PatientRecord, PatientProfileUpdate } from '../types';
|
||||
|
||||
/**
|
||||
* دریافت یک پروندهٔ بیمار بههمراه پروفایل کامل (`profile`).
|
||||
* منبع: GET /api/v1/patient/{uuid}. کلید کوئری: ['patient', uuid].
|
||||
*/
|
||||
export function usePatient(uuid: string | undefined) {
|
||||
const query = useQuery<ApiResponse<PatientRecord>>({
|
||||
queryKey: ['patient', uuid],
|
||||
queryFn: () => api.get(`/api/v1/patient/${uuid}`),
|
||||
enabled: !!uuid,
|
||||
});
|
||||
|
||||
const record = query.data?.data ?? null;
|
||||
|
||||
return {
|
||||
record,
|
||||
profile: record?.profile ?? null,
|
||||
isLoading: query.isLoading,
|
||||
isError: query.isError,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* بهروزرسانی partial پروفایل بیمار.
|
||||
* منبع: PATCH /api/v1/patient/{uuid}. پس از موفقیت، کوئری همان بیمار را invalidate میکند.
|
||||
*/
|
||||
export function useUpdatePatient(uuid: string) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation<ApiResponse<PatientRecord>, unknown, PatientProfileUpdate>({
|
||||
mutationFn: (body) => api.patch(`/api/v1/patient/${uuid}`, body),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['patient', uuid] });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user