| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use server';
- import { createImport, triggerImportProcess } from './imports';
- import { getLayoutConfigurations } from './layout-configurations';
- /**
- * Creates an import record for TerraTech GasOilWater workflow
- * @param fileId - The uploaded file ID from blob storage
- * @param fileName - The original file name
- * @returns The created import record
- */
- export async function createTerraTechImportRecord(fileId: string, fileName: string) {
- try {
- // Step 1: Find the "TerraTech - GasOilWater Summary" layout configuration
- const layoutsResult = await getLayoutConfigurations();
- if (!layoutsResult.success || !layoutsResult.data) {
- throw new Error('Failed to fetch layout configurations');
- }
- const terraTechLayout = layoutsResult.data.find(
- (layout: { id: number; name: string }) => layout.name === 'TerraTech - GasOilWater Summary'
- );
- if (!terraTechLayout) {
- throw new Error('Layout configuration "TerraTech - GasOilWater Summary" not found');
- }
- // Step 2: Create import record with the layout configuration
- const importName = `TerraTech - ${fileName}`;
- const importResult = await createImport({
- name: importName,
- layoutId: terraTechLayout.id,
- fileId: fileId,
- });
- if (!importResult.success) {
- throw new Error(importResult.error || 'Failed to create import record');
- }
- return {
- success: true,
- data: importResult.data,
- layout: terraTechLayout
- };
- } catch (error) {
- console.error('Error creating TerraTech import record:', error);
- return {
- success: false,
- error: error instanceof Error ? error.message : 'Failed to create TerraTech import record'
- };
- }
- }
- /**
- * Processes the TerraTech 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 processTerraTechImportData(importId: number) {
- try {
- console.log(`Starting TerraTech import data processing for import ID: ${importId}`);
- // Use the existing triggerImportProcess function from imports.ts
- const result = await triggerImportProcess(importId);
- if (result.success) {
- console.log('TerraTech import data processing completed successfully:', result);
- return {
- success: true,
- data: result,
- message: result.message || `Successfully imported records`
- };
- } else {
- console.error('TerraTech import data processing failed:', result);
- return {
- success: false,
- error: result.error || 'Failed to process import data'
- };
- }
- } catch (error) {
- console.error('Error processing TerraTech import data:', error);
- return {
- success: false,
- error: error instanceof Error ? error.message : 'Failed to process TerraTech import data'
- };
- }
- }
- /**
- * Gets the TerraTech - GasOilWater Summary layout configuration
- * @returns The layout configuration or null if not found
- */
- export async function getTerraTechLayout() {
- try {
- const layoutsResult = await getLayoutConfigurations();
- if (!layoutsResult.success) {
- return null;
- }
- return layoutsResult.data?.find(
- (layout: { id: number; name: string }) => layout.name === 'TerraTech - GasOilWater Summary'
- ) || null;
- } catch (error) {
- console.error('Error fetching TerraTech layout:', error);
- return null;
- }
- }
|