// 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 B2B_HEADERS = ['createdAt', 'institution', 'type', 'contact', 'phone', 'email', 'orders', 'page', 'status']; const B2B_TYPES = ['Hospital', 'Clinic', 'Company', 'NGO', 'Pharmacy', 'Other']; // Institution sign-ups live on their own tab, so the waitlist count (first tab) stays right. function b2b_(d) { const org = String(d.institution || '').trim().slice(0, 160); 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 (org.length < 2 || name.length < 2 || !phone) return json_({ ok: false, error: 'invalid' }); const kind = B2B_TYPES.includes(d.kind) ? d.kind : 'Other'; const orders = String(d.orders || '').slice(0, 500); const ss = SpreadsheetApp.getActiveSpreadsheet(); const sh = ss.getSheetByName('Institutions') || ss.insertSheet('Institutions'); if (sh.getLastRow() === 0) sh.appendRow(B2B_HEADERS); sh.appendRow([new Date(), org, kind, name, "'" + phone, email, orders, String(d.page || '').slice(0, 200), 'new']); if (NOTIFY) MailApp.sendEmail(NOTIFY, 'New institution sign-up: ' + org, [org + ' (' + kind + ')', name, phone, email, orders].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 === 'b2b') return b2b_(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' }); } }