chore: lint

This commit is contained in:
karishmas6
2024-09-17 20:58:30 +05:30
parent 8417c2f3e8
commit 81456ee8ba

View File

@@ -5,54 +5,52 @@ import path from 'path';
export const router = Router() export const router = Router()
router.post('/upload-credentials', (req, res) => { router.post('/upload-credentials', (req, res) => {
const credentials = req.body.credentials; const credentials = req.body.credentials;
if (!credentials) { if (!credentials) {
return res.status(400).json({ message: 'Credentials are required.' }); return res.status(400).json({ message: 'Credentials are required.' });
} }
// Todo: Store the credentials in a secure place (for test, we store them locally) // Todo: Store the credentials in a secure place (for test, we store them locally)
const storedCredentialsPath = path.join(__dirname, 'service_account_credentials.json'); const storedCredentialsPath = path.join(__dirname, 'service_account_credentials.json');
fs.writeFileSync(storedCredentialsPath, JSON.stringify(credentials)); fs.writeFileSync(storedCredentialsPath, JSON.stringify(credentials));
res.status(200).json({ message: 'Service Account credentials saved successfully.' }); res.status(200).json({ message: 'Service Account credentials saved successfully.' });
}); });
router.post('/write-to-sheet', async (req, res) => { router.post('/write-to-sheet', async (req, res) => {
try { try {
const { spreadsheetId, range, values } = req.body; const { spreadsheetId, range, values } = req.body;
// Load the stored credentials // Load the stored credentials
const credentialsPath = path.join(__dirname, 'service_account_credentials.json'); const credentialsPath = path.join(__dirname, 'service_account_credentials.json');
if (!fs.existsSync(credentialsPath)) { if (!fs.existsSync(credentialsPath)) {
return res.status(400).json({ message: 'No credentials found. Please provide credentials first.' }); return res.status(400).json({ message: 'No credentials found. Please provide credentials first.' });
} }
const credentials = JSON.parse(fs.readFileSync(credentialsPath, 'utf-8')); const credentials = JSON.parse(fs.readFileSync(credentialsPath, 'utf-8'));
// Authenticate with Google using the service account credentials // Authenticate with Google using the service account credentials
const auth = new google.auth.GoogleAuth({ const auth = new google.auth.GoogleAuth({
credentials, credentials,
scopes: ['https://www.googleapis.com/auth/spreadsheets'], scopes: ['https://www.googleapis.com/auth/spreadsheets'],
}); });
const sheets = google.sheets({ version: 'v4', auth }); const sheets = google.sheets({ version: 'v4', auth });
// Write data to the provided Google Sheet and range // Write data to the provided Google Sheet and range
await sheets.spreadsheets.values.append({ await sheets.spreadsheets.values.append({
spreadsheetId, spreadsheetId,
range, range,
valueInputOption: 'USER_ENTERED', valueInputOption: 'USER_ENTERED',
requestBody: { requestBody: {
values: values, // Expecting an array of arrays, like [['data1', 'data2'], ['data3', 'data4']] values: values, // Expecting an array of arrays, like [['data1', 'data2'], ['data3', 'data4']]
}, },
}); });
res.status(200).json({ message: 'Data written to Google Sheet successfully.' }); res.status(200).json({ message: 'Data written to Google Sheet successfully.' });
} catch (error) { } catch (error) {
console.error('Error writing to sheet:', error); console.error('Error writing to sheet:', error);
res.status(500).json({ message: 'Failed to write to Google Sheet.', error }); res.status(500).json({ message: 'Failed to write to Google Sheet.', error });
} }
}); });