import api from '@services/api/axios-instance'; import type { ApiResponse } from '@shared/types/api.types'; import type { Warehouse, WarehouseLocation, CreateWarehouseDto, UpdateWarehouseDto, CreateLocationDto, UpdateLocationDto, WarehouseFilters, LocationFilters, WarehousesResponse, LocationsResponse, } from '../types'; const BASE_URL = '/api/v1/warehouses'; // ==================== Warehouses API ==================== export const warehousesApi = { // List all warehouses with filters list: async (filters?: WarehouseFilters): Promise => { const response = await api.get & { total?: number }>(BASE_URL, { params: filters, }); if (!response.data.success) { throw new Error(response.data.error || 'Error al obtener almacenes'); } return { data: response.data.data || [], total: response.data.total || response.data.data?.length || 0, }; }, // Get warehouse by ID getById: async (id: string): Promise => { const response = await api.get>(`${BASE_URL}/${id}`); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Almacen no encontrado'); } return response.data.data; }, // Get warehouse by code getByCode: async (code: string): Promise => { const response = await api.get>(`${BASE_URL}/code/${code}`); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Almacen no encontrado'); } return response.data.data; }, // Get default warehouse getDefault: async (): Promise => { const response = await api.get>(`${BASE_URL}/default`); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'No hay almacen por defecto'); } return response.data.data; }, // Get active warehouses getActive: async (): Promise => { const response = await api.get & { total?: number }>(`${BASE_URL}/active`); if (!response.data.success) { throw new Error(response.data.error || 'Error al obtener almacenes activos'); } return response.data.data || []; }, // Create warehouse create: async (data: CreateWarehouseDto): Promise => { const response = await api.post>(BASE_URL, data); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Error al crear almacen'); } return response.data.data; }, // Update warehouse update: async (id: string, data: UpdateWarehouseDto): Promise => { const response = await api.patch>(`${BASE_URL}/${id}`, data); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Error al actualizar almacen'); } return response.data.data; }, // Delete warehouse delete: async (id: string): Promise => { const response = await api.delete(`${BASE_URL}/${id}`); if (!response.data.success) { throw new Error(response.data.error || 'Error al eliminar almacen'); } }, }; // ==================== Locations API ==================== export const locationsApi = { // List all locations with filters list: async (filters?: LocationFilters): Promise => { const response = await api.get & { total?: number }>( `${BASE_URL}/locations`, { params: filters } ); if (!response.data.success) { throw new Error(response.data.error || 'Error al obtener ubicaciones'); } return { data: response.data.data || [], total: response.data.total || response.data.data?.length || 0, }; }, // Get location by ID getById: async (id: string): Promise => { const response = await api.get>(`${BASE_URL}/locations/${id}`); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Ubicacion no encontrada'); } return response.data.data; }, // Get locations by warehouse getByWarehouse: async (warehouseId: string): Promise => { const response = await api.get & { total?: number }>( `${BASE_URL}/${warehouseId}/locations` ); if (!response.data.success) { throw new Error(response.data.error || 'Error al obtener ubicaciones del almacen'); } return response.data.data || []; }, // Create location create: async (data: CreateLocationDto): Promise => { const response = await api.post>(`${BASE_URL}/locations`, data); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Error al crear ubicacion'); } return response.data.data; }, // Update location update: async (id: string, data: UpdateLocationDto): Promise => { const response = await api.patch>(`${BASE_URL}/locations/${id}`, data); if (!response.data.success || !response.data.data) { throw new Error(response.data.error || 'Error al actualizar ubicacion'); } return response.data.data; }, // Delete location delete: async (id: string): Promise => { const response = await api.delete(`${BASE_URL}/locations/${id}`); if (!response.data.success) { throw new Error(response.data.error || 'Error al eliminar ubicacion'); } }, };