49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
|
|
import api from './api';
|
||
|
|
import type { Product, ProductBundleComponent } from '../types/Product';
|
||
|
|
|
||
|
|
export const productService = {
|
||
|
|
getAll: async (): Promise<Product[]> => {
|
||
|
|
const response = await api.get<Product[]>('/products');
|
||
|
|
return response.data;
|
||
|
|
},
|
||
|
|
|
||
|
|
getById: async (id: number): Promise<Product> => {
|
||
|
|
const response = await api.get<Product>(`/products/${id}`);
|
||
|
|
return response.data;
|
||
|
|
},
|
||
|
|
|
||
|
|
create: async (product: Partial<Product>): Promise<Product> => {
|
||
|
|
const response = await api.post<Product>('/products', product);
|
||
|
|
return response.data;
|
||
|
|
},
|
||
|
|
|
||
|
|
update: async (id: number, product: Partial<Product>): Promise<void> => {
|
||
|
|
await api.put(`/products/${id}`, product);
|
||
|
|
},
|
||
|
|
|
||
|
|
// --- GESTIÓN DE COMBOS (BUNDLES) ---
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Agrega un producto hijo a un combo padre.
|
||
|
|
*/
|
||
|
|
addComponentToBundle: async (bundleId: number, component: { childProductId: number; quantity: number; fixedAllocationAmount?: number }) => {
|
||
|
|
await api.post(`/products/${bundleId}/components`, component);
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Elimina un componente de un combo.
|
||
|
|
*/
|
||
|
|
removeComponentFromBundle: async (bundleId: number, childProductId: number) => {
|
||
|
|
// Nota: El backend espera el ID del producto hijo, no el ID de la relación,
|
||
|
|
// según nuestra implementación de 'RemoveComponentFromBundleAsync' en el repo.
|
||
|
|
await api.delete(`/products/${bundleId}/components/${childProductId}`);
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Obtiene la lista de componentes que forman un combo.
|
||
|
|
*/
|
||
|
|
getBundleComponents: async (bundleId: number): Promise<ProductBundleComponent[]> => {
|
||
|
|
const response = await api.get<ProductBundleComponent[]>(`/products/${bundleId}/components`);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
};
|