Admin-side booking (POST /api/v1/my/appointment and /api/v1/admin/appointment) resolved the patient User by mobile only, so one person booked under two mobiles produced two User rows — and two case-files, since PatientRecord is keyed on user_id. National code is the real unique identity (User.national_code is already unique); a person may have several mobiles. Booking now requires + validates patient_national_code and resolves the patient national-code-first (then mobile) via a shared PatientResolver, so the case-file stays unique per national code even across mobiles. Reusing a mobile already bound to a different national code returns 422 ERR_PROFILE_MOBILE_TAKEN. The admin create form and NewAppointmentDrawer gain a national-code field and send it; both had a dead patient-picker URL (/api/v1/patient) fixed to the real /api/v1/patients, whose payload already carries user_national_code for autofill. Docs (appointment.md, admin.md) and tests updated; new AppointmentNationalCodeTest covers success, single-file reuse, missing, invalid, and identity-conflict cases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
57 lines
3.0 KiB
TypeScript
57 lines
3.0 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { screen, fireEvent, waitFor } from '@testing-library/react';
|
|
import { renderWithProviders } 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 { useAuthStore } from '../stores/authStore';
|
|
import AppointmentCreatePage from './AppointmentCreatePage';
|
|
|
|
const get = api.get as ReturnType<typeof vi.fn>;
|
|
const post = api.post as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
get.mockReset();
|
|
post.mockReset();
|
|
useAuthStore.setState({ primaryRole: 'doctor', dbUuid: 'doc1' } as any);
|
|
get.mockResolvedValue({ success: true, data: [] });
|
|
post.mockResolvedValue({ success: true, data: { uuid: 'new1' } });
|
|
});
|
|
|
|
describe('AppointmentCreatePage — افزودن نوبت', () => {
|
|
it('posts a new appointment with the entered patient (happy path)', async () => {
|
|
renderWithProviders(<AppointmentCreatePage />, { route: '/admin/appointments/new' });
|
|
|
|
fireEvent.change(screen.getByPlaceholderText('نام و نام خانوادگی مراجعه کننده'), { target: { value: 'علی محمدی' } });
|
|
fireEvent.change(screen.getByPlaceholderText('شماره تماس مراجعه کننده'), { target: { value: '09121234567' } });
|
|
fireEvent.change(screen.getByPlaceholderText('کد ملی مراجعه کننده'), { target: { value: '1234567891' } });
|
|
|
|
fireEvent.click(screen.getByText('ثبت اطلاعات'));
|
|
|
|
await waitFor(() => expect(post).toHaveBeenCalled());
|
|
const [url, body] = post.mock.calls[0];
|
|
expect(url).toBe('/api/v1/my/appointment');
|
|
expect(body).toMatchObject({ doctor_uuid: 'doc1', patient_name: 'علی محمدی', patient_mobile: '09121234567', patient_national_code: '1234567891' });
|
|
});
|
|
|
|
it('keeps the submit button disabled until a valid patient is entered (boundary)', () => {
|
|
renderWithProviders(<AppointmentCreatePage />, { route: '/admin/appointments/new' });
|
|
const btn = screen.getByText('ثبت اطلاعات') as HTMLButtonElement;
|
|
expect(btn).toBeDisabled();
|
|
|
|
// شماره ناقص → همچنان غیرفعال
|
|
fireEvent.change(screen.getByPlaceholderText('نام و نام خانوادگی مراجعه کننده'), { target: { value: 'ب' } });
|
|
fireEvent.change(screen.getByPlaceholderText('شماره تماس مراجعه کننده'), { target: { value: '0912' } });
|
|
expect(btn).toBeDisabled();
|
|
|
|
// نام و موبایل معتبر ولی کد ملی خالی → همچنان غیرفعال (کد ملی الزامی است)
|
|
fireEvent.change(screen.getByPlaceholderText('نام و نام خانوادگی مراجعه کننده'), { target: { value: 'علی محمدی' } });
|
|
fireEvent.change(screen.getByPlaceholderText('شماره تماس مراجعه کننده'), { target: { value: '09121234567' } });
|
|
expect(btn).toBeDisabled();
|
|
});
|
|
});
|