Servicios API
The servicios_firestore.js module provides comprehensive functions for managing repair service orders, including creation, updates, duplicate detection, and status tracking.
Functions
guardarServicio
Creates a new service order with automatic folio generation and duplicate detection.
import { guardarServicio } from './services/servicios_firestore' ;
const result = await guardarServicio ({
clienteId: 'abc123' ,
nombre: 'Juan Pérez' ,
direccion: 'Calle Principal 123' ,
telefono: '5551234567' ,
tipoDispositivo: 'laptop' ,
marca: 'HP' ,
modelo: 'Pavilion' ,
numeroSerie: 'SN12345' ,
trabajo: 'No enciende' ,
costo: '500' ,
procesador: 'Intel i5' ,
ram: '8GB' ,
disco: '500GB'
});
console . log ( result ); // { id: 'doc123', folio: 'HP-001' }
The service form data object Client document ID reference
Device type: laptop, pc, impresora, or monitor
Serial number (omitted if omitirNumeroSerie is true)
Skip serial number requirement
Description of work/issue
Service cost (empty if precioDespues is true)
Price to be determined later
caracteristicasPendientes
Device characteristics pending
Manual folio (auto-generated if not provided)
Show laptop/pc specific fields
Show impresora specific fields
Show monitor specific fields
The created service document ID
The generated or provided folio number
Throws:
Error with code DUPLICATE_SERVICE if an active duplicate service exists
Error if folio generation fails
Error if folio already exists
buscarServicioDuplicadoActivo
Searches for active duplicate services based on device and customer information.
import { buscarServicioDuplicadoActivo } from './services/servicios_firestore' ;
const duplicado = await buscarServicioDuplicadoActivo ({
telefono: '5551234567' ,
tipoDispositivo: 'laptop' ,
marca: 'HP' ,
modelo: 'Pavilion' ,
trabajo: 'No enciende'
});
if ( duplicado ) {
console . log ( `Duplicate found: ${ duplicado . folio } ` );
}
Object with service data to check for duplicates
Returns the duplicate service object if found, or null if no duplicate exists
buscarServicioPorFolio
Searches for a service by its folio number using an indexed lookup.
import { buscarServicioPorFolio } from './services/servicios_firestore' ;
const servicio = await buscarServicioPorFolio ( 'HP-001' );
if ( servicio ) {
console . log ( `Service ID: ${ servicio . id } ` );
console . log ( `Status: ${ servicio . status } ` );
}
The folio number to search for
Returns the service object with id and all service data, or null if not found
listarServiciosPendientes
Lists all services that are not in a final status (entregado, cancelado, no_reparable).
import { listarServiciosPendientes } from './services/servicios_firestore' ;
const pendientes = await listarServiciosPendientes ();
pendientes . forEach ( servicio => {
console . log ( ` ${ servicio . folio } : ${ servicio . status } ` );
});
Array of service objects ordered by creation date (newest first)
listarServiciosHistorial
Lists all services in final status (entregado, cancelado, no_reparable).
import { listarServiciosHistorial } from './services/servicios_firestore' ;
const historial = await listarServiciosHistorial ();
console . log ( `Total completed services: ${ historial . length } ` );
Array of completed service objects ordered by creation date (newest first)
actualizarServicioPorId
Updates an existing service by its document ID. Prevents folio changes and locks services in final status.
import { actualizarServicioPorId } from './services/servicios_firestore' ;
const updated = await actualizarServicioPorId ( 'doc123' , {
status: 'en_reparacion' ,
costo: '750'
});
console . log ( updated );
Object containing fields to update
The updated service object with id and all current data
Special behavior:
Automatically sets fechaEntregado timestamp when status changes to “entregado”
Clears fechaEntregado when status changes away from “entregado”
Locks service and sets locked: true when reaching final status
Prevents folio changes on existing services
Throws:
Error if service not found
Error if attempting to change folio
listarServiciosPorClienteId
Lists all services associated with a specific client ID.
import { listarServiciosPorClienteId } from './services/servicios_firestore' ;
const servicios = await listarServiciosPorClienteId ( 'abc123' );
servicios . forEach ( s => {
console . log ( `Folio: ${ s . folio } , Status: ${ s . status } ` );
});
Array of service objects for the specified client, ordered by creation date (newest first)
guardarOActualizarPorFolio
Upsert operation: creates a new service if folio doesn’t exist, or updates existing service.
import { guardarOActualizarPorFolio } from './services/servicios_firestore' ;
const result = await guardarOActualizarPorFolio ({
folio: 'HP-001' ,
nombre: 'Juan Pérez' ,
telefono: '5551234567' ,
tipoDispositivo: 'laptop' ,
marca: 'HP' ,
modelo: 'Pavilion' ,
trabajo: 'No enciende' ,
costo: '500'
});
if ( result . mode === 'created' ) {
console . log ( 'New service created' );
} else {
console . log ( 'Existing service updated' );
}
Service form data (same structure as guardarServicio)
Either "created" or "updated"
Status Values
Services track their lifecycle through status values:
pendiente - Initial status, awaiting diagnosis
en_reparacion - Currently being repaired
entregado - Final status - Delivered to customer
cancelado - Final status - Service cancelled
no_reparable - Final status - Cannot be repaired
Final statuses automatically lock the service and prevent further modifications.
Data Structure
Each service document contains:
{
id : 'doc123' ,
folio : 'HP-001' ,
clienteId : 'abc123' ,
nombre : 'Juan Pérez' ,
direccion : 'Calle Principal 123' ,
telefono : '5551234567' ,
tipoDispositivo : 'laptop' ,
marca : 'HP' ,
modelo : 'Pavilion' ,
numeroSerie : 'SN12345' ,
omitirNumeroSerie : false ,
trabajo : 'No enciende' ,
trabajoNorm : 'no enciende' , // normalized for search
costo : '500' ,
precioDespues : false ,
caracteristicasPendientes : false ,
status : 'pendiente' ,
entregado : false ,
fechaEntregado : null , // timestamp when delivered
locked : false ,
lockedReason : null ,
dedupeKey : 'tel|tipo|marca|modelo' , // for duplicate detection
createdAt : Timestamp ,
updatedAt : Timestamp ,
// Device-specific fields
laptopPc : { /* ... */ },
impresora : { /* ... */ },
monitor : { /* ... */ }
}