Files
LogisicsApp_Client/src/services/clientAddress.service.ts
m7amedez5511 db64b79fe3 first action update user pages .
2-update driver pages [fixed image display and notyfi]
3-update car pages [fixed image]
4-build tripe pages crud
5-build some role pages
6-build audit page
7-update ui compounants
8-update saidebar and topbar
9-cheange view to be ar view
10-cheange stractcher to be app,src
11-add validation layer by yup
12-add api image-proxy to broke image corse bloken
2026-06-23 15:54:07 +03:00

49 lines
2.0 KiB
TypeScript

// Mirrors user.service.ts pattern: explicit token param, buildPayload helper.
import { get, post, put, patch, del } from "./api";
import type {
ApiResponse,
ClientAddress,
ClientAddressFormData,
} from "@/src/types/client";
const base = (clientId: string) => `client/${clientId}/addresses`;
export const clientAddressService = {
/** جلب جميع عناوين عميل معين */
getAll: (clientId: string, token: string | null) =>
get<ApiResponse<ClientAddress[]>>(base(clientId), token),
/** جلب عنوان واحد بالـ ID */
getById: (clientId: string, addressId: string, token: string | null) =>
get<ApiResponse<ClientAddress>>(`${base(clientId)}/${addressId}`, token),
/** إضافة عنوان جديد لعميل */
create: (clientId: string, data: ClientAddressFormData, token: string | null) =>
post<ApiResponse<ClientAddress>>(base(clientId), buildPayload(clientId, data), token),
/** تعديل عنوان موجود */
update: (clientId: string, addressId: string, data: ClientAddressFormData, token: string | null) =>
put<ApiResponse<ClientAddress>>(`${base(clientId)}/${addressId}`, buildPayload(clientId, data), token),
/** حذف عنوان */
delete: (clientId: string, addressId: string, token: string | null) =>
del<void>(`${base(clientId)}/${addressId}`, token),
/** تعيين عنوان كعنوان رئيسي — الباكند يُلغي الاختيار عن الباقين */
setPrimary: (clientId: string, addressId: string, token: string | null) =>
patch<ApiResponse<ClientAddress>>(`${base(clientId)}/${addressId}/primary`, {}, token),
};
/** تحويل بيانات النموذج إلى payload */
function buildPayload(clientId: string, data: ClientAddressFormData): Record<string, unknown> {
return {
clientId,
label: data.label.trim(),
street: data.street.trim(),
city: data.city.trim(),
state: data.state.trim(),
postalCode: data.postalCode.trim(),
country: data.country.trim(),
isPrimary: data.isPrimary,
};
}