'use server'; import { prisma } from '@/lib/prisma'; import { revalidatePath } from 'next/cache'; import { z } from 'zod'; // Validation schemas const createImportSchema = z.object({ name: z.string().min(1, 'Import name is required'), layoutId: z.number().int().positive('Layout configuration is required'), fileId: z.string().optional(), }); const updateImportSchema = z.object({ id: z.number().int().positive(), name: z.string().min(1, 'Import name is required'), fileId: z.string().optional(), }); // Create a new import export async function createImport(data: { name: string; layoutId: number; fileId?: string; }) { try { const validatedData = createImportSchema.parse(data); const importRecord = await prisma.import.create({ data: { name: validatedData.name, layoutId: validatedData.layoutId, importDate: new Date(), ...(validatedData.fileId && { fileId: validatedData.fileId }), }, include: { layout: true, }, }); revalidatePath('/imports'); return { success: true, data: importRecord }; } catch (error) { console.error('Error creating import:', error); return { success: false, error: 'Failed to create import' }; } } // Get all imports export async function getImports() { try { const imports = await prisma.import.findMany({ include: { layout: true, }, orderBy: { importDate: 'desc', }, }); return { success: true, data: imports }; } catch (error) { console.error('Error fetching imports:', error); return { success: false, error: 'Failed to fetch imports' }; } } // Get a single import by ID export async function getImportById(id: number) { try { const importRecord = await prisma.import.findUnique({ where: { id }, include: { layout: { include: { sections: { include: { fields: true, }, }, }, }, cintasSummaries: { orderBy: { weekId: 'desc', }, }, }, }); if (!importRecord) { return { success: false, error: 'Import not found' }; } return { success: true, data: importRecord }; } catch (error) { console.error('Error fetching import:', error); return { success: false, error: 'Failed to fetch import' }; } } // Update an import export async function updateImport(data: { id: number; name: string; fileId?: string; }) { try { const validatedData = updateImportSchema.parse(data); const importRecord = await prisma.import.update({ where: { id: validatedData.id }, data: { name: validatedData.name, ...(validatedData.fileId !== undefined && { fileId: validatedData.fileId }), }, include: { layout: true, }, }); revalidatePath('/imports'); return { success: true, data: importRecord }; } catch (error) { console.error('Error updating import:', error); return { success: false, error: 'Failed to update import' }; } } // Delete an import export async function deleteImport(id: number) { try { await prisma.import.delete({ where: { id }, }); revalidatePath('/imports'); return { success: true }; } catch (error) { console.error('Error deleting import:', error); return { success: false, error: 'Failed to delete import' }; } } // Calculate Cintas summaries for an import export async function calculateCintasSummaries(importId: number) { try { // This would typically call a stored procedure or perform calculations // For now, we'll simulate the calculation // In a real implementation, you might call: // await prisma.$executeRaw`CALL cintas_calculate_summary(${importId})`; // For demo purposes, we'll create some sample data const summaries = [ { importId, week: '2024-W01', trrTotal: 100, fourWkAverages: 95, trrPlus4Wk: 195, powerAdds: 25, weekId: 1, }, { importId, week: '2024-W02', trrTotal: 110, fourWkAverages: 100, trrPlus4Wk: 210, powerAdds: 30, weekId: 2, }, ]; // Clear existing summaries for this import await prisma.cintasSummary.deleteMany({ where: { importId }, }); // Create new summaries const createdSummaries = await Promise.all( summaries.map(summary => prisma.cintasSummary.create({ data: summary, }) ) ); return { success: true, data: createdSummaries }; } catch (error) { console.error('Error calculating Cintas summaries:', error); return { success: false, error: 'Failed to calculate summaries' }; } } // Get available layout configurations export async function getLayoutConfigurations() { try { const layouts = await prisma.layoutConfiguration.findMany({ include: { sections: { include: { fields: true, }, }, }, orderBy: { name: 'asc', }, }); return { success: true, data: layouts }; } catch (error) { console.error('Error fetching layout configurations:', error); return { success: false, error: 'Failed to fetch layout configurations' }; } } // Trigger import process export async function triggerImport(importId: number) { try { // Validate import exists const importRecord = await prisma.import.findUnique({ where: { id: importId }, include: { file: true, layout: true } }); if (!importRecord) { return { success: false, error: 'Import not found' }; } if (!importRecord.file) { return { success: false, error: 'No file attached to import' }; } if (!importRecord.layout) { return { success: false, error: 'No layout configuration found' }; } // Return success - the actual processing will be handled by the API endpoint return { success: true, message: 'Import process triggered successfully' }; } catch (error) { console.error('Error triggering import:', error); return { success: false, error: 'Failed to trigger import' }; } }