// Gishen waitlist backend: a Google Sheet + this Apps Script. // Setup: create a Sheet → Extensions → Apps Script → paste this file → Deploy → New deployment → // Web app, Execute as "Me", Access "Anyone" → copy the /exec URL into ENDPOINT in index.html. // NOTIFY: every signup is also emailed here. Set it to the inbox Kerod sets up (see TASKS.md). const NOTIFY = 'info@gishenpharmacy.org'; const HEADERS = ['createdAt', 'name', 'phone', 'email', 'channel', 'locale', 'page']; function sheet_() { const sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; if (sh.getLastRow() === 0) sh.appendRow(HEADERS); return sh; } const json_ = (o) => ContentService.createTextOutput(JSON.stringify(o)).setMimeType(ContentService.MimeType.JSON); // GET → { count } for the "N people are already waiting" pill. function doGet() { return json_({ count: Math.max(sheet_().getLastRow() - 1, 0) }); } const SPECIAL_HEADERS = ['createdAt', 'item', 'qty', 'name', 'phone', 'notes', 'page', 'status']; // Special order requests live on their own tab, so the waitlist count (first tab) stays right. function special_(d) { const item = String(d.item || '').trim().slice(0, 200); const name = String(d.name || '').trim().slice(0, 120); const phone = /^\+\d{8,15}$/.test(d.phone || '') ? d.phone : ''; if (item.length < 2 || name.length < 2 || !phone) return json_({ ok: false, error: 'invalid' }); const ss = SpreadsheetApp.getActiveSpreadsheet(); const sh = ss.getSheetByName('Special orders') || ss.insertSheet('Special orders'); if (sh.getLastRow() === 0) sh.appendRow(SPECIAL_HEADERS); const qty = String(d.qty || '').slice(0, 100), notes = String(d.notes || '').slice(0, 500); sh.appendRow([new Date(), item, qty, name, "'" + phone, notes, String(d.page || '').slice(0, 200), 'new']); if (NOTIFY) MailApp.sendEmail(NOTIFY, 'Special order request: ' + item, [item, qty, name, phone, notes].filter(String).join('\n')); return json_({ ok: true }); } function doPost(e) { try { const d = JSON.parse(e.postData.contents || '{}'); if (d.company) return json_({ ok: true }); // honeypot: bots fill the hidden field if (d.type === 'special') return special_(d); const name = String(d.name || '').trim().slice(0, 120); const phone = /^\+\d{8,15}$/.test(d.phone || '') ? d.phone : ''; const email = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(d.email || '') ? String(d.email).toLowerCase().slice(0, 200) : ''; if (name.length < 2 || (!phone && !email)) return json_({ ok: false, error: 'invalid' }); const lock = LockService.getScriptLock(); lock.waitLock(10000); try { const sh = sheet_(); const rows = sh.getLastRow() > 1 ? sh.getRange(2, 3, sh.getLastRow() - 1, 2).getValues() : []; // Prefix with ' so Sheets keeps +251… as text, not a number. const dup = rows.some(([p, m]) => (phone && String(p).replace(/^'/, '') === phone) || (email && m === email)); if (!dup) { sh.appendRow([new Date(), name, phone ? "'" + phone : '', email, d.channel === 'email' ? 'email' : 'sms', String(d.locale || '').slice(0, 20), String(d.page || '').slice(0, 200)]); if (NOTIFY) MailApp.sendEmail(NOTIFY, 'New Gishen waitlist signup: ' + name, [name, phone, email].filter(String).join('\n')); } } finally { lock.releaseLock(); } return json_({ ok: true }); } catch (err) { return json_({ ok: false, error: 'server' }); } }