chore: remove stale code
This commit is contained in:
@@ -24,70 +24,59 @@ function saveIntegrations(integrations: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.post('/upload-credentials', async (req, res) => {
|
router.post('/upload-credentials', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { fileName, credentials, spreadsheetId, range } = req.body;
|
const { fileName, credentials, spreadsheetId, range } = req.body;
|
||||||
|
|
||||||
console.log(`fileName: ${fileName}, credentials: ${credentials}, spreadsheetId: ${spreadsheetId}, range: ${range}`);
|
|
||||||
|
|
||||||
if (!fileName || !credentials || !spreadsheetId || !range) {
|
if (!fileName || !credentials || !spreadsheetId || !range) {
|
||||||
return res.status(400).json({ message: 'Credentials, Spreadsheet ID, and Range are required.' });
|
return res.status(400).json({ message: 'Credentials, Spreadsheet ID, and Range are required.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the credentials in a secure place (for test, we store them locally)
|
// Store the credentials in a secure place (for test, we store them locally)
|
||||||
// Load existing integrations from the JSON file
|
// Load existing integrations from the JSON file
|
||||||
const integrations = loadIntegrations();
|
const integrations = loadIntegrations();
|
||||||
|
|
||||||
// Add or update the integration for the specific task (fileName)
|
|
||||||
integrations[fileName] = { fileName, spreadsheetId, range, credentials };
|
integrations[fileName] = { fileName, spreadsheetId, range, credentials };
|
||||||
|
|
||||||
// Save the updated integrations back to the file
|
|
||||||
saveIntegrations(integrations);
|
saveIntegrations(integrations);
|
||||||
logger.log('info', 'Service account credentials saved successfully.');
|
logger.log('info', 'Service account credentials saved successfully.');
|
||||||
|
|
||||||
return res.send(true);
|
return res.send(true);
|
||||||
|
} catch (error: any) {
|
||||||
//fs.writeFileSync(storedCredentialsPath, JSON.stringify(credentials));
|
logger.log('error', `Error saving credentials: ${error.message}`);
|
||||||
} catch (error: any) {
|
return res.status(500).json({ message: 'Failed to save credentials.', error: error.message });
|
||||||
logger.log('error', `Error saving credentials: ${error.message}`);
|
}
|
||||||
return res.status(500).json({ message: 'Failed to save credentials.', error: error.message });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/update-google-sheet/:fileName/:runId', async (req, res) => {
|
router.post('/update-google-sheet/:fileName/:runId', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const run = await readFile(`./../storage/runs/${req.params.fileName}_${req.params.runId}.json`);
|
const run = await readFile(`./../storage/runs/${req.params.fileName}_${req.params.runId}.json`);
|
||||||
const parsedRun = JSON.parse(run);
|
const parsedRun = JSON.parse(run);
|
||||||
|
|
||||||
if (parsedRun.status === 'success' && parsedRun.serializableOutput) {
|
|
||||||
const data = parsedRun.serializableOutput['item-0'] as { [key: string]: any }[];
|
|
||||||
const integrationConfig = await loadIntegrations();
|
|
||||||
|
|
||||||
logger.log(`info`, `integration config ${JSON.stringify(integrationConfig)}`)
|
if (parsedRun.status === 'success' && parsedRun.serializableOutput) {
|
||||||
|
const data = parsedRun.serializableOutput['item-0'] as { [key: string]: any }[];
|
||||||
if (integrationConfig) {
|
const integrationConfig = await loadIntegrations();
|
||||||
const { spreadsheetId, range, credentials } = integrationConfig;
|
|
||||||
|
|
||||||
logger.log(`info`, `data from routeeeeeeeeeeeeeeeee: ${JSON.stringify(data)}`)
|
logger.log(`info`, `integration config ${JSON.stringify(integrationConfig)}`)
|
||||||
|
|
||||||
if (spreadsheetId && range && credentials) {
|
if (integrationConfig) {
|
||||||
// Convert data to Google Sheets format (headers and rows)
|
const { spreadsheetId, range, credentials } = integrationConfig;
|
||||||
const headers = Object.keys(data[0]);
|
|
||||||
const rows = data.map((row: { [key: string]: any }) => Object.values(row));
|
logger.log(`info`, `data from routeeeeeeeeeeeeeeeee: ${JSON.stringify(data)}`)
|
||||||
const outputData = [headers, ...rows]; // Include headers
|
|
||||||
|
if (spreadsheetId && range && credentials) {
|
||||||
await writeDataToSheet(spreadsheetId, range, outputData);
|
// Convert data to Google Sheets format (headers and rows)
|
||||||
logger.log('info', `Data written to Google Sheet successfully for ${req.params.fileName}_${req.params.runId}`);
|
const headers = Object.keys(data[0]);
|
||||||
return res.send({ success: true, message: 'Data updated in Google Sheet' });
|
const rows = data.map((row: { [key: string]: any }) => Object.values(row));
|
||||||
}
|
const outputData = [headers, ...rows]; // Include headers
|
||||||
|
|
||||||
|
await writeDataToSheet(spreadsheetId, range, outputData);
|
||||||
|
logger.log('info', `Data written to Google Sheet successfully for ${req.params.fileName}_${req.params.runId}`);
|
||||||
|
return res.send({ success: true, message: 'Data updated in Google Sheet' });
|
||||||
}
|
}
|
||||||
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' });
|
return res.status(400).send({ success: false, message: 'Google Sheet integration not configured' });
|
||||||
} 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 });
|
|
||||||
}
|
}
|
||||||
});
|
return res.status(400).send({ success: false, message: 'Run not successful or no data to update' });
|
||||||
|
} 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) {
|
export async function updateGoogleSheet(fileName: string, runId: string) {
|
||||||
try {
|
try {
|
||||||
@@ -97,7 +86,7 @@ export async function updateGoogleSheet(fileName: string, runId: string) {
|
|||||||
if (parsedRun.status === 'success' && parsedRun.serializableOutput) {
|
if (parsedRun.status === 'success' && parsedRun.serializableOutput) {
|
||||||
const data = parsedRun.serializableOutput['item-0'] as { [key: string]: any }[];
|
const data = parsedRun.serializableOutput['item-0'] as { [key: string]: any }[];
|
||||||
const integrationConfig = await loadIntegrations();
|
const integrationConfig = await loadIntegrations();
|
||||||
|
|
||||||
if (integrationConfig) {
|
if (integrationConfig) {
|
||||||
const { spreadsheetId, range, credentials } = integrationConfig;
|
const { spreadsheetId, range, credentials } = integrationConfig;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user