se supone que volvio
Some checks failed
build-and-deploy / filter (push) Successful in 2s
Sync to GitHub / sync (push) Failing after 1s
build-and-deploy / build (push) Successful in 54s
build-and-deploy / deploy (push) Successful in 15s

This commit is contained in:
2025-05-30 23:51:22 -06:00
parent 254beb897a
commit 32f2686f14
2108 changed files with 301539 additions and 19567 deletions

View File

@@ -17,31 +17,33 @@ const EXTERNAL_DB_NAME = process.env.EXTERNAL_DB_NAME || 'YOUR_EXTERNAL_DB_NAME'
const EXTERNAL_DB_EMPLEADOS_TABLE = process.env.EXTERNAL_DB_EMPLEADOS_TABLE || 'empleados_externos'; // User must set this.
// --- End of External Database Configuration ---
// Import types from the shared Prisma package.
// We are not importing PrismaClient here as the worker will fetch data from the API.
import type { Cliente } from '@empresa/prisma-schema'; // Assuming Cliente is the relevant type for employees
// Define a type for the employee data we expect from the API.
// This might be identical to Cliente or a subset, depending on the API endpoint.
type EmployeeDataFromAPI = Pick<Cliente, 'id' | 'name' | 'cedula' | 'ubicacion' | 'telefono'>;
import { PrismaClient } from '../api/prisma/generated/client';
const prisma = new PrismaClient();
async function syncEmpleadosToExternalDB() {
console.log('[SyncEmpleados] Starting synchronization process...');
// Core logic for synchronization, wrapped in try/catch/finally to ensure
// resources like the Prisma client are properly managed (e.g., disconnected).
try {
// TODO: Fetch employee data from the API (e.g., GET /api/empleados)
// This is a placeholder for the API call logic.
console.log('[SyncEmpleados] Fetching employee data from API...');
const response = await fetch('http://localhost:4000/api/empleados'); // Replace with your actual API endpoint
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const localEmpleados: EmployeeDataFromAPI[] = await response.json();
// Fetch all 'Cliente' records from the local Prisma database that are marked as 'empleado'.
const localEmpleados = await prisma.cliente.findMany({
where: { empleado: true }, // Filters for clients who are also employees
select: {
id: true, // Local ID, might be useful for logging/tracing
name: true,
cedula: true,
ubicacion: true,
telefono: true,
// avatar_url: true, // Add other relevant fields
// idciat: true, // Add other relevant fields
},
});
if (!localEmpleados || !localEmpleados.length) {
console.log('[SyncEmpleados] No employees found from API. Nothing to sync.');
if (!localEmpleados.length) {
console.log('[SyncEmpleados] No employees found in local database. Nothing to sync.');
return;
}
console.log(`[SyncEmpleados] Found ${localEmpleados.length} employees from API to sync.`);
console.log(`[SyncEmpleados] Found ${localEmpleados.length} employees to sync.`);
// --- External DB Connection Logic (User to implement based on EXTERNAL_DB_TYPE) ---
// User must implement the actual database connection logic here based on EXTERNAL_DB_TYPE
@@ -117,7 +119,9 @@ async function syncEmpleadosToExternalDB() {
// Catch any errors that occur during the synchronization process.
console.error('[SyncEmpleados] Error during synchronization:', error);
} finally {
// No Prisma client to disconnect as we are using API.
// Ensure the Prisma client is always disconnected, even if an error occurs.
await prisma.$disconnect();
console.log('[SyncEmpleados] Prisma client disconnected.');
}
console.log('[SyncEmpleados] Synchronization process finished.');
}
@@ -132,17 +136,19 @@ export { syncEmpleadosToExternalDB };
// 2. **Manual Testing:**
// * Once configured, you can test the script by running it directly: `node worker/sync-empleados.js`
// * Observe the console logs for any errors or successful completion messages.
// * Verify that data fetched from the API appears correctly (or is logged as intended for now)
// if you have implemented the external DB sync part.
// * The cron job in `worker/cron-worker.js` is scheduled to run this script.
// * Verify that data from your local 'Cliente' table (where empleado=true) appears correctly
// in your designated external database table (`EXTERNAL_DB_EMPLEADOS_TABLE`).
// * Check that subsequent runs correctly update existing records and insert new ones.
// * The cron job in `worker/cron-worker.js` is scheduled to run this script daily at midnight.
// You can monitor logs after this time to see its execution.
//
// 3. **Automated Testing (Recommendations for future development):**
// * **Unit Tests:**
// - Mock the API call (`fetch`) to return predefined employee data.
// - Mock the Prisma client (`../api/prisma/generated/client`) to return predefined employee data
// and spy on its methods.
// - Mock the external database client (e.g., `pg`, `mysql2`) to simulate connection,
// query execution (select, insert, update), and disconnections. This allows testing
// the sync logic without actual API or database dependencies.
// the sync logic without actual database dependencies.
// * **Integration Tests:**
// - If possible, set up a dedicated test instance of your external database.
// - Write tests that populate the local Prisma test database with sample employee data,