terratech-workflow.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use server';
  2. import { createImport, triggerImportProcess } from './imports';
  3. import { getLayoutConfigurations } from './layout-configurations';
  4. /**
  5. * Creates an import record for TerraTech GasOilWater 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 createTerraTechImportRecord(fileId: string, fileName: string) {
  11. try {
  12. // Step 1: Find the "TerraTech - GasOilWater Summary" 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 terraTechLayout = layoutsResult.data.find(
  18. (layout: { id: number; name: string }) => layout.name === 'TerraTech - GasOilWater Summary'
  19. );
  20. if (!terraTechLayout) {
  21. throw new Error('Layout configuration "TerraTech - GasOilWater Summary" not found');
  22. }
  23. // Step 2: Create import record with the layout configuration
  24. const importName = `TerraTech - ${fileName}`;
  25. const importResult = await createImport({
  26. name: importName,
  27. layoutId: terraTechLayout.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: terraTechLayout
  37. };
  38. } catch (error) {
  39. console.error('Error creating TerraTech import record:', error);
  40. return {
  41. success: false,
  42. error: error instanceof Error ? error.message : 'Failed to create TerraTech import record'
  43. };
  44. }
  45. }
  46. /**
  47. * Processes the TerraTech 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 processTerraTechImportData(importId: number) {
  52. try {
  53. console.log(`Starting TerraTech import data processing for import ID: ${importId}`);
  54. // Use the existing triggerImportProcess function from imports.ts
  55. const result = await triggerImportProcess(importId);
  56. if (result.success) {
  57. console.log('TerraTech import data processing completed successfully:', result);
  58. return {
  59. success: true,
  60. data: result,
  61. message: result.message || `Successfully imported records`
  62. };
  63. } else {
  64. console.error('TerraTech import data processing failed:', result);
  65. return {
  66. success: false,
  67. error: result.error || 'Failed to process import data'
  68. };
  69. }
  70. } catch (error) {
  71. console.error('Error processing TerraTech import data:', error);
  72. return {
  73. success: false,
  74. error: error instanceof Error ? error.message : 'Failed to process TerraTech import data'
  75. };
  76. }
  77. }
  78. /**
  79. * Gets the TerraTech - GasOilWater Summary layout configuration
  80. * @returns The layout configuration or null if not found
  81. */
  82. export async function getTerraTechLayout() {
  83. try {
  84. const layoutsResult = await getLayoutConfigurations();
  85. if (!layoutsResult.success) {
  86. return null;
  87. }
  88. return layoutsResult.data?.find(
  89. (layout: { id: number; name: string }) => layout.name === 'TerraTech - GasOilWater Summary'
  90. ) || null;
  91. } catch (error) {
  92. console.error('Error fetching TerraTech layout:', error);
  93. return null;
  94. }
  95. }