From 9e990204ce0eca1430ebf062b6408af70beb91cd Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Thu, 19 Sep 2024 12:52:58 +0530 Subject: [PATCH] feat(temp): standalone gsheet update fxn --- server/src/routes/integration.ts | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/server/src/routes/integration.ts b/server/src/routes/integration.ts index f342624a..47069b46 100644 --- a/server/src/routes/integration.ts +++ b/server/src/routes/integration.ts @@ -79,8 +79,40 @@ router.post('/update-google-sheet/:fileName/:runId', async (req, res) => { return res.status(400).send({ success: false, message: 'Google Sheet integration not configured' }); } return res.status(400).send({ success: false, message: 'Run not successful or no data to update' }); - } catch (error) { + } catch (error: any) { logger.log('error', `Failed to write data to Google Sheet for ${req.params.fileName}_${req.params.runId}: ${error.message}`); return res.status(500).send({ success: false, message: 'Failed to update Google Sheet', error: error.message }); } }); + +export async function updateGoogleSheet(fileName: string, runId: string) { + try { + const run = await readFile(`./../storage/runs/${fileName}_${runId}.json`); + const parsedRun = JSON.parse(run); + + if (parsedRun.status === 'success' && parsedRun.serializableOutput) { + const data = parsedRun.serializableOutput as { [key: string]: any }[]; + const integrationConfig = await loadIntegrations(); // Assume this function loads config + + if (integrationConfig) { + const { spreadsheetId, sheetName } = integrationConfig; + + if (spreadsheetId && sheetName) { + // Convert data to Google Sheets format (headers and rows) + const headers = Object.keys(data[0]); + const rows = data.map((row: { [key: string]: any }) => Object.values(row)); + const outputData = [headers, ...rows]; // Include headers + + await writeDataToSheet(spreadsheetId, sheetName, outputData); + logger.log('info', `Data written to Google Sheet successfully for ${fileName}_${runId}`); + } + } else { + logger.log('error', 'Google Sheet integration not configured'); + } + } else { + logger.log('error', 'Run not successful or no data to update'); + } + } catch (error: any) { + logger.log('error', `Failed to write data to Google Sheet for ${fileName}_${runId}: ${error.message}`); + } +} \ No newline at end of file