Overview
The request system allows users to create spare part requests between warehouse locations using a cart-based workflow. Requests are tracked with status updates and detailed history.
useRequestsStore
Zustand store for managing request cart and destinations.
import { useRequestsStore } from '@/features/spares-request-workshop';
const {
cartItems,
loadCart,
addItemToCart,
removeItemFromCart
} = useRequestsStore();
State
Current items in the request cart
Available destination locations
Loading state for cart operations
Actions
loadCart
(locationId: string) => Promise<void>
Loads cart items for the specified location.await loadCart('uuid-location-id');
addItemToCart
(userId: string, locationId: string, repuestoId: string, quantity?: number) => Promise<void>
Adds item to cart with specified quantity.await addItemToCart(
'user-uuid',
'location-uuid',
'repuesto-uuid',
5
);
removeItemFromCart
(cartItemId: string) => Promise<void>
Removes item from cart.await removeItemFromCart('cart-item-uuid');
clearCartAfterSubmit
(cartItemIds: string[]) => Promise<void>
Clears specified items after successful request creation.await clearCartAfterSubmit(['id-1', 'id-2', 'id-3']);
setDestinations
(destinations: UserLocation[]) => void
Sets available destination locations.
API Functions
createRequest
Creates a new spare part request.
import { createRequest } from '@/features/spares-request-workshop/api';
await createRequest({
id_localizacion_origen: 'origin-uuid',
id_localizacion_destino: 'destination-uuid',
id_usuario_solicitante: 'user-uuid',
observaciones_generales: 'Urgent request for workshop',
items: [
{ id_repuesto: 'repuesto-1', cantidad: 5 },
{ id_repuesto: 'repuesto-2', cantidad: 3 }
]
});
Parameters
Origin warehouse location UUID
Destination warehouse location UUID
General observations/notes for the request
Array of spare parts and quantities to request
getCartItems
Fetches current cart items for a location.
import { getCartItems } from '@/features/spares-request-workshop/api';
const cartItems = await getCartItems('location-uuid');
addCartItem
Adds item to cart (upserts if exists).
import { addCartItem } from '@/features/spares-request-workshop/api';
await addCartItem(
'user-uuid',
'location-uuid',
'repuesto-uuid',
3 // quantity
);
removeCartItem
Removes single cart item.
import { removeCartItem } from '@/features/spares-request-workshop/api';
await removeCartItem('cart-item-uuid');
bulkRemoveCartItems
Removes multiple cart items at once.
import { bulkRemoveCartItems } from '@/features/spares-request-workshop/api';
await bulkRemoveCartItems(['id-1', 'id-2', 'id-3']);
getRequestHistory
Fetches request history.
import { getRequestHistory } from '@/entities/requests/api';
const history = await getRequestHistory();
Types
CartItem
interface CartItem {
id_item_carrito: string;
id_usuario: string;
id_localizacion: string;
cantidad: number;
created_at: string;
nombre_solicitante: string;
rol_solicitante: string;
id_repuesto: string;
referencia: string;
nombre_repuesto: string;
url_imagen: string | null;
stock_actual_en_taller: number;
}
CreateRequestData
interface CreateRequestData {
id_localizacion_origen: string;
id_localizacion_destino: string;
id_usuario_solicitante: string;
observaciones_generales?: string;
items: RequestItem[];
}
interface RequestItem {
id_repuesto: string;
cantidad: number;
}
RequestHistoryItem
interface RequestHistoryItem {
id_solicitud: string;
fecha_creacion: string;
estado: string;
observaciones_generales: string;
id_localizacion_destino: number;
nombre_destino: string;
id_localizacion_origen: number;
nombre_origen: string;
id_usuario_solicitante: string;
nombre_solicitante: string;
}
Example Usage
import { useRequestsStore } from '@/features/spares-request-workshop';
import { createRequest } from '@/features/spares-request-workshop/api';
import { useUserStore } from '@/entities/user';
import { useState } from 'react';
function CreateRequestPage() {
const [destination, setDestination] = useState('');
const [notes, setNotes] = useState('');
const { cartItems, loadCart, clearCartAfterSubmit } = useRequestsStore();
const currentLocation = useUserStore(state => state.currentLocation);
const sessionData = useUserStore(state => state.sessionData);
const handleSubmit = async () => {
if (!currentLocation || !sessionData?.user || !destination) return;
await createRequest({
id_localizacion_origen: currentLocation.id_localizacion,
id_localizacion_destino: destination,
id_usuario_solicitante: sessionData.user.id,
observaciones_generales: notes,
items: cartItems.map(item => ({
id_repuesto: item.id_repuesto,
cantidad: item.cantidad
}))
});
// Clear cart after successful creation
const cartItemIds = cartItems.map(item => item.id_item_carrito);
await clearCartAfterSubmit(cartItemIds);
// Reload cart to show updated state
await loadCart(currentLocation.id_localizacion);
};
return (
<div>
<h1>Create Request</h1>
<select value={destination} onChange={(e) => setDestination(e.target.value)}>
<option value="">Select destination...</option>
{/* Render destination options */}
</select>
<textarea
value={notes}
onChange={(e) => setNotes(e.target.value)}
placeholder="Add notes..."
/>
<div>
<h2>Cart Items ({cartItems.length})</h2>
{cartItems.map(item => (
<div key={item.id_item_carrito}>
<span>{item.nombre_repuesto}</span>
<span>Qty: {item.cantidad}</span>
</div>
))}
</div>
<button onClick={handleSubmit}>Create Request</button>
</div>
);
}