require('dotenv').config(); const express = require('express'); const cors = require('cors'); const { GoogleSpreadsheet } = require('google-spreadsheet'); const { JWT } = require('google-auth-library'); const app = express(); app.use(cors()); app.use(express.json()); // Initialize Google Sheets Auth const serviceAccountAuth = new JWT({ email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'), scopes: ['https://www.googleapis.com/auth/spreadsheets'], }); const doc = new GoogleSpreadsheet(process.env.GOOGLE_SHEET_ID, serviceAccountAuth); // ROUTE 1: The Keep-Alive Ping (For cron-job.org) app.get('/ping', (req, res) => { res.status(200).json({ status: 'awake', message: 'RollyBeans Backend is live.' }); }); // ROUTE 2: Handle Application Submissions app.post('/api/apply', async (req, res) => { try { const { name, role, phone, email, portfolio, whyJoin, agreed } = req.body; // Basic Validation if (!name || !role || !phone || !email || !agreed) { return res.status(400).json({ error: 'Missing required fields or terms not accepted.' }); } await doc.loadInfo(); const sheet = doc.sheetsByIndex[0]; // Gets the first tab in your sheet // Append the new row await sheet.addRow({ 'Timestamp': new Date().toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' }), 'Name': name, 'Role': role, 'Phone': phone, 'Email': email, 'Portfolio Link': portfolio || 'N/A', 'Why RollyBeans?': whyJoin, 'Agreed to Terms': agreed ? 'YES' : 'NO' }); res.status(200).json({ success: true, message: 'Application submitted successfully.' }); } catch (error) { console.error('Error saving application:', error); res.status(500).json({ success: false, error: 'Failed to submit application. Try again.' }); } }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`RollyBeans Engine running on port ${PORT}`); });