2024-09-18 07:28:57 +05:30
|
|
|
import { Router } from 'express';
|
2024-09-17 20:39:42 +05:30
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2024-09-18 18:27:38 +05:30
|
|
|
import logger from "../logger";
|
2024-09-17 20:38:32 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
export const router = Router();
|
2024-09-17 20:38:32 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
router.post('/upload-credentials', async (req, res) => {
|
|
|
|
|
const { credentials, spreadsheetId, range } = req.body;
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
if (!credentials || !spreadsheetId || !range) {
|
|
|
|
|
return res.status(400).json({ message: 'Credentials, Spreadsheet ID, and Range are required.' });
|
2024-09-17 20:39:42 +05:30
|
|
|
}
|
2024-09-18 18:27:38 +05:30
|
|
|
|
2024-09-18 18:47:59 +05:30
|
|
|
// Store the credentials in a secure place (for test, we store them locally)
|
2024-09-17 20:58:30 +05:30
|
|
|
const storedCredentialsPath = path.join(__dirname, 'service_account_credentials.json');
|
2024-09-18 18:28:00 +05:30
|
|
|
|
2024-09-17 20:47:36 +05:30
|
|
|
try {
|
2024-09-18 18:27:38 +05:30
|
|
|
fs.writeFileSync(storedCredentialsPath, JSON.stringify(credentials));
|
|
|
|
|
logger.log('info', 'Service account credentials saved successfully.');
|
2024-09-18 18:44:04 +05:30
|
|
|
} catch (error: any) {
|
2024-09-18 18:27:38 +05:30
|
|
|
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
|
|
|
});
|