'use server'; import { createImport } from './imports'; import { getLayoutConfigurations } from './layout-configurations'; /** * Creates an import record for Cintas Install Calendar workflow * @param fileId - The uploaded file ID from blob storage * @param fileName - The original file name * @returns The created import record */ export async function createCintasImportRecord(fileId: string, fileName: string) { try { // Step 1: Find the "Cintas Install Calendar" layout configuration const layoutsResult = await getLayoutConfigurations(); if (!layoutsResult.success || !layoutsResult.data) { throw new Error('Failed to fetch layout configurations'); } const cintasLayout = layoutsResult.data.find( (layout: { id: number; name: string }) => layout.name === 'Cintas Install Calendar' ); if (!cintasLayout) { throw new Error('Layout configuration "Cintas Install Calendar" not found'); } // Step 2: Create import record with the layout configuration const importName = `Cintas Install Calendar - ${fileName}`; const importResult = await createImport({ name: importName, layoutId: cintasLayout.id, fileId: fileId, }); if (!importResult.success) { throw new Error(importResult.error || 'Failed to create import record'); } return { success: true, data: importResult.data, layout: cintasLayout }; } catch (error) { console.error('Error creating Cintas import record:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to create Cintas import record' }; } } /** * Processes the Cintas import by reading the Excel file and importing data into PostgreSQL * @param importId - The import record ID to process * @returns The processing result */ export async function processCintasImportData(importId: number) { try { console.log(`Starting Cintas import data processing for import ID: ${importId}`); // Import the processCintasImport function const { processCintasImport } = await import('./process-cintas-import'); // Process the import const result = await processCintasImport(importId); if (result.success) { console.log('Cintas import data processing completed successfully:', result); return { success: true, data: result, message: `Successfully imported ${'totalInserted' in result ? result.totalInserted : 0} records` }; } else { console.error('Cintas import data processing failed:', result); return { success: false, error: 'error' in result ? result.error : 'Failed to process import data' }; } } catch (error) { console.error('Error processing Cintas import data:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to process Cintas import data' }; } } /** * Gets the Cintas Install Calendar layout configuration * @returns The layout configuration or null if not found */ export async function getCintasInstallCalendarLayout() { try { const layoutsResult = await getLayoutConfigurations(); if (!layoutsResult.success) { return null; } return layoutsResult.data?.find( layout => layout.name === 'Cintas Install Calendar' ) || null; } catch (error) { console.error('Error fetching Cintas layout:', error); return null; } }