cintas-workflow.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 => 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. * Gets the Cintas Install Calendar layout configuration
  48. * @returns The layout configuration or null if not found
  49. */
  50. export async function getCintasInstallCalendarLayout() {
  51. try {
  52. const layoutsResult = await getLayoutConfigurations();
  53. if (!layoutsResult.success) {
  54. return null;
  55. }
  56. return layoutsResult.data?.find(
  57. layout => layout.name === 'Cintas Install Calendar'
  58. ) || null;
  59. } catch (error) {
  60. console.error('Error fetching Cintas layout:', error);
  61. return null;
  62. }
  63. }