Files
parcer/server/src/routes/integration.ts

73 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-09-18 07:28:57 +05:30
import { Router } from 'express';
import fs from 'fs';
import path from 'path';
import logger from "../logger";
2024-09-19 09:00:56 +05:30
import { writeDataToSheet } from "../workflow-management/integrations/gsheet"
import { readFile } from "../workflow-management/storage";
export const router = Router();
2024-09-18 20:29:47 +05:30
// Temproary Path to the JSON file that will store the integration details
const integrationsFilePath = path.join(__dirname, 'integrations.json');
2024-09-18 21:43:41 +05:30
export function loadIntegrations() {
2024-09-18 20:29:47 +05:30
if (fs.existsSync(integrationsFilePath)) {
const data = fs.readFileSync(integrationsFilePath, 'utf-8');
return JSON.parse(data);
}
return {};
}
function saveIntegrations(integrations: any) {
fs.writeFileSync(integrationsFilePath, JSON.stringify(integrations, null, 2));
}
router.post('/upload-credentials', async (req, res) => {
2024-09-19 17:43:42 +05:30
try {
2024-09-18 20:00:42 +05:30
const { fileName, credentials, spreadsheetId, range } = req.body;
if (!fileName || !credentials || !spreadsheetId || !range) {
2024-09-19 17:43:42 +05:30
return res.status(400).json({ message: 'Credentials, Spreadsheet ID, and Range are required.' });
}
2024-09-18 18:47:59 +05:30
// Store the credentials in a secure place (for test, we store them locally)
2024-09-18 20:29:47 +05:30
// Load existing integrations from the JSON file
const integrations = loadIntegrations();
2024-09-19 17:38:35 +05:30
integrations[fileName] = { fileName, spreadsheetId, range, credentials };
2024-09-18 20:29:47 +05:30
saveIntegrations(integrations);
2024-09-18 22:33:01 +05:30
logger.log('info', 'Service account credentials saved successfully.');
return res.send(true);
2024-09-19 17:43:42 +05:30
} catch (error: any) {
logger.log('error', `Error saving credentials: ${error.message}`);
return res.status(500).json({ message: 'Failed to save credentials.', error: error.message });
}
2024-09-17 20:58:30 +05:30
});
2024-09-19 09:00:56 +05:30
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) {
2024-09-19 17:38:35 +05:30
const data = parsedRun.serializableOutput['item-0'] as { [key: string]: any }[];
const integrationConfig = await loadIntegrations();
2024-09-19 17:43:42 +05:30
if (integrationConfig) {
2024-09-19 17:38:35 +05:30
const { spreadsheetId, range, credentials } = integrationConfig;
2024-09-19 17:38:35 +05:30
if (spreadsheetId && range && credentials) {
// 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));
2024-09-19 17:38:35 +05:30
const outputData = [headers, ...rows];
2024-09-19 17:38:35 +05:30
await writeDataToSheet(spreadsheetId, range, outputData);
logger.log('info', `Data written to Google Sheet successfully for ${fileName}_${runId}`);
}
}
2024-09-19 17:38:35 +05:30
logger.log('error', `Google Sheet integration not configured for ${fileName}_${runId}`);
}
2024-09-19 17:38:35 +05:30
logger.log('error', `Run not successful or no data to update for ${fileName}_${runId}`);
} catch (error: any) {
logger.log('error', `Failed to write data to Google Sheet for ${fileName}_${runId}: ${error.message}`);
}
2024-09-19 17:38:35 +05:30
};