Skip to main content

Overview

The catalog actions module provides server actions for creating, updating, toggling status, and deleting catalog entries across all catalog tables. All operations require either admin or coordinadora role, except permanent deletion which requires admin role. All functions are Next.js Server Actions (marked with 'use server').

createCatalogEntry

Creates a new entry in any catalog table. Requires admin or coordinadora role.
async function createCatalogEntry(
  table: string,
  data: any
): Promise<{ data?: any; error?: string }>
table
string
required
Name of the catalog table: proveedores, productos, presentaciones, destinos, estatus, or unidades
data
object
required
Catalog entry data. Structure depends on the table:

Returns

data
object
The created catalog entry with generated ID and activo: true
error
string
Error message if creation fails
All new catalog entries are automatically set to activo: true.

Example

import { createCatalogEntry } from '@/lib/actions/catalogos'

// Create a new supplier
const supplier = await createCatalogEntry('proveedores', {
  nombre: 'Nuevo Proveedor SA'
})

// Create a new product
const product = await createCatalogEntry('productos', {
  nombre: 'Cloro Industrial',
  descripcion: 'Hipoclorito de sodio 13%'
})

// Create a new status
const status = await createCatalogEntry('estatus', {
  nombre: 'En Tránsito',
  color_hex: '#FFA500'
})

// Create a new unit
const unit = await createCatalogEntry('unidades', {
  nombre: 'Kilogramo',
  abreviatura: 'kg'
})

if (supplier.error) {
  console.error('Failed to create:', supplier.error)
} else {
  console.log('Created:', supplier.data)
}

updateCatalogEntry

Updates an existing catalog entry. Requires admin or coordinadora role.
async function updateCatalogEntry(
  table: string,
  id: string,
  data: any
): Promise<{ data?: any; error?: string }>
table
string
required
Name of the catalog table
id
string
required
ID of the entry to update
data
object
required
Fields to update (structure depends on table)

Returns

data
object
The updated catalog entry
error
string
Error message if update fails

Example

import { updateCatalogEntry } from '@/lib/actions/catalogos'

// Update supplier name
const result = await updateCatalogEntry(
  'proveedores',
  'supplier-uuid',
  { nombre: 'Proveedor Actualizado SA' }
)

// Update product description
await updateCatalogEntry(
  'productos',
  'product-uuid',
  { descripcion: 'Nueva descripción del producto' }
)

// Update status color
await updateCatalogEntry(
  'estatus',
  'status-uuid',
  { color_hex: '#FF0000' }
)

if (result.error) {
  console.error('Failed to update:', result.error)
}

toggleCatalogStatus

Toggles the active/inactive status of a catalog entry. Requires admin or coordinadora role.
async function toggleCatalogStatus(
  table: string,
  id: string,
  active: boolean
): Promise<{ success?: boolean; error?: string }>
table
string
required
Name of the catalog table
id
string
required
ID of the entry to update
active
boolean
required
New active status: true to activate, false to deactivate

Returns

success
boolean
true if update was successful
error
string
Error message if update fails
Deactivating an entry does not delete it - it simply marks it as inactive and typically hides it from dropdowns and selectors.

Example

import { toggleCatalogStatus } from '@/lib/actions/catalogos'

// Deactivate a supplier
const result = await toggleCatalogStatus(
  'proveedores',
  'supplier-uuid',
  false
)

// Reactivate a product
await toggleCatalogStatus(
  'productos',
  'product-uuid',
  true
)

if (result.error) {
  console.error('Failed to toggle status:', result.error)
} else {
  console.log('Status updated successfully')
}

deleteCatalogEntry

Permanently deletes a catalog entry. Requires admin role.
async function deleteCatalogEntry(
  table: string,
  id: string
): Promise<{ success?: boolean; error?: string }>
table
string
required
Name of the catalog table
id
string
required
ID of the entry to delete

Returns

success
boolean
true if deletion was successful
error
string
Error message if deletion fails (including foreign key constraint violations)
This operation is permanent and cannot be undone. If the catalog entry is referenced by existing requisitions, the deletion will fail with a foreign key constraint error. In that case, consider using toggleCatalogStatus to deactivate it instead.

Example

import { deleteCatalogEntry } from '@/lib/actions/catalogos'

const result = await deleteCatalogEntry(
  'proveedores',
  'supplier-uuid'
)

if (result.error) {
  if (result.error.includes('está siendo utilizado')) {
    console.error('Cannot delete: entry is in use')
    // Consider deactivating instead
  } else {
    console.error('Failed to delete:', result.error)
  }
} else {
  console.log('Entry deleted successfully')
}

Catalog Tables

The following catalog tables are supported:
// Suppliers
{
  nombre: string
}

Build docs developers (and LLMs) love