| 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()); |
|
|
| |
| 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); |
|
|
| |
| app.get('/ping', (req, res) => { |
| res.status(200).json({ status: 'awake', message: 'RollyBeans Backend is live.' }); |
| }); |
|
|
| |
| app.post('/api/apply', async (req, res) => { |
| try { |
| const { name, role, phone, email, portfolio, whyJoin, agreed } = req.body; |
|
|
| |
| 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]; |
|
|
| |
| 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}`); |
| }); |