51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import { CrudClient } from "../infra/crud-client"
|
|
import { ApiClient, type ApiClientOptions } from "../infra/client"
|
|
import type { ApiPath, ApiRequestBody } from "../infra/types"
|
|
import type { ApiListQueryParams } from "../contracts/types"
|
|
|
|
export const CUSTOMER_ROUTES = {
|
|
INDEX: "/api/customers",
|
|
BY_ID: "/api/customers/{id}",
|
|
EXPORT: "/api/customers/export",
|
|
IMPORT: "/api/customers/import",
|
|
CUSTOMER_TYPES: "/api/customer-types",
|
|
ADD_NOTE: "/api/customers/{id}/add-note",
|
|
DELETE_NOTE: "/api/customers/{id}/delete-note",
|
|
UPDATE_PERMISSIONS: "/api/customers/{id}/update-permissions",
|
|
} as const satisfies Record<string, ApiPath>
|
|
|
|
export class CustomersClient extends CrudClient<typeof CUSTOMER_ROUTES.INDEX, typeof CUSTOMER_ROUTES.BY_ID> {
|
|
|
|
constructor(baseUrl?: string, defaultOptions?: ApiClientOptions) {
|
|
super(baseUrl, defaultOptions, CUSTOMER_ROUTES.INDEX, CUSTOMER_ROUTES.BY_ID)
|
|
}
|
|
|
|
async getById(id: string) {
|
|
return this.show(id)
|
|
}
|
|
|
|
async listCustomerTypes(query?: ApiListQueryParams) {
|
|
return this.get(CUSTOMER_ROUTES.CUSTOMER_TYPES, query ? { query } as never : undefined)
|
|
}
|
|
|
|
async export() {
|
|
return this.get(CUSTOMER_ROUTES.EXPORT)
|
|
}
|
|
|
|
async import(payload: ApiRequestBody<typeof CUSTOMER_ROUTES.IMPORT, "post">) {
|
|
return this.post(CUSTOMER_ROUTES.IMPORT, payload)
|
|
}
|
|
|
|
async addNote(id: string, payload: { note: string }) {
|
|
return this.post(CUSTOMER_ROUTES.ADD_NOTE, payload as never, { params: { id } } as never)
|
|
}
|
|
|
|
async deleteNote(customerId: string, noteId: number) {
|
|
return this.delete(CUSTOMER_ROUTES.DELETE_NOTE, { params: { id: customerId }, query: { note_id: noteId } } as never)
|
|
}
|
|
|
|
async updatePermissions(id: string, payload: ApiRequestBody<typeof CUSTOMER_ROUTES.UPDATE_PERMISSIONS, "post">) {
|
|
return this.post(CUSTOMER_ROUTES.UPDATE_PERMISSIONS, payload as never, { params: { id } } as never)
|
|
}
|
|
}
|