44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { useApi } from "~/composables/useApi"
|
|
import type { CustomerData, CustomerPayload } from "~/services/dto/customer-data"
|
|
|
|
export type CustomerListResponse =
|
|
| CustomerData[]
|
|
| { "hydra:member"?: CustomerData[] }
|
|
|
|
export async function getCustomerList(): Promise<CustomerData[]> {
|
|
const api = useApi()
|
|
const response = await api.get<CustomerListResponse>("customers", {}, {
|
|
toastErrorKey: "errors.customer.list",
|
|
})
|
|
|
|
if (Array.isArray(response)) return response
|
|
if (response && typeof response === "object" && Array.isArray(response["hydra:member"])) {
|
|
return response["hydra:member"]
|
|
}
|
|
return []
|
|
}
|
|
|
|
export async function getCustomer(id: number): Promise<CustomerData> {
|
|
const api = useApi()
|
|
return api.get<CustomerData>(`customers/${id}`, {}, {
|
|
toastErrorKey: "errors.customer.fetch",
|
|
})
|
|
}
|
|
|
|
export async function updateCustomer(id: number, payload: Partial<CustomerPayload>): Promise<CustomerData> {
|
|
const api = useApi()
|
|
return api.patch<CustomerData>(`customers/${id}`, payload, {
|
|
toastErrorKey: "errors.customer.update",
|
|
toastSuccessKey: "success.customer.update",
|
|
})
|
|
}
|
|
|
|
export async function createCustomer(payload: CustomerPayload): Promise<CustomerData> {
|
|
const api = useApi()
|
|
return api.post<CustomerData>("customers", payload, {
|
|
toastErrorKey: "errors.customer.create",
|
|
toastSuccessKey: "success.customer.create",
|
|
})
|
|
}
|
|
|