RezinaLMS-CustomerEnd/tools/waitlist-apps-script.gs
kirukib 629403da0b feat(waitlist): save signups to a Google Sheet via Apps Script
Server-side validation, email dedupe under a script lock, formula-injection
guard, honeypot field. The page posts text/plain so no CORS preflight is
needed. tools/waitlist-check.mjs runs the script against stub services.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 01:33:46 +03:00

59 lines
2.7 KiB
JavaScript

/**
* Waitlist receiver for apps/demo/public/waitlist/index.html → one row per signup in a Google Sheet.
*
* Setup (once, in the Google account that should own the list):
* 1. Create a Google Sheet, e.g. "MakeUp Academy waitlist".
* 2. Extensions → Apps Script. Replace Code.gs with this file and save.
* 3. Deploy → New deployment → type "Web app". Execute as: Me. Who has access: Anyone.
* 4. Authorize, then copy the Web app URL (ends in /exec).
* 5. Paste it into ENDPOINT in apps/demo/public/waitlist/index.html, then commit and deploy.
* After editing this script, use Deploy → Manage deployments → Edit → New version (the URL stays the same).
*/
const SHEET_NAME = 'Signups';
const HEADER = ['Signed up (UTC)', 'Name', 'Email', 'Phone', 'Language', 'Page'];
function doPost(e) {
let data;
try { data = JSON.parse((e && e.postData && e.postData.contents) || '{}'); } catch (_) { return reply({ ok: false, error: 'bad_json' }); }
if (data.company) return reply({ ok: true }); // honeypot filled → bot; pretend success
const row = clean(data);
if (row.error) return reply({ ok: false, error: row.error });
const lock = LockService.getScriptLock();
lock.waitLock(10000);
try {
const sheet = getSheet();
const last = sheet.getLastRow();
const emails = last > 1 ? sheet.getRange(2, 3, last - 1, 1).getValues().map((r) => String(r[0]).toLowerCase()) : [];
if (emails.indexOf(row.email) === -1) {
sheet.appendRow([new Date().toISOString(), safe(row.name), safe(row.email), "'" + row.phone, safe(row.locale), safe(row.page)]);
}
} finally { lock.releaseLock(); }
return reply({ ok: true });
}
/** Server-side check: the page validates too, but anyone can POST here. */
function clean(d) {
const name = String(d.name || '').trim().slice(0, 80);
const email = String(d.email || '').trim().toLowerCase().slice(0, 120);
const phone = String(d.phone || '').replace(/[\s()-]/g, '');
if (name.length < 2) return { error: 'name' };
if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) return { error: 'email' };
if (!/^\+\d{8,15}$/.test(phone)) return { error: 'phone' };
return { name, email, phone, locale: String(d.locale || '').slice(0, 20), page: String(d.page || '').slice(0, 200) };
}
/** Cells starting with = + - @ would run as formulas when someone opens the sheet. */
function safe(v) { return /^[=+\-@]/.test(v) ? "'" + v : v; }
function getSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(SHEET_NAME);
if (!sheet) { sheet = ss.insertSheet(SHEET_NAME); sheet.appendRow(HEADER); sheet.setFrozenRows(1); }
return sheet;
}
function reply(obj) {
return ContentService.createTextOutput(JSON.stringify(obj)).setMimeType(ContentService.MimeType.JSON);
}