mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-09 05:58:18 +00:00
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import { client } from "@/utils/api";
|
|
import { unwrap } from "@/utils/endpoint";
|
|
import { URL_CONSTANTS } from "@/constants/URLS";
|
|
import type { ApiResponse } from "@/types/apiResponse";
|
|
import type {
|
|
CreateCustomerDto,
|
|
Customer,
|
|
UpdateCustomerDto,
|
|
} from "@/types/customers";
|
|
import { isAxiosError } from "axios";
|
|
|
|
const BASE = URL_CONSTANTS.CUSTOMERS_API.BASE;
|
|
|
|
export const customersService = {
|
|
list: async (): Promise<Customer[]> => {
|
|
const response = await client.get<ApiResponse<Customer[]>>(BASE);
|
|
return unwrap(response.data);
|
|
},
|
|
|
|
getById: async (id: string): Promise<Customer> => {
|
|
const response = await client.get<ApiResponse<Customer>>(
|
|
URL_CONSTANTS.CUSTOMERS_API.BY_ID(id),
|
|
);
|
|
return unwrap(response.data);
|
|
},
|
|
|
|
getByUserId: async (userId: string): Promise<Customer | null> => {
|
|
try {
|
|
const response = await client.get<ApiResponse<Customer>>(
|
|
URL_CONSTANTS.CUSTOMERS_API.BY_USER_ID(userId),
|
|
);
|
|
return unwrap(response.data);
|
|
} catch (e) {
|
|
if (isAxiosError(e) && e.response?.status === 404) {
|
|
return null;
|
|
}
|
|
throw e;
|
|
}
|
|
},
|
|
|
|
create: async (payload: CreateCustomerDto): Promise<Customer> => {
|
|
const response = await client.post<ApiResponse<Customer>>(BASE, payload);
|
|
return unwrap(response.data);
|
|
},
|
|
|
|
update: async (id: string, payload: UpdateCustomerDto): Promise<Customer> => {
|
|
const response = await client.patch<ApiResponse<Customer>>(
|
|
URL_CONSTANTS.CUSTOMERS_API.BY_ID(id),
|
|
payload,
|
|
);
|
|
return unwrap(response.data);
|
|
},
|
|
|
|
remove: async (id: string): Promise<void> => {
|
|
await client.delete(URL_CONSTANTS.CUSTOMERS_API.BY_ID(id));
|
|
},
|
|
};
|