Four pages on the existing design system: a resources list whose branch/type/skill/
status filters live in the URL and go straight to the server, and three supporting
pages for types, skills and pools. Filtering client-side over a list the server had
already filtered would have been a second source of truth, so the page does neither.
The pool members dialog only offers resources from the pool's own branch and type —
the same rule the server enforces with 422, applied early so the user never reaches
the error. Skill assignment and pool membership are both full replacements, and both
say so in the dialog, because a partial-looking save that silently drops rows is
worse than an explicit one.
Wiring that was missing: deactivating a staff member through
PATCH /api/v1/staff/{uuid}/toggle now closes their resource too. Without it an
inactive operator would still have shown up in availability search. It is an explicit
call rather than a Doctrine lifecycle callback, since callbacks do not fire for
getArrayResult() — which is how every admin list is built — and that asymmetry is
its own bug. The reverse does not hold: closing a resource does not deactivate the
person, who may be purely administrative.
docs/api/resource.md documents all sixteen endpoints with responses captured from
real curl runs against ddev, including the 422 bodies for person-capacity and
non-scalar attributes. staff.md gains a "relationship to resources" section stating
that job_title is not a skill. tenancy.md contrasts these aggregate children —
whose roots do carry a tenant pair — with the branch_working_hours case from task 01,
where the root was global and the classification was wrong.
Also fixed a pre-existing flaky test: NumericFieldNormalizerTest guarded its random
mobile against collision on the never-reset db_test but not its random national code,
so a full-suite run could fail with 422 and close the EntityManager, taking an
unrelated test down with it. Both are now guarded, and the assertion prints the
server's response instead of a bare "422 is not 201".
Verified: phpunit 1119 tests / 3113 assertions green; slot-mode frozen contract green;
phpstan 14 errors before and after, none in touched files; tsc clean; vitest 88 files
/ 617 tests green.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
183 lines
7.7 KiB
TypeScript
183 lines
7.7 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { toast } from 'sonner';
|
|
import { api, ApiError, type ApiResponse } from '../lib/api';
|
|
import type {
|
|
ClinicResource, ResourcePool, ResourcePayload, ResourceType, Skill,
|
|
} from '../types';
|
|
|
|
/**
|
|
* منابع: هر چیزی که ممکن است اشغال باشد. هر منبع مال یک شعبه است، و شعبه همان
|
|
* آدرس محل نوبتدهی است — پس فیلترها با `address_uuid` کار میکنند نه `branch_id`.
|
|
*/
|
|
const RESOURCES_KEY = 'resources';
|
|
const TYPES_KEY = ['resource-types'];
|
|
const SKILLS_KEY = ['skills'];
|
|
const POOLS_KEY = ['resource-pools'];
|
|
|
|
function fail(e: unknown, fallback: string) {
|
|
toast.error(e instanceof ApiError ? e.message : fallback);
|
|
}
|
|
|
|
export type ResourceFilters = {
|
|
address_uuid?: string;
|
|
type_uuid?: string;
|
|
skill_uuid?: string;
|
|
active?: string;
|
|
};
|
|
|
|
function toQuery(filters: ResourceFilters): string {
|
|
const params = new URLSearchParams();
|
|
Object.entries(filters).forEach(([k, v]) => {
|
|
if (v) params.set(k, v);
|
|
});
|
|
const qs = params.toString();
|
|
return qs === '' ? '' : `?${qs}`;
|
|
}
|
|
|
|
export function useResources(filters: ResourceFilters = {}) {
|
|
const qc = useQueryClient();
|
|
const invalidate = () => qc.invalidateQueries({ queryKey: [RESOURCES_KEY] });
|
|
|
|
const query = useQuery({
|
|
queryKey: [RESOURCES_KEY, filters],
|
|
queryFn: () => api.get<ApiResponse<ClinicResource[]>>(`/api/v1/resources${toQuery(filters)}`),
|
|
});
|
|
|
|
const create = useMutation({
|
|
mutationFn: (d: ResourcePayload) => api.post<ApiResponse<ClinicResource>>('/api/v1/resource', d),
|
|
onSuccess: () => { toast.success('منبع افزوده شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'افزودن منبع ناموفق بود'),
|
|
});
|
|
|
|
const update = useMutation({
|
|
mutationFn: ({ uuid, d }: { uuid: string; d: ResourcePayload }) =>
|
|
api.patch<ApiResponse<ClinicResource>>(`/api/v1/resource/${uuid}`, d),
|
|
onSuccess: () => { toast.success('منبع بهروزرسانی شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'بهروزرسانی منبع ناموفق بود'),
|
|
});
|
|
|
|
const remove = useMutation({
|
|
mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/resource/${uuid}`),
|
|
onSuccess: () => { toast.success('منبع حذف شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'حذف منبع ناموفق بود'),
|
|
});
|
|
|
|
/** جایگزینی کامل: مهارتی که در بدنه نیست، برداشته میشود. */
|
|
const setSkills = useMutation({
|
|
mutationFn: ({ uuid, skills }: { uuid: string; skills: { skill_uuid: string; level: number }[] }) =>
|
|
api.put<ApiResponse<ClinicResource>>(`/api/v1/resource/${uuid}/skills`, { skills }),
|
|
onSuccess: () => { toast.success('مهارتها ذخیره شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'ذخیرهٔ مهارتها ناموفق بود'),
|
|
});
|
|
|
|
return {
|
|
resources: query.data?.data ?? [],
|
|
loading: query.isLoading,
|
|
create, update, remove, setSkills,
|
|
};
|
|
}
|
|
|
|
export function useResourceTypes() {
|
|
const qc = useQueryClient();
|
|
const invalidate = () => qc.invalidateQueries({ queryKey: TYPES_KEY });
|
|
|
|
const query = useQuery({
|
|
queryKey: TYPES_KEY,
|
|
queryFn: () => api.get<ApiResponse<ResourceType[]>>('/api/v1/resource-types'),
|
|
});
|
|
|
|
const create = useMutation({
|
|
mutationFn: (d: { code: string; name: string }) =>
|
|
api.post<ApiResponse<ResourceType>>('/api/v1/resource-types', d),
|
|
onSuccess: () => { toast.success('نوع منبع افزوده شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'افزودن نوع منبع ناموفق بود'),
|
|
});
|
|
|
|
const update = useMutation({
|
|
mutationFn: ({ uuid, d }: { uuid: string; d: { name?: string; active?: boolean } }) =>
|
|
api.patch<ApiResponse<ResourceType>>(`/api/v1/resource-type/${uuid}`, d),
|
|
onSuccess: () => { toast.success('نوع منبع بهروزرسانی شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'بهروزرسانی ناموفق بود'),
|
|
});
|
|
|
|
const remove = useMutation({
|
|
mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/resource-type/${uuid}`),
|
|
onSuccess: () => { toast.success('نوع منبع حذف شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'حذف نوع منبع ناموفق بود'),
|
|
});
|
|
|
|
return { types: query.data?.data ?? [], loading: query.isLoading, create, update, remove };
|
|
}
|
|
|
|
export function useSkills() {
|
|
const qc = useQueryClient();
|
|
const invalidate = () => qc.invalidateQueries({ queryKey: SKILLS_KEY });
|
|
|
|
const query = useQuery({
|
|
queryKey: SKILLS_KEY,
|
|
queryFn: () => api.get<ApiResponse<Skill[]>>('/api/v1/skills'),
|
|
});
|
|
|
|
const create = useMutation({
|
|
mutationFn: (d: { name: string }) => api.post<ApiResponse<Skill>>('/api/v1/skills', d),
|
|
onSuccess: () => { toast.success('مهارت افزوده شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'افزودن مهارت ناموفق بود'),
|
|
});
|
|
|
|
const update = useMutation({
|
|
mutationFn: ({ uuid, d }: { uuid: string; d: { name?: string; active?: boolean } }) =>
|
|
api.patch<ApiResponse<Skill>>(`/api/v1/skill/${uuid}`, d),
|
|
onSuccess: () => { toast.success('مهارت بهروزرسانی شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'بهروزرسانی ناموفق بود'),
|
|
});
|
|
|
|
const remove = useMutation({
|
|
mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/skill/${uuid}`),
|
|
onSuccess: () => { toast.success('مهارت حذف شد'); invalidate(); },
|
|
// پیام سرور دقیق است («به N منبع داده شده»)، پس همان نشان داده میشود.
|
|
onError: (e) => fail(e, 'حذف مهارت ناموفق بود'),
|
|
});
|
|
|
|
return { skills: query.data?.data ?? [], loading: query.isLoading, create, update, remove };
|
|
}
|
|
|
|
export function useResourcePools() {
|
|
const qc = useQueryClient();
|
|
const invalidate = () => qc.invalidateQueries({ queryKey: POOLS_KEY });
|
|
|
|
const query = useQuery({
|
|
queryKey: POOLS_KEY,
|
|
queryFn: () => api.get<ApiResponse<ResourcePool[]>>('/api/v1/resource-pools'),
|
|
});
|
|
|
|
const create = useMutation({
|
|
mutationFn: (d: { address_uuid: string; type_uuid: string; name: string }) =>
|
|
api.post<ApiResponse<ResourcePool>>('/api/v1/resource-pools', d),
|
|
onSuccess: () => { toast.success('استخر افزوده شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'افزودن استخر ناموفق بود'),
|
|
});
|
|
|
|
const update = useMutation({
|
|
mutationFn: ({ uuid, d }: { uuid: string; d: { name?: string; active?: boolean } }) =>
|
|
api.patch<ApiResponse<ResourcePool>>(`/api/v1/resource-pool/${uuid}`, d),
|
|
onSuccess: () => { toast.success('استخر بهروزرسانی شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'بهروزرسانی ناموفق بود'),
|
|
});
|
|
|
|
const remove = useMutation({
|
|
mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/resource-pool/${uuid}`),
|
|
onSuccess: () => { toast.success('استخر حذف شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'حذف استخر ناموفق بود'),
|
|
});
|
|
|
|
/** جایگزینی کامل اعضا؛ سرور همشعبه و همنوع بودن را اجبار میکند. */
|
|
const setMembers = useMutation({
|
|
mutationFn: ({ uuid, members }: { uuid: string; members: { resource_uuid: string; priority: number }[] }) =>
|
|
api.put<ApiResponse<ResourcePool>>(`/api/v1/resource-pool/${uuid}/members`, { members }),
|
|
onSuccess: () => { toast.success('اعضای استخر ذخیره شد'); invalidate(); },
|
|
onError: (e) => fail(e, 'ذخیرهٔ اعضا ناموفق بود'),
|
|
});
|
|
|
|
return { pools: query.data?.data ?? [], loading: query.isLoading, create, update, remove, setMembers };
|
|
}
|