feat(frontend): Add customers API client
Created customers.ts API client following coupons pattern Types: ✅ Customer - Full customer data ✅ CustomerAddress - Billing/shipping address ✅ CustomerStats - Order statistics ✅ CustomerListResponse - Paginated list response ✅ CustomerFormData - Create/update payload ✅ CustomerSearchResult - Autocomplete result API Methods: ✅ list() - Get customers with pagination/search/filter ✅ get() - Get single customer with full details ✅ create() - Create new customer ✅ update() - Update customer data ✅ delete() - Delete customer ✅ search() - Autocomplete search Next: Create CRUD pages (index, new, edit)
This commit is contained in:
109
admin-spa/src/lib/api/customers.ts
Normal file
109
admin-spa/src/lib/api/customers.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { api } from '../api';
|
||||
|
||||
export interface CustomerAddress {
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
company?: string;
|
||||
address_1: string;
|
||||
address_2?: string;
|
||||
city: string;
|
||||
state?: string;
|
||||
postcode: string;
|
||||
country: string;
|
||||
phone?: string;
|
||||
}
|
||||
|
||||
export interface CustomerStats {
|
||||
total_orders: number;
|
||||
total_spent: number;
|
||||
}
|
||||
|
||||
export interface Customer {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
display_name: string;
|
||||
registered: string;
|
||||
role: string;
|
||||
billing?: CustomerAddress;
|
||||
shipping?: CustomerAddress;
|
||||
stats?: CustomerStats;
|
||||
}
|
||||
|
||||
export interface CustomerListResponse {
|
||||
data: Customer[];
|
||||
pagination: {
|
||||
total: number;
|
||||
total_pages: number;
|
||||
current: number;
|
||||
per_page: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CustomerFormData {
|
||||
email: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
billing?: Partial<CustomerAddress>;
|
||||
shipping?: Partial<CustomerAddress>;
|
||||
send_email?: boolean;
|
||||
}
|
||||
|
||||
export interface CustomerSearchResult {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export const CustomersApi = {
|
||||
/**
|
||||
* List customers with pagination and filtering
|
||||
*/
|
||||
list: async (params?: {
|
||||
page?: number;
|
||||
per_page?: number;
|
||||
search?: string;
|
||||
role?: string;
|
||||
}): Promise<CustomerListResponse> => {
|
||||
return api.get('/customers', { params });
|
||||
},
|
||||
|
||||
/**
|
||||
* Get single customer
|
||||
*/
|
||||
get: async (id: number): Promise<Customer> => {
|
||||
return api.get(`/customers/${id}`);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create new customer
|
||||
*/
|
||||
create: async (data: CustomerFormData): Promise<Customer> => {
|
||||
return api.post('/customers', data);
|
||||
},
|
||||
|
||||
/**
|
||||
* Update customer
|
||||
*/
|
||||
update: async (id: number, data: Partial<CustomerFormData>): Promise<Customer> => {
|
||||
return api.put(`/customers/${id}`, data);
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete customer
|
||||
*/
|
||||
delete: async (id: number): Promise<void> => {
|
||||
return api.del(`/customers/${id}`);
|
||||
},
|
||||
|
||||
/**
|
||||
* Search customers (for autocomplete)
|
||||
*/
|
||||
search: async (query: string, limit?: number): Promise<CustomerSearchResult[]> => {
|
||||
return api.get('/customers/search', { params: { q: query, limit } });
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user