File size: 1,996 Bytes
61652e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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}`);
});