From 0589328f247e68629d342b8b737f7373a99c302f Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Mon, 28 Jun 2021 10:03:54 -0400 Subject: [PATCH] when provisioning a new account on hosted system, automatically add hello-world and dial-time apps --- db/generate-beta-invite-codes.js | 40 ++++++++++++++++++++++++++++++++ lib/routes/api/register.js | 23 ++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 db/generate-beta-invite-codes.js diff --git a/db/generate-beta-invite-codes.js b/db/generate-beta-invite-codes.js new file mode 100644 index 0000000..38dc32f --- /dev/null +++ b/db/generate-beta-invite-codes.js @@ -0,0 +1,40 @@ +#!/usr/bin/env node +const crypto = require('crypto'); +const {promisePool} = require('../lib/db'); +const sql = 'INSERT INTO beta_invite_codes (invite_code) VALUES (?);'; + +const rand_string = (n) => { + if (n <= 0) { + return ''; + } + var rs = ''; + try { + rs = crypto.randomBytes(Math.ceil(n/2)).toString('hex').slice(0,n); + /* note: could do this non-blocking, but still might fail */ + } + catch (ex) { + /* known exception cause: depletion of entropy info for randomBytes */ + console.error('Exception generating random string: ' + ex); + /* weaker random fallback */ + rs = ''; + var r = n % 8, q = (n - r) / 8, i; + for (i = 0; i < q; i++) { + rs += Math.random().toString(16).slice(2); + } + if (r > 0) { + rs += Math.random().toString(16).slice(2, i); + } + } + return rs; +}; + +const doIt = async(len) => { + for (let i = 0; i < 50; i++) { + const val = rand_string(len).toUpperCase(); + await promisePool.execute(sql, [val]); + } + process.exit(0); +}; + +doIt(6); + diff --git a/lib/routes/api/register.js b/lib/routes/api/register.js index 3edf799..6523aa5 100644 --- a/lib/routes/api/register.js +++ b/lib/routes/api/register.js @@ -20,6 +20,13 @@ values (?, ?, ?, ?, ?, 0, 'local', ?)`; const insertAccountSql = `INSERT into accounts (account_sid, service_provider_sid, name, is_active, webhook_secret, trial_end_date) values (?, ?, ?, ?, ?, CURDATE() + INTERVAL 21 DAY)`; +const insertWebookSql = `INSERT INTO webhooks (webhook_sid, url, method) +VALUES (?, ?, ?)`; +const insertApplicationSql = `INSERT INTO applications +(application_sid, account_sid, name, call_hook_sid, call_status_hook_sid, +speech_synthesis_vendor, speech_synthesis_language, speech_synthesis_voice, +speech_recognizer_vendor, speech_recognizer_language) +VALUES (?,?,?,?,?,?,?,?,?,?)`; const queryRootDomainSql = `SELECT root_domain FROM service_providers WHERE service_providers.service_provider_sid = ?`; @@ -281,6 +288,22 @@ router.post('/', async(req, res) => { userProfile.provider_userid); } + /* add hello-world and dial-time as starter applications */ + const callStatusSid = uuid(); + const helloWordSid = uuid(); + const dialTimeSid = uuid(); + + /* 3 webhooks */ + await promisePool.execute(insertWebookSql, [callStatusSid, 'https://public-apps.jambonz.us/call-status', 'POST']); + await promisePool.execute(insertWebookSql, [helloWordSid, 'https://public-apps.jambonz.us/hello-world', 'POST']); + await promisePool.execute(insertWebookSql, [dialTimeSid, 'https://public-apps.jambonz.us/dial-time', 'POST']); + + /* 2 applications */ + await promisePool.execute(insertApplicationSql, [uuid(), userProfile.account_sid, 'hello world', + helloWordSid, callStatusSid, 'google', 'en-US', 'en-US-Wavenet-C', 'google', 'en-US']); + await promisePool.execute(insertApplicationSql, [uuid(), userProfile.account_sid, 'dial time clock', + dialTimeSid, callStatusSid, 'google', 'en-US', 'en-US-Wavenet-C', 'google', 'en-US']); + Object.assign(userProfile, { pristine: true, is_active: req.body.provider !== 'local',