fix(secretary): settings menu structure, clinic timeline access, patient delete gate

Three reported secretary-access bugs.

1) Settings menu structure. Phase B flat-listed staff/discounts/sms/tags/
   appointment_settings/clinic_doctors in the secretary's main sidebar. Mirror
   the doctor/clinic layout instead: only inventory + services stay in the main
   «مدیریت» nav; the rest live under a single «تنظیمات» entry
   (→ /admin/account-settings). Made both settings navs permission-aware for
   secretaries: SETTINGS_MENU (menuForRole now takes `can`) and
   PurchaseSubscriptionSidebar filter by a per-item `perm`/`alwaysOpen` instead
   of role only, so a secretary sees exactly their permitted settings pages and
   owner-only items (subscription, secretary-management) stay hidden.

2) Clinic secretary appointment timeline. AppointmentsPage treated a
   clinic-scoped secretary as a single-doctor profile: the doctor list was
   fetched/shown only for isClinic/isAdmin, so no doctor tabs, timeline, or
   booking. Now a clinic-scoped secretary is multi-doctor: fetches the doctor
   list, shows tabs, auto-selects the first doctor. The list comes from a new
   authenticated endpoint GET /api/v1/my/clinic-doctors returning only the
   secretary's ASSIGNED doctors — /clinic/doctor-list is on the public (no-JWT)
   firewall and cannot scope by user, so it would have leaked unbookable doctors.

3) Patient record delete. The `patients.delete` toggle was dead: every record
   delete (note/medical-record/attachment/call/message) was gated as
   `patients.update`. Mapped them to `patients.delete` so the toggle is honored
   and delete is controllable separately from edit.

New SecretaryAccessChecker::assignedClinicDoctorIds. Tests: doctor-list scoping,
patients.delete separation (denied/allowed). docs/api secretary.md +
appointment.md updated. Backend 286 + frontend 25 pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-23 18:01:32 +03:30
co-authored by Claude Opus 4.8
parent 653dd57300
commit 54c8b008bf
14 changed files with 274 additions and 122 deletions
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import { screen } from '@testing-library/react';
import { renderWithProviders } from '../../test/utils';
import { useAuthStore } from '../../stores/authStore';
import PurchaseSubscriptionSidebar from './PurchaseSubscriptionSidebar';
describe('PurchaseSubscriptionSidebar — settings menu', () => {
@@ -20,6 +21,23 @@ describe('PurchaseSubscriptionSidebar — settings menu', () => {
expect(screen.getByRole('link', { name: 'مدیریت منشی' })).not.toHaveAttribute('aria-current');
});
it('برای منشی فقط آیتم‌های مجاز را نشان می‌دهد (staff مجاز، بقیه پنهان)', () => {
useAuthStore.setState({
primaryRole: 'secretary',
context: { scope: 'clinic', permissions: { resources: { staff: { view: true } } } },
} as any);
renderWithProviders(<PurchaseSubscriptionSidebar active="staff" />, { route: '/admin/staff' });
// مجاز
expect(screen.getByRole('link', { name: 'پرسنل' })).toHaveAttribute('href', '/admin/staff');
expect(screen.getByRole('link', { name: 'حساب کاربری' })).toBeInTheDocument();
// owner-only / بدون مجوز → پنهان
expect(screen.queryByRole('link', { name: 'مدیریت منشی' })).not.toBeInTheDocument();
expect(screen.queryByRole('link', { name: 'خرید اشتراک' })).not.toBeInTheDocument();
expect(screen.queryByRole('link', { name: 'مدیریت تخفیف‌ها' })).not.toBeInTheDocument();
useAuthStore.setState({ primaryRole: null, context: null } as any);
});
// regression: the settings menu must stay visible on mobile (was `hidden lg:block`,
// which dropped the whole menu below the lg breakpoint → no settings nav on phones).
it('is not hidden on mobile', () => {
@@ -2,6 +2,7 @@ import React, { useMemo, useState } from 'react';
import { Link } from 'react-router-dom';
import { SearchHeaderP } from '../../pages/subscriptionIcons';
import { useAuthStore } from '../../stores/authStore';
import { usePermissions } from '../../hooks/usePermissions';
/**
* Settings sub-navigation for the subscription page — item list and order copied
@@ -13,33 +14,52 @@ import { useAuthStore } from '../../stores/authStore';
* This list is intentionally separate from SETTINGS_MENU (SettingsLayout) so the
* other settings pages are not affected.
*/
/** `roles`: when set, the item is only shown to those roles (omit = every role). */
type NavItem = { key: string; label: string; to?: string; roles?: string[] };
/**
* `roles`: when set, the item is only shown to those roles (omit = every role).
* `perm`: [resource, action] — منشی فقط با داشتن این مجوز آیتم را می‌بیند.
* `alwaysOpen`: برای منشی همیشه نمایش داده می‌شود (حساب/تنظیمات پایه).
* آیتم‌های owner-only (خرید اشتراک، مدیریت منشی) نه `perm` دارند نه `alwaysOpen`
* → برای منشی پنهان می‌شوند.
*/
type NavItem = {
key: string;
label: string;
to?: string;
roles?: string[];
perm?: [string, string];
alwaysOpen?: boolean;
};
const NAV_ITEMS: NavItem[] = [
{ key: 'subscription', label: 'خرید اشتراک', to: '/admin/subscription' },
{ key: 'payment', label: 'مدیریت پرداخت', to: '/admin/my-financial' },
{ key: 'appointment', label: 'مدیریت نوبت دهی', to: '/admin/appointment-settings', roles: ['doctor'] },
{ key: 'appointment', label: 'مدیریت نوبت دهی', to: '/admin/settings/appointment-settings', roles: ['clinic'] },
{ key: 'insurance', label: 'مدیریت بیمه', to: '/admin/insurance-pricing' },
{ key: 'discounts', label: 'مدیریت تخفیف‌ها', to: '/admin/discounts', roles: ['doctor', 'clinic'] },
{ key: 'tags', label: 'تگ ها', to: '/admin/tags-settings' },
{ key: 'sms', label: 'پیامک ها', to: '/admin/sms-wallet' },
{ key: 'clinic-doctors', label: 'پزشکان کلینیک', to: '/admin/settings/clinic-doctors', roles: ['clinic'] },
{ key: 'payment', label: 'مدیریت پرداخت', to: '/admin/my-financial', perm: ['payments', 'view'] },
{ key: 'appointment', label: 'مدیریت نوبت دهی', to: '/admin/appointment-settings', roles: ['doctor'], perm: ['appointment_settings', 'view'] },
{ key: 'appointment', label: 'مدیریت نوبت دهی', to: '/admin/settings/appointment-settings', roles: ['clinic'], perm: ['appointment_settings', 'view'] },
{ key: 'insurance', label: 'مدیریت بیمه', to: '/admin/insurance-pricing', perm: ['insurances', 'view'] },
{ key: 'discounts', label: 'مدیریت تخفیف‌ها', to: '/admin/discounts', roles: ['doctor', 'clinic'], perm: ['discounts', 'view'] },
{ key: 'tags', label: 'تگ ها', to: '/admin/tags-settings', perm: ['tags', 'view'] },
{ key: 'sms', label: 'پیامک ها', to: '/admin/sms-wallet', perm: ['sms', 'view'] },
{ key: 'clinic-doctors', label: 'پزشکان کلینیک', to: '/admin/settings/clinic-doctors', roles: ['clinic'], perm: ['clinic_doctors', 'view'] },
{ key: 'secretary', label: 'مدیریت منشی', to: '/admin/my-secretaries' },
{ key: 'staff', label: 'پرسنل', to: '/admin/staff' },
{ key: 'account', label: 'حساب کاربری', to: '/admin/account-settings' },
{ key: 'security', label: 'تنظیمات' },
{ key: 'staff', label: 'پرسنل', to: '/admin/staff', perm: ['staff', 'view'] },
{ key: 'account', label: 'حساب کاربری', to: '/admin/account-settings', alwaysOpen: true },
{ key: 'security', label: 'تنظیمات', alwaysOpen: true },
];
export default function PurchaseSubscriptionSidebar({ active }: { active: string }) {
const [query, setQuery] = useState('');
const primaryRole = useAuthStore((s) => s.primaryRole);
const { can } = usePermissions();
const items = useMemo(
() => NAV_ITEMS
.filter((i) => !i.roles || (primaryRole != null && i.roles.includes(primaryRole)))
// منشی: بر اساس مجوز، نه نقش. آیتمِ بدونِ perm/alwaysOpen برای منشی پنهان است.
.filter((i) =>
primaryRole === 'secretary'
? (i.alwaysOpen || (i.perm ? can(i.perm[0], i.perm[1]) : false))
: (!i.roles || (primaryRole != null && i.roles.includes(primaryRole))),
)
.filter((i) => i.label.includes(query.trim())),
[query, primaryRole],
[query, primaryRole, can],
);
return (
@@ -17,27 +17,43 @@ export type SettingsMenuItem = {
to?: string;
/** when set, the item is only shown to these roles (omit = every role) */
roles?: string[];
/** [resource, action] — منشی فقط با داشتن این مجوز آیتم را می‌بیند. */
perm?: [string, string];
/** برای منشی همیشه نمایش داده می‌شود (حساب کاربری). */
alwaysOpen?: boolean;
};
export const SETTINGS_MENU: SettingsMenuItem[] = [
{ key: 'subscription', label: 'خرید اشتراک', icon: CreditCardIcon, to: '/admin/subscription' },
{ key: 'doctor', label: 'مدیریت پزشک', icon: UserIcon, to: '/admin/profile', roles: ['doctor'] },
{ key: 'appointment', label: 'مدیریت نوبت دهی', icon: CalendarDaysIcon, to: '/admin/appointment-settings', roles: ['doctor'] },
{ key: 'appointment', label: 'مدیریت نوبت دهی', icon: CalendarDaysIcon, to: '/admin/settings/appointment-settings', roles: ['clinic'] },
{ key: 'clinic-doctors', label: 'پزشکان کلینیک', icon: BuildingOffice2Icon, to: '/admin/settings/clinic-doctors', roles: ['clinic'] },
{ key: 'payment', label: 'مدیریت پرداخت', icon: BanknotesIcon, to: '/admin/my-financial' },
{ key: 'appointment', label: 'مدیریت نوبت دهی', icon: CalendarDaysIcon, to: '/admin/appointment-settings', roles: ['doctor'], perm: ['appointment_settings', 'view'] },
{ key: 'appointment', label: 'مدیریت نوبت دهی', icon: CalendarDaysIcon, to: '/admin/settings/appointment-settings', roles: ['clinic'], perm: ['appointment_settings', 'view'] },
{ key: 'clinic-doctors', label: 'پزشکان کلینیک', icon: BuildingOffice2Icon, to: '/admin/settings/clinic-doctors', roles: ['clinic'], perm: ['clinic_doctors', 'view'] },
{ key: 'payment', label: 'مدیریت پرداخت', icon: BanknotesIcon, to: '/admin/my-financial', perm: ['payments', 'view'] },
{ key: 'secretary', label: 'مدیریت منشی', icon: UsersIcon, to: '/admin/my-secretaries' },
{ key: 'staff', label: 'پرسنل', icon: UserPlusIcon, to: '/admin/staff' },
{ key: 'insurance', label: 'مدیریت بیمه', icon: ShieldCheckIcon, to: '/admin/insurance-pricing' },
{ key: 'discounts', label: 'مدیریت تخفیف‌ها', icon: ReceiptPercentIcon, to: '/admin/discounts', roles: ['doctor', 'clinic'] },
{ key: 'tags', label: 'برچسب‌ها', icon: TagIcon, to: '/admin/tags-settings' },
{ key: 'sms', label: 'پیامک‌ها', icon: ChatBubbleLeftRightIcon, to: '/admin/sms-wallet' },
{ key: 'account', label: 'حساب کاربری', icon: UserCircleIcon, to: '/admin/account-settings' },
{ key: 'staff', label: 'پرسنل', icon: UserPlusIcon, to: '/admin/staff', perm: ['staff', 'view'] },
{ key: 'insurance', label: 'مدیریت بیمه', icon: ShieldCheckIcon, to: '/admin/insurance-pricing', perm: ['insurances', 'view'] },
{ key: 'discounts', label: 'مدیریت تخفیف‌ها', icon: ReceiptPercentIcon, to: '/admin/discounts', roles: ['doctor', 'clinic'], perm: ['discounts', 'view'] },
{ key: 'tags', label: 'برچسب‌ها', icon: TagIcon, to: '/admin/tags-settings', perm: ['tags', 'view'] },
{ key: 'sms', label: 'پیامک‌ها', icon: ChatBubbleLeftRightIcon, to: '/admin/sms-wallet', perm: ['sms', 'view'] },
{ key: 'account', label: 'حساب کاربری', icon: UserCircleIcon, to: '/admin/account-settings', alwaysOpen: true },
];
/** Menu items visible to the given role (items without `roles` are shown to all). */
export function menuForRole(role: string | null | undefined): SettingsMenuItem[] {
return SETTINGS_MENU.filter((i) => !i.roles || (role != null && i.roles.includes(role)));
/**
* Menu items visible to the given role. برای منشی بر اساس مجوز فیلتر می‌شود
* (آیتمِ بدونِ perm/alwaysOpen پنهان است)؛ سایر نقش‌ها با roles.
*/
export function menuForRole(
role: string | null | undefined,
can?: (resource: string, action: string) => boolean,
): SettingsMenuItem[] {
return SETTINGS_MENU.filter((i) => {
if (role === 'secretary') {
if (i.alwaysOpen) return true;
return i.perm && can ? can(i.perm[0], i.perm[1]) : false;
}
return !i.roles || (role != null && i.roles.includes(role));
});
}
/**
+19 -33
View File
@@ -104,55 +104,41 @@ describe("Sidebar — گِیت منوی منشی بر اساس مجوز", () =>
);
});
it("با مجوز inventory.view و tags.view، همان آیتم‌ها نمایش داده می‌شوند", () => {
setSecretary({ inventory: { view: true }, tags: { view: true } });
it("انبارداری با مجوز inventory.view در سایدبار اصلی می‌آید", () => {
setSecretary({ inventory: { view: true } });
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.getByText("انبارداری").closest("a")).toHaveAttribute(
"href",
"/admin/inventory",
);
expect(screen.getByText("تگ‌ها").closest("a")).toHaveAttribute(
});
it("آیتم «تنظیمات» همیشه برای منشی نمایش داده می‌شود", () => {
setSecretary({ appointments: { view: true } });
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
// «تنظیمات» هم عنوان بخش است هم آیتم؛ فقط لینکِ آیتم را می‌سنجیم.
expect(screen.getByRole("link", { name: "تنظیمات" })).toHaveAttribute(
"href",
"/admin/tags-settings",
"/admin/account-settings",
);
});
it("منابع فاز B (staff/discounts/sms/appointment_settings) با مجوز نمایش داده می‌شوند", () => {
it("منابعِ زیرمجموعهٔ تنظیمات (staff/discounts/sms/tags/appointment/clinic_doctors) در سایدبار اصلی نمی‌آیند", () => {
// حتی با همهٔ مجوزها، این‌ها باید فقط داخل صفحهٔ تنظیمات باشند نه nav اصلی.
setSecretary({
staff: { view: true },
discounts: { view: true },
sms: { view: true },
tags: { view: true },
appointment_settings: { view: true },
clinic_doctors: { view: true },
});
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.getByText("پرسنل").closest("a")).toHaveAttribute("href", "/admin/staff");
expect(screen.getByText("تخفیف‌ها").closest("a")).toHaveAttribute("href", "/admin/discounts");
expect(screen.getByText("پیامک‌ها").closest("a")).toHaveAttribute("href", "/admin/sms-wallet");
// scope=clinic → مسیر تنظیمات کلینیک
expect(screen.getByText("تنظیمات نوبت‌دهی").closest("a")).toHaveAttribute(
"href",
"/admin/settings/appointment-settings",
);
});
it("مدیریت پزشکان کلینیک در scope=doctor حتی با مجوز دیده نمی‌شود", () => {
useAuthStore.setState({
primaryRole: "secretary",
dbUuid: "d1",
userName: "منشی",
availableContexts: [],
context: { scope: "doctor", permissions: { resources: { clinic_doctors: { view: true } } } },
} as any);
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.queryByText("پرسنل")).not.toBeInTheDocument();
expect(screen.queryByText("تخفیف‌ها")).not.toBeInTheDocument();
expect(screen.queryByText("پیامک‌ها")).not.toBeInTheDocument();
expect(screen.queryByText("تگ‌ها")).not.toBeInTheDocument();
expect(screen.queryByText("تنظیمات نوبت‌دهی")).not.toBeInTheDocument();
expect(screen.queryByText("پزشکان کلینیک")).not.toBeInTheDocument();
});
it("مدیریت پزشکان کلینیک در scope=clinic با مجوز دیده می‌شود", () => {
setSecretary({ clinic_doctors: { view: true } });
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
expect(screen.getByText("پزشکان کلینیک").closest("a")).toHaveAttribute(
"href",
"/admin/settings/clinic-doctors",
);
});
});
+15 -48
View File
@@ -18,7 +18,6 @@ import {
KeyIcon,
LockClosedIcon,
PlusIcon,
ReceiptPercentIcon,
ShieldCheckIcon,
StarIcon,
TagIcon,
@@ -416,60 +415,28 @@ function buildSections(
label: "انبارداری",
});
}
if (can("tags", "view")) {
items.push({
to: "/admin/tags-settings",
icon: TagIcon,
label: "تگ‌ها",
});
}
if (can("staff", "view")) {
items.push({
to: "/admin/staff",
icon: UsersIcon,
label: "پرسنل",
});
}
if (can("discounts", "view")) {
items.push({
to: "/admin/discounts",
icon: ReceiptPercentIcon,
label: "تخفیف‌ها",
});
}
if (can("sms", "view")) {
items.push({
to: "/admin/sms-wallet",
icon: DevicePhoneMobileIcon,
label: "پیامک‌ها",
});
}
if (can("appointment_settings", "view")) {
items.push({
// مسیر بسته به محیط فعال: کلینیک vs مطب شخصی.
to:
scope === "clinic"
? "/admin/settings/appointment-settings"
: "/admin/appointment-settings",
icon: Cog6ToothIcon,
label: "تنظیمات نوبت‌دهی",
});
}
// مدیریت پزشکان کلینیک فقط در محیطِ کلینیک معنا دارد.
if (scope === "clinic" && can("clinic_doctors", "view")) {
items.push({
to: "/admin/settings/clinic-doctors",
icon: HeartIcon,
label: "پزشکان کلینیک",
});
}
// منابعِ زیرمجموعهٔ «تنظیمات» (staff/discounts/sms/tags/appointment_settings/
// clinic_doctors) در سایدبار اصلی نمی‌آیند — دقیقاً مثل پزشک/کلینیک، فقط داخل
// صفحهٔ «تنظیمات» با منویِ permission-aware نمایش داده می‌شوند.
return [
{
label: "عمومی",
items: [{ to: "/admin/dashboard", icon: ChartBarIcon, label: "داشبورد" }],
},
{ label: "مدیریت", items },
{
label: "تنظیمات",
items: [
{
// لندینگِ تنظیمات؛ حساب کاربری همیشه باز است و منویِ کناری
// بقیهٔ بخش‌های مجاز را بر اساس permission نشان می‌دهد.
to: "/admin/account-settings",
icon: Cog6ToothIcon,
label: "تنظیمات",
},
],
},
];
}
+16 -6
View File
@@ -489,10 +489,14 @@ export default function AppointmentsPage() {
// در محیط کلینیک، dbUuid شناسهٔ کلینیک است نه پزشک؛ uuid پزشک فقط از doctorUuid می‌آید.
const doctorUuid = useAuthStore(s => s.doctorUuid);
const clinicUuid = useClinicContext();
const scope = useAuthStore(s => s.context?.scope);
const isAdmin = primaryRole === 'admin';
const isClinic = primaryRole === 'clinic';
const isDoctor = primaryRole === 'doctor';
const isRepresentation = primaryRole === 'representation';
// منشیِ محیطِ کلینیک باید مثل کلینیک چندپزشکه رفتار کند: تب پزشکان + تایم‌لاین.
// dbUuid در این محیط uuid کلینیک است (نه پزشک) — همان مبنای clinic/doctor-list.
const isClinicScopedSecretary = primaryRole === 'secretary' && scope === 'clinic';
const [params] = useSearchParams();
const today = new Date().toISOString().slice(0, 10);
@@ -542,9 +546,15 @@ export default function AppointmentsPage() {
// ── Clinic doctors (authoritative list for tabs)
const clinicDoctorsQuery = useQuery<ApiResponse<{ data: { uuid: string; name: string }[] }>>({
queryKey: ['clinic-doctors', dbUuid],
queryFn: () => api.get(`/api/v1/clinic/doctor-list/${dbUuid}`),
enabled: isClinic && !!dbUuid,
queryKey: ['clinic-doctors', dbUuid, isClinicScopedSecretary],
// منشی از اندپوینتِ احرازشده می‌گیرد تا فقط پزشکانِ تخصیص‌یافته‌اش بیایند؛
// کلینیک/ادمین از لیستِ کاملِ عمومیِ کلینیک.
queryFn: () => api.get(
isClinicScopedSecretary
? '/api/v1/my/clinic-doctors'
: `/api/v1/clinic/doctor-list/${dbUuid}`,
),
enabled: (isClinic || isClinicScopedSecretary) && !!dbUuid,
});
const clinicDoctorsList = clinicDoctorsQuery.data?.data?.data ?? [];
@@ -570,16 +580,16 @@ export default function AppointmentsPage() {
// نوع پروفایل: کلینیک چندپزشکه (تب دکترها + مدیریت چند پزشک) در برابر پزشک مستقل.
// نقش clinic/admin = چندپزشکه؛ نقش doctor (حتی مهمانِ کلینیک) = مستقل، فقط برنامهٔ خودش.
const isMultiDoctorClinic = isClinic || isAdmin;
const isMultiDoctorClinic = isClinic || isAdmin || isClinicScopedSecretary;
const showDoctorTabs = isMultiDoctorClinic && doctors.length >= 1;
const showDoctorCol = isAdmin && !selectedDoctorUuid;
// در کلینیک، اولین دکتر به‌صورت پیش‌فرض انتخاب می‌شود تا زمانبندی مثل طرح پر باشد.
useEffect(() => {
if (isClinic && !selectedDoctorUuid && doctors.length > 0) {
if ((isClinic || isClinicScopedSecretary) && !selectedDoctorUuid && doctors.length > 0) {
setSelectedDoctorUuid(doctors[0].uuid);
}
}, [isClinic, selectedDoctorUuid, doctors]);
}, [isClinic, isClinicScopedSecretary, selectedDoctorUuid, doctors]);
// ── محل نوبت‌دهی برای ادمین
// ادمین context کلینیکی ندارد (useClinicContext → null)؛ بدون clinic_uuid فقط برنامهٔ
+3 -1
View File
@@ -3,6 +3,7 @@ import { Link } from 'react-router-dom';
import { ChevronLeftIcon } from '@heroicons/react/24/outline';
import { menuForRole } from '../components/layout/SettingsLayout';
import { useAuthStore } from '../stores/authStore';
import { usePermissions } from '../hooks/usePermissions';
/**
* SettingsMenuPage — the settings landing list for doctor/clinic users.
@@ -12,7 +13,8 @@ import { useAuthStore } from '../stores/authStore';
*/
export default function SettingsMenuPage() {
const primaryRole = useAuthStore((s) => s.primaryRole);
const items = menuForRole(primaryRole);
const { can } = usePermissions();
const items = menuForRole(primaryRole, can);
return (
<div className="fade-in" style={{ maxWidth: 720, margin: '0 auto' }}>