feat: add TourProgressController and related entities for user tour progress tracking
- Implemented TourProgressController to handle API endpoints for tracking guided tours seen by users. - Created UserTourProgress entity to store the highest version of tours seen by each user. - Developed UserTourProgressRepository for database interactions related to user tour progress. - Introduced TourProgressService to manage business logic for marking tours as seen and retrieving seen maps. - Added comprehensive tests for API endpoints and entity behavior to ensure functionality and data integrity.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { screen, 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 {},
|
||||
}));
|
||||
|
||||
const drive = vi.fn();
|
||||
vi.mock('driver.js', () => ({ driver: vi.fn(() => ({ drive })) }));
|
||||
vi.mock('driver.js/dist/driver.css', () => ({}));
|
||||
|
||||
import { driver } from 'driver.js';
|
||||
import { api } from '../lib/api';
|
||||
import { useAuthStore } from '../stores/authStore';
|
||||
import AppointmentsPage from './AppointmentsPage';
|
||||
import { appointmentsTour } from '../lib/tour/tours/appointments';
|
||||
import { resolveSteps } from '../lib/tour/resolveSteps';
|
||||
|
||||
const get = api.get as ReturnType<typeof vi.fn>;
|
||||
const driverMock = driver as unknown as ReturnType<typeof vi.fn>;
|
||||
|
||||
/** پاسخهای ثابت صفحه؛ فقط وضعیت تور بین تستها فرق میکند. */
|
||||
function mockApi(seen: Record<string, number>) {
|
||||
get.mockImplementation((url: string) => {
|
||||
if (url.includes('/my/tours')) return Promise.resolve({ success: true, data: { seen } });
|
||||
if (url.includes('today-stats')) return Promise.resolve({ success: true, data: { total: 7, completed: 3, waiting: 2, cancelled: 1 } });
|
||||
if (url.includes('appointment-slots')) return Promise.resolve({ success: true, data: { sessions: [], empty_reason: 'holiday' } });
|
||||
if (url.includes('/my/appointments')) return Promise.resolve({ success: true, data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } });
|
||||
return Promise.resolve({ success: true, data: [] });
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
get.mockReset();
|
||||
drive.mockReset();
|
||||
driverMock.mockClear();
|
||||
useAuthStore.setState({ primaryRole: 'doctor', dbUuid: 'doc1', doctorUuid: 'doc1' } as any);
|
||||
});
|
||||
|
||||
describe('AppointmentsPage — راهنمای صفحه', () => {
|
||||
it('دکمهٔ راهنما کنار عنوان است', async () => {
|
||||
mockApi({});
|
||||
renderWithProviders(<AppointmentsPage />);
|
||||
|
||||
expect(await screen.findByRole('button', { name: 'راهنمای این صفحه' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('برای کاربری که تور را ندیده، خودکار اجرا میشود', async () => {
|
||||
mockApi({});
|
||||
renderWithProviders(<AppointmentsPage />);
|
||||
|
||||
await waitFor(() => expect(drive).toHaveBeenCalledTimes(1), { timeout: 3000 });
|
||||
});
|
||||
|
||||
it('برای کاربری که تور را دیده، خودکار اجرا نمیشود', async () => {
|
||||
mockApi({ appointments: appointmentsTour.version });
|
||||
renderWithProviders(<AppointmentsPage />);
|
||||
|
||||
await screen.findByText('این روز تعطیل است');
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
|
||||
expect(drive).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('پزشک مستقل تب پزشکان ندارد، پس آن استپ از تور حذف میشود', async () => {
|
||||
mockApi({});
|
||||
renderWithProviders(<AppointmentsPage />);
|
||||
|
||||
await waitFor(() => expect(drive).toHaveBeenCalled(), { timeout: 3000 });
|
||||
|
||||
const anchors = driverMock.mock.calls[0][0].steps.map((s: { element: string }) => s.element);
|
||||
expect(anchors).not.toContain('[data-tour="appointments-doctors"]');
|
||||
expect(anchors).toContain('[data-tour="appointments-stats"]');
|
||||
// شمارندهٔ تور همان تعداد استپِ واقعاً موجود است، نه کل تعریف
|
||||
expect(anchors).toHaveLength(resolveSteps(appointmentsTour.steps).length);
|
||||
expect(anchors.length).toBeLessThan(appointmentsTour.steps.length);
|
||||
});
|
||||
});
|
||||
@@ -28,6 +28,8 @@ import { useUrlState } from '../hooks/useUrlState';
|
||||
import TurnsTimeline from '../components/appointments/TurnsTimeline';
|
||||
import TurnsTable from '../components/appointments/TurnsTable';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { useTour } from '../hooks/useTour';
|
||||
import TourButton from '../components/ui/TourButton';
|
||||
import NewAppointmentModal from '../components/appointments/NewAppointmentModal';
|
||||
import type { BookingSlot } from '../components/appointments/NewAppointmentModal';
|
||||
import { useDoctorBookingServices } from '../hooks/useDoctorBookingServices';
|
||||
@@ -68,7 +70,7 @@ function DateNavigator({ date, onChange }: { date: string; onChange: (d: string)
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={ref} style={{ display: 'flex', alignItems: 'center', gap: 2, position: 'relative' }}>
|
||||
<div ref={ref} data-tour="appointments-date" style={{ display: 'flex', alignItems: 'center', gap: 2, position: 'relative' }}>
|
||||
<button className="btn sm" style={navBtnSx} onClick={() => addDays(1)}>
|
||||
<ChevronRightIcon style={{ width: 15, height: 15 }} />
|
||||
</button>
|
||||
@@ -417,14 +419,23 @@ export default function AppointmentsPage() {
|
||||
navigate(`/admin/appointments/${a.uuid}`);
|
||||
}
|
||||
|
||||
// تور بار اول فقط وقتی راه میافتد که نوبتها آمده باشند؛ قبل از آن نیمی از
|
||||
// المانهای هدف هنوز روی صفحه نیستند.
|
||||
useTour('appointments', { ready: !apptQuery.isLoading });
|
||||
|
||||
return (
|
||||
<div style={{ padding: '20px 24px' }}>
|
||||
<div style={{ maxWidth: 1050, margin: '0 auto' }}>
|
||||
{/* عنوان */}
|
||||
<h1 style={{ fontSize: 20, fontWeight: 700, color: 'var(--text)', marginBottom: 16 }}>نوبت ها</h1>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 4, marginBottom: 16 }}>
|
||||
<h1 style={{ fontSize: 20, fontWeight: 700, color: 'var(--text)' }}>نوبت ها</h1>
|
||||
<TourButton tourId="appointments" />
|
||||
</div>
|
||||
|
||||
{/* نوار آمار */}
|
||||
<TurnsStatInfo stats={stats} />
|
||||
<div data-tour="appointments-stats">
|
||||
<TurnsStatInfo stats={stats} />
|
||||
</div>
|
||||
|
||||
{/* نوار ابزار (بیرونِ کارت، مطابق طرح) */}
|
||||
<div style={{
|
||||
@@ -454,13 +465,16 @@ export default function AppointmentsPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<TurnsViewToggle viewMode={viewMode} onChange={setViewMode} />
|
||||
<div data-tour="appointments-view" style={{ display: 'flex' }}>
|
||||
<TurnsViewToggle viewMode={viewMode} onChange={setViewMode} />
|
||||
</div>
|
||||
|
||||
<div style={{ flex: 1 }} />
|
||||
|
||||
{/* سمت چپ: فیلتر + افزودن نوبت */}
|
||||
<button
|
||||
aria-label="فیلترها"
|
||||
data-tour="appointments-filters"
|
||||
className="btn sm"
|
||||
onClick={() => setFiltersOpen(true)}
|
||||
style={{
|
||||
@@ -476,6 +490,7 @@ export default function AppointmentsPage() {
|
||||
{!isRepresentation && canCreateAppt && (
|
||||
<button
|
||||
className="btn primary sm"
|
||||
data-tour="appointments-new"
|
||||
onClick={() => {
|
||||
// تب منبع فعال است ⇒ نوبت برای همان منبع، با سرویسهای خودش.
|
||||
if (activeResource) { setBookingResource(activeResource); return; }
|
||||
@@ -491,21 +506,23 @@ export default function AppointmentsPage() {
|
||||
</div>
|
||||
|
||||
{/* کارت اصلی — تب دکترها (هدر) + محتوا */}
|
||||
<div style={{
|
||||
<div data-tour="appointments-list" style={{
|
||||
background: 'var(--surface)', border: '1px solid var(--border)',
|
||||
borderRadius: 'var(--r)', overflow: 'hidden',
|
||||
}}>
|
||||
{showDoctorTabs && (
|
||||
<DoctorTabs
|
||||
doctors={doctors.map(d => ({
|
||||
uuid: d.uuid,
|
||||
name: d.name,
|
||||
note: d.hasSchedule ? undefined : 'بدون ساعت کاری',
|
||||
}))}
|
||||
selected={selectedDoctorUuid}
|
||||
onSelect={selectDoctor}
|
||||
showAll={isAdmin}
|
||||
/>
|
||||
<div data-tour="appointments-doctors">
|
||||
<DoctorTabs
|
||||
doctors={doctors.map(d => ({
|
||||
uuid: d.uuid,
|
||||
name: d.name,
|
||||
note: d.hasSchedule ? undefined : 'بدون ساعت کاری',
|
||||
}))}
|
||||
selected={selectedDoctorUuid}
|
||||
onSelect={selectDoctor}
|
||||
showAll={isAdmin}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* منابعِ همین پزشک، نه همهٔ منابع: ارتباط پزشک↔منبع روی خودِ منبع تعریف شده
|
||||
@@ -735,7 +752,7 @@ function ServiceFilterSelect({ value, options, onChange }: {
|
||||
value: string; options: { uuid: string; name: string }[]; onChange: (v: string) => void;
|
||||
}) {
|
||||
return (
|
||||
<div style={{ minWidth: 280 }}>
|
||||
<div data-tour="appointments-service" style={{ minWidth: 280 }}>
|
||||
<SearchableSelect
|
||||
options={options.map(s => ({ value: s.uuid, label: s.name }))}
|
||||
value={value || null}
|
||||
|
||||
Reference in New Issue
Block a user