-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-waitlist.js
More file actions
150 lines (132 loc) Β· 5.45 KB
/
Copy pathtest-waitlist.js
File metadata and controls
150 lines (132 loc) Β· 5.45 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Test script for waitlist API
// Run with: node test-waitlist.js
// Make sure .env file exists with your credentials
require('dotenv').config();
const { google } = require('googleapis');
async function testWaitlist() {
console.log('π§ͺ Testing Google Sheets Waitlist Integration...\n');
// Check environment variables
const credentials = process.env.GOOGLE_SERVICE_ACCOUNT;
const spreadsheetId = process.env.GOOGLE_SHEET_ID;
const sheetName = process.env.GOOGLE_SHEET_NAME || 'Waitlist';
console.log('π Configuration Check:');
console.log(' β GOOGLE_SERVICE_ACCOUNT:', credentials ? `Set (${credentials.length} chars)` : 'β MISSING');
console.log(' β GOOGLE_SHEET_ID:', spreadsheetId || 'β MISSING');
console.log(' β GOOGLE_SHEET_NAME:', sheetName);
console.log('');
if (!credentials || !spreadsheetId) {
console.error('β Missing required environment variables!');
console.error(' Create a .env file with GOOGLE_SERVICE_ACCOUNT and GOOGLE_SHEET_ID');
process.exit(1);
}
try {
// Parse credentials
console.log('π Parsing credentials...');
const credentialsTrimmed = credentials.trim();
let serviceAccount;
try {
serviceAccount = JSON.parse(credentialsTrimmed);
console.log(' β Credentials parsed successfully');
console.log(' β Service account email:', serviceAccount.client_email);
} catch (parseError) {
console.error(' β Failed to parse credentials:', parseError.message);
process.exit(1);
}
// Authenticate
console.log('\nπ Authenticating with Google...');
const auth = new google.auth.GoogleAuth({
credentials: serviceAccount,
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
console.log(' β Authentication successful');
// Get sheets client
const sheets = google.sheets({ version: 'v4', auth });
// Test 1: Get spreadsheet metadata
console.log('\nπ Test 1: Checking spreadsheet access...');
try {
const spreadsheet = await sheets.spreadsheets.get({
spreadsheetId,
});
console.log(' β Spreadsheet found:', spreadsheet.data.properties.title);
console.log(' β Spreadsheet ID:', spreadsheetId);
} catch (error) {
console.error(' β Cannot access spreadsheet:', error.message);
if (error.code === 403) {
console.error(' β Permission denied. Make sure the service account email has Editor access to the sheet.');
console.error(' β Service account email:', serviceAccount.client_email);
} else if (error.code === 404) {
console.error(' β Spreadsheet not found. Check the GOOGLE_SHEET_ID.');
}
process.exit(1);
}
// Test 2: Check if sheet tab exists
console.log('\nπ Test 2: Checking sheet tab...');
try {
const spreadsheet = await sheets.spreadsheets.get({
spreadsheetId,
});
const sheetTabs = spreadsheet.data.sheets.map(s => s.properties.title);
console.log(' β Available sheet tabs:', sheetTabs.join(', '));
if (!sheetTabs.includes(sheetName)) {
console.error(` β Sheet tab "${sheetName}" not found!`);
console.error(' β Available tabs:', sheetTabs.join(', '));
console.error(' β Update GOOGLE_SHEET_NAME or create a tab named "' + sheetName + '"');
process.exit(1);
}
console.log(` β Sheet tab "${sheetName}" exists`);
} catch (error) {
console.error(' β Error checking sheet tabs:', error.message);
process.exit(1);
}
// Test 3: Try to read from the sheet
console.log('\nπ Test 3: Reading from sheet...');
try {
const response = await sheets.spreadsheets.values.get({
spreadsheetId,
range: `${sheetName}!A1:C10`,
});
console.log(' β Can read from sheet');
if (response.data.values) {
console.log(' β Current rows:', response.data.values.length);
}
} catch (error) {
console.error(' β Cannot read from sheet:', error.message);
process.exit(1);
}
// Test 4: Try to append a test row
console.log('\nβοΈ Test 4: Testing write access...');
try {
const testTimestamp = new Date().toISOString();
const testEmail = 'test@example.com';
const testName = 'Test User';
const values = [[testTimestamp, testEmail, testName]];
await sheets.spreadsheets.values.append({
spreadsheetId,
range: `${sheetName}!A:C`,
valueInputOption: 'USER_ENTERED',
insertDataOption: 'INSERT_ROWS',
resource: {
values,
},
});
console.log(' β Successfully wrote test row to sheet');
console.log(' β Check your Google Sheet - you should see a test entry');
} catch (error) {
console.error(' β Cannot write to sheet:', error.message);
if (error.response?.status === 403) {
console.error(' β Permission denied. Service account needs Editor access.');
}
process.exit(1);
}
console.log('\nβ
All tests passed! Your configuration is correct.');
console.log('\nπ‘ Next steps:');
console.log(' 1. Make sure these same values are set in Vercel environment variables');
console.log(' 2. Deploy to Vercel');
console.log(' 3. Test the form on your live site');
} catch (error) {
console.error('\nβ Unexpected error:', error.message);
console.error(' Stack:', error.stack);
process.exit(1);
}
}
testWaitlist();