refactor(branch): remove the branch domain, keep the address

Branches and rooms are not part of the resource-first product: a room is a
resource like any other, and the only thing the branch pages still managed —
opening hours — duplicated the resource's own shift.

What could not go is the address. Every appointment carries address_id (75 of
75 rows), the public booking site reads /clinic-pro/doctor-address/{id}, and a
resource derives its tenant pair from the address it belongs to. So
DoctorAddress stays as an invisible anchor with no page and no menu entry, and
GET /api/v1/addresses replaces GET /api/v1/branches for the forms that still
need to say "where".

BranchResolver was likewise not a branch feature. doctor_addresses is a global
table, so TenantFilter does not cover it and eight callers across booking,
availability, pricing and the catalog went through this resolver to avoid
leaking another clinic's address. It moved to Doctor\Service\AddressResolver
rather than dying with the domain.

The availability engine loses one layer: a resource's real hours were the
branch hours intersected with its shift, and are now the shift alone. That is
the single behavioural change, and the three tests that asserted the old
contract are replaced by one that states the new one.

Rooms already had a resource row each; the migration drops only the bridge
back to `rooms`, and drops it before the table — that foreign key is ON DELETE
CASCADE and the other order would take the resources, and their appointments,
with it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-02 15:25:32 +03:30
co-authored by Claude Opus 5
parent 1c4f2a2451
commit dd284ec622
59 changed files with 566 additions and 3128 deletions
+23
View File
@@ -0,0 +1,23 @@
import { useQuery } from '@tanstack/react-query';
import { api, type ApiResponse } from '../lib/api';
import type { Branch } from '../types';
/**
* محل‌های نوبت‌دهی محیط جاری — همان `doctor_addresses`.
*
* جانشین `useBranches` است. مفهوم «شعبه» از محصول حذف شد، ولی فرم منبع، لیست قیمت و
* استخر منبع هنوز باید بگویند «کجا»، پس فهرست آدرس‌ها فقط برای انتخاب می‌ماند. ساخت و
* ویرایش آدرس همان‌جایی است که همیشه بود (جزئیات کلینیک/پزشک).
*/
export function useAddresses() {
const query = useQuery({
queryKey: ['addresses'],
queryFn: () => api.get<ApiResponse<Branch[]>>('/api/v1/addresses'),
});
// پاسخِ غیرآرایه (خطای سرور، شکل دیگر) نباید صفحه را با «map is not a function»
// بترکاند؛ فهرست خالی رفتار درست است.
const addresses = Array.isArray(query.data?.data) ? query.data.data : [];
return { addresses, loading: query.isLoading };
}
-103
View File
@@ -1,103 +0,0 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { api, ApiError, type ApiResponse } from '../lib/api';
import type { Branch, BranchWorkingHours, Room, RoomPayload, WorkingHoursPayload } from '../types';
/**
* «شعبه» یک جدول تازه نیست — همان آدرس محل نوبت‌دهی است (`doctor_addresses`).
* ساخت/ویرایش نام و آدرس همان‌جایی انجام می‌شود که همیشه (جزئیات کلینیک/پزشک)؛
* این هوک فقط چیزهای شعبه‌ای را می‌دهد: فعال/غیرفعال، منطقهٔ زمانی، ساعت کاری، اتاق.
*/
const BRANCHES_KEY = ['branches'];
function fail(e: unknown, fallback: string) {
toast.error(e instanceof ApiError ? e.message : fallback);
}
export function useBranches() {
const qc = useQueryClient();
const query = useQuery({
queryKey: BRANCHES_KEY,
queryFn: () => api.get<ApiResponse<Branch[]>>('/api/v1/branches'),
});
const update = useMutation({
mutationFn: ({ uuid, d }: { uuid: string; d: { active?: boolean; timezone?: string } }) =>
api.patch<ApiResponse<Branch>>(`/api/v1/branch/${uuid}`, d),
onSuccess: () => {
toast.success('شعبه به‌روزرسانی شد');
qc.invalidateQueries({ queryKey: BRANCHES_KEY });
},
onError: (e) => fail(e, 'به‌روزرسانی شعبه ناموفق بود'),
});
// پاسخِ غیرآرایه (خطای سرور، شکل دیگر) نباید صفحه را با «map is not a function»
// بترکاند؛ فهرست خالی رفتار درست است.
const branches = Array.isArray(query.data?.data) ? query.data.data : [];
return { branches, loading: query.isLoading, update };
}
export function useBranchWorkingHours(branchUuid: string | undefined) {
const qc = useQueryClient();
const key = ['branch-working-hours', branchUuid];
const query = useQuery({
queryKey: key,
queryFn: () => api.get<ApiResponse<BranchWorkingHours>>(`/api/v1/branch/${branchUuid}/working-hours`),
enabled: !!branchUuid,
});
/** PUT قرارداد جایگزینی کامل دارد: آرایهٔ خالی یعنی شعبه بسته، نه «تغییری نده». */
const save = useMutation({
mutationFn: (days: WorkingHoursPayload) =>
api.put<ApiResponse<BranchWorkingHours>>(`/api/v1/branch/${branchUuid}/working-hours`, { days }),
onSuccess: () => {
toast.success('ساعت کاری ذخیره شد');
qc.invalidateQueries({ queryKey: key });
qc.invalidateQueries({ queryKey: BRANCHES_KEY });
},
onError: (e) => fail(e, 'ذخیرهٔ ساعت کاری ناموفق بود'),
});
return { workingHours: query.data?.data, loading: query.isLoading, save };
}
export function useBranchRooms(branchUuid: string | undefined) {
const qc = useQueryClient();
const key = ['branch-rooms', branchUuid];
const invalidate = () => {
qc.invalidateQueries({ queryKey: key });
qc.invalidateQueries({ queryKey: BRANCHES_KEY });
};
const query = useQuery({
queryKey: key,
queryFn: () => api.get<ApiResponse<Room[]>>(`/api/v1/branch/${branchUuid}/rooms`),
enabled: !!branchUuid,
});
const create = useMutation({
mutationFn: (d: RoomPayload) =>
api.post<ApiResponse<Room>>('/api/v1/room', { ...d, address_uuid: branchUuid }),
onSuccess: () => { toast.success('اتاق افزوده شد'); invalidate(); },
onError: (e) => fail(e, 'افزودن اتاق ناموفق بود'),
});
const update = useMutation({
mutationFn: ({ uuid, d }: { uuid: string; d: RoomPayload }) =>
api.patch<ApiResponse<Room>>(`/api/v1/room/${uuid}`, d),
onSuccess: () => { toast.success('اتاق به‌روزرسانی شد'); invalidate(); },
onError: (e) => fail(e, 'به‌روزرسانی اتاق ناموفق بود'),
});
const remove = useMutation({
mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/room/${uuid}`),
onSuccess: () => { toast.success('اتاق حذف شد'); invalidate(); },
onError: (e) => fail(e, 'حذف اتاق ناموفق بود'),
});
return { rooms: query.data?.data ?? [], loading: query.isLoading, create, update, remove };
}