cintas-workflow.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use server';
  2. import { createImport } from './imports';
  3. import { getLayoutConfigurations } from './layout-configurations';
  4. /**
  5. * Creates an import record for Cintas Install Calendar workflow
  6. * @param fileId - The uploaded file ID from blob storage
  7. * @param fileName - The original file name
  8. * @returns The created import record
  9. */
  10. export async function createCintasImportRecord(fileId: string, fileName: string) {
  11. try {
  12. // Step 1: Find the "Cintas Install Calendar" layout configuration
  13. const layoutsResult = await getLayoutConfigurations();
  14. if (!layoutsResult.success || !layoutsResult.data) {
  15. throw new Error('Failed to fetch layout configurations');
  16. }
  17. const cintasLayout = layoutsResult.data.find(
  18. (layout: { id: number; name: string }) => layout.name === 'Cintas Install Calendar'
  19. );
  20. if (!cintasLayout) {
  21. throw new Error('Layout configuration "Cintas Install Calendar" not found');
  22. }
  23. // Step 2: Create import record with the layout configuration
  24. const importName = `Cintas Install Calendar - ${fileName}`;
  25. const importResult = await createImport({
  26. name: importName,
  27. layoutId: cintasLayout.id,
  28. fileId: fileId,
  29. });
  30. if (!importResult.success) {
  31. throw new Error(importResult.error || 'Failed to create import record');
  32. }
  33. return {
  34. success: true,
  35. data: importResult.data,
  36. layout: cintasLayout
  37. };
  38. } catch (error) {
  39. console.error('Error creating Cintas import record:', error);
  40. return {
  41. success: false,
  42. error: error instanceof Error ? error.message : 'Failed to create Cintas import record'
  43. };
  44. }
  45. }
  46. /**
  47. * Processes the Cintas import by reading the Excel file and importing data into PostgreSQL
  48. * @param importId - The import record ID to process
  49. * @returns The processing result
  50. */
  51. export async function processCintasImportData(importId: number) {
  52. try {
  53. console.log(`Starting Cintas import data processing for import ID: ${importId}`);
  54. // Import the processCintasImport function
  55. const { processCintasImport } = await import('./process-cintas-import');
  56. // Process the import
  57. const result = await processCintasImport(importId);
  58. if (result.success) {
  59. console.log('Cintas import data processing completed successfully:', result);
  60. return {
  61. success: true,
  62. data: result,
  63. message: `Successfully imported ${'totalInserted' in result ? result.totalInserted : 0} records`
  64. };
  65. } else {
  66. console.error('Cintas import data processing failed:', result);
  67. return {
  68. success: false,
  69. error: 'error' in result ? result.error : 'Failed to process import data'
  70. };
  71. }
  72. } catch (error) {
  73. console.error('Error processing Cintas import data:', error);
  74. return {
  75. success: false,
  76. error: error instanceof Error ? error.message : 'Failed to process Cintas import data'
  77. };
  78. }
  79. }
  80. /**
  81. * Gets the Cintas Install Calendar layout configuration
  82. * @returns The layout configuration or null if not found
  83. */
  84. export async function getCintasInstallCalendarLayout() {
  85. try {
  86. const layoutsResult = await getLayoutConfigurations();
  87. if (!layoutsResult.success) {
  88. return null;
  89. }
  90. return layoutsResult.data?.find(
  91. (layout: { id: number; name: string }) => layout.name === 'Cintas Install Calendar'
  92. ) || null;
  93. } catch (error) {
  94. console.error('Error fetching Cintas layout:', error);
  95. return null;
  96. }
  97. }