Overview
The requisitions module provides server actions for creating, reading, updating, and deleting requisitions (procurement requests). All operations include role-based access control and maintain an audit trail.
All functions are Next.js Server Actions (marked with 'use server').
createRequisicion
Creates a new requisition. Requires admin or coordinadora role.
async function createRequisicion (
data : RequisicionFormData
) : Promise <{ data ?: Requisicion ; error ?: string }>
data
RequisicionFormData
required
Requisition data to create Reception date (ISO 8601 format)
Supplier ID (foreign key to proveedores)
Product ID (foreign key to productos)
Presentation ID (foreign key to presentaciones)
Destination ID (foreign key to destinos)
Status ID (foreign key to estatus)
Unit ID (foreign key to unidades)
Returns
The created requisition object with generated ID
Error message if creation fails
Example
import { createRequisicion } from '@/lib/actions/requisiciones'
const result = await createRequisicion ({
fecha_recepcion: '2026-03-05' ,
proveedor_id: 'uuid-proveedor' ,
producto_id: 'uuid-producto' ,
presentacion_id: 'uuid-presentacion' ,
destino_id: 'uuid-destino' ,
estatus_id: 'uuid-estatus' ,
cantidad_solicitada: 100 ,
unidad_cantidad_id: 'uuid-unidad' ,
requisicion_numero: 'REQ-2026-001' ,
comentarios: 'Urgente para producción'
})
if ( result . error ) {
console . error ( 'Failed to create:' , result . error )
} else {
console . log ( 'Created requisition:' , result . data . id )
}
updateRequisicion
Updates an existing requisition and logs changes to the audit trail. Requires admin or coordinadora role.
async function updateRequisicion (
id : string ,
data : Partial < RequisicionFormData >,
camposModificados : Array <{
campo : string ;
anterior : string ;
nuevo : string ;
}>
) : Promise <{ data ?: Requisicion ; error ?: string }>
ID of the requisition to update
data
Partial<RequisicionFormData>
required
Fields to update (any subset of RequisicionFormData)
Array of change records for audit trail Name of the modified field
Previous value (as string)
Returns
The updated requisition object
Error message if update fails
The audit trail entries are automatically written to requisiciones_historial table with the current user’s ID.
Example
import { updateRequisicion } from '@/lib/actions/requisiciones'
const result = await updateRequisicion (
'requisicion-uuid' ,
{
cantidad_entregada: 95 ,
fecha_entregado: '2026-03-10'
},
[
{
campo: 'cantidad_entregada' ,
anterior: 'null' ,
nuevo: '95'
},
{
campo: 'fecha_entregado' ,
anterior: 'null' ,
nuevo: '2026-03-10'
}
]
)
if ( result . error ) {
console . error ( 'Failed to update:' , result . error )
}
deleteRequisicion
Deletes a requisition and its audit history (via CASCADE). Requires admin or coordinadora role.
async function deleteRequisicion (
id : string
) : Promise <{ success ?: boolean ; error ?: string }>
ID of the requisition to delete
Returns
true if deletion was successful
Error message if deletion fails
This operation is permanent. The requisition and all associated history records will be deleted.
Example
import { deleteRequisicion } from '@/lib/actions/requisiciones'
const result = await deleteRequisicion ( 'requisicion-uuid' )
if ( result . error ) {
console . error ( 'Failed to delete:' , result . error )
} else {
console . log ( 'Requisition deleted successfully' )
}
getRequisiciones
Retrieves requisitions with optional filters. Includes all related catalog data via joins.
async function getRequisiciones (
filters ?: RequisicionFilters
) : Promise <{ data ?: Requisicion []; error ?: string }>
Optional filters to apply Start date for date range filter (ISO 8601)
End date for date range filter (ISO 8601)
Search term (not yet implemented in this function)
Returns
Array of requisitions with populated relations (proveedor, producto, presentacion, destino, estatus, unidad_cantidad)
Error message if query fails
Example
import { getRequisiciones } from '@/lib/actions/requisiciones'
// Get all requisitions
const all = await getRequisiciones ()
// Get filtered requisitions
const filtered = await getRequisiciones ({
proveedor_id: 'supplier-uuid' ,
fecha_desde: '2026-01-01' ,
fecha_hasta: '2026-03-31' ,
estatus_id: 'status-uuid'
})
if ( filtered . data ) {
filtered . data . forEach ( req => {
console . log ( ` ${ req . requisicion_numero } : ${ req . proveedor ?. nombre } ` )
})
}
getCatalogos
Retrieves all catalog data (suppliers, products, presentations, destinations, statuses, and units).
async function getCatalogos () : Promise < Catalogos >
Returns
Object containing all catalog arrays
All catalog arrays are sorted alphabetically by name.
Example
import { getCatalogos } from '@/lib/actions/requisiciones'
const catalogos = await getCatalogos ()
console . log ( 'Suppliers:' , catalogos . proveedores )
console . log ( 'Products:' , catalogos . productos )
console . log ( 'Statuses:' , catalogos . estatus )
getHistorial
Retrieves audit trail history for requisitions.
async function getHistorial (
requisicionId ?: string
) : Promise <{ data ?: RequisicionHistorial []; error ?: string }>
Optional ID to filter history for a specific requisition. If omitted, returns all history.
Returns
Array of history records with populated user profiles, sorted by creation date (newest first)
Error message if query fails
Example
import { getHistorial } from '@/lib/actions/requisiciones'
// Get history for specific requisition
const history = await getHistorial ( 'requisicion-uuid' )
if ( history . data ) {
history . data . forEach ( entry => {
console . log ( ` ${ entry . campo_modificado } : ${ entry . valor_anterior } → ${ entry . valor_nuevo } ` )
console . log ( `Changed by: ${ entry . profiles ?. nombre_completo } ` )
})
}
// Get all history
const allHistory = await getHistorial ()