-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* google custom voice * wip * wip * fix failing testcase
- Loading branch information
Showing
9 changed files
with
509 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const Model = require('./model'); | ||
const {promisePool} = require('../db'); | ||
|
||
class GoogleCustomVoice extends Model { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
static async retrieveAllBySpeechCredentialSid(speech_credential_sid) { | ||
const sql = `SELECT * FROM ${this.table} WHERE speech_credential_sid = ?`; | ||
const [rows] = await promisePool.query(sql, speech_credential_sid); | ||
return rows; | ||
} | ||
|
||
static async deleteAllBySpeechCredentialSid(speech_credential_sid) { | ||
const sql = `DELETE FROM ${this.table} WHERE speech_credential_sid = ?`; | ||
const [rows] = await promisePool.query(sql, speech_credential_sid); | ||
return rows; | ||
} | ||
|
||
static async retrieveAllByLabel(service_provider_sid, account_sid, label) { | ||
let sql; | ||
if (account_sid) { | ||
sql = `SELECT gcv.* FROM ${this.table} gcv | ||
LEFT JOIN speech_credentials sc ON gcv.speech_credential_sid = sc.speech_credential_sid | ||
WHERE sc.account_sid = ? OR (sc.account_sid is NULL && sc.service_provider_sid = ?) | ||
${label ? 'AND label = ?' : 'AND label is NULL'}`; | ||
} else { | ||
sql = `SELECT gcv.* FROM ${this.table} gcv | ||
LEFT JOIN speech_credentials sc ON gcv.speech_credential_sid = sc.speech_credential_sid | ||
WHERE sc.service_provider_sid = ? ${label ? 'AND label = ?' : 'AND label is NULL'}`; | ||
} | ||
const [rows] = await promisePool.query(sql, [...(account_sid ? | ||
[account_sid, service_provider_sid] : [service_provider_sid]), label]); | ||
return rows; | ||
} | ||
} | ||
GoogleCustomVoice.table = 'google_custom_voices'; | ||
GoogleCustomVoice.fields = [ | ||
{ | ||
name: 'google_custom_voice_sid', | ||
type: 'string', | ||
primaryKey: true | ||
}, | ||
{ | ||
name: 'model', | ||
type: 'string', | ||
required: true | ||
}, | ||
{ | ||
name: 'reported_usage', | ||
type: 'number' | ||
}, | ||
{ | ||
name: 'name', | ||
type: 'string', | ||
required: true | ||
} | ||
]; | ||
|
||
module.exports = GoogleCustomVoice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const router = require('express').Router(); | ||
const GoogleCustomVoice = require('../../models/google-custom-voice'); | ||
const SpeechCredential = require('../../models/speech-credential'); | ||
const decorate = require('./decorate'); | ||
const {DbErrorBadRequest, DbErrorForbidden} = require('../../utils/errors'); | ||
const sysError = require('../error'); | ||
|
||
const validateCredentialPermission = async(req) => { | ||
const credential = await SpeechCredential.retrieve(req.body.speech_credential_sid); | ||
if (!credential || credential.length === 0) { | ||
throw new DbErrorBadRequest('Invalid speech_credential_sid'); | ||
} | ||
const cred = credential[0]; | ||
|
||
if (req.user.hasServiceProviderAuth && cred.service_provider_sid !== req.user.service_provider_sid) { | ||
throw new DbErrorForbidden('Insufficient privileges'); | ||
} | ||
if (req.user.hasAccountAuth && cred.account_sid !== req.user.account_sid) { | ||
throw new DbErrorForbidden('Insufficient privileges'); | ||
} | ||
}; | ||
|
||
const validateAdd = async(req) => { | ||
if (!req.body.speech_credential_sid) { | ||
throw new DbErrorBadRequest('missing speech_credential_sid'); | ||
} | ||
|
||
await validateCredentialPermission(req); | ||
}; | ||
|
||
const validateUpdate = async(req) => { | ||
if (req.body.speech_credential_sid) { | ||
await validateCredentialPermission(req); | ||
} | ||
}; | ||
|
||
const preconditions = { | ||
add: validateAdd, | ||
update: validateUpdate, | ||
}; | ||
|
||
decorate(router, GoogleCustomVoice, ['add', 'retrieve', 'update', 'delete'], preconditions); | ||
|
||
router.get('/', async(req, res) => { | ||
const logger = req.app.locals.logger; | ||
const account_sid = req.user.account_sid || req.query.account_sid; | ||
const service_provider_sid = req.user.service_provider_sid || req.query.service_provider_sid; | ||
const speech_credential_sid = req.query.speech_credential_sid; | ||
const label = req.query.label; | ||
try { | ||
let results = []; | ||
if (speech_credential_sid) { | ||
const [cred] = await SpeechCredential.retrieve(speech_credential_sid); | ||
if (!cred) { | ||
return res.sendStatus(404); | ||
} | ||
if (account_sid && cred.account_sid && cred.account_sid !== account_sid) { | ||
throw new DbErrorForbidden('Insufficient privileges'); | ||
} | ||
if (service_provider_sid && cred.service_provider_sid && cred.service_provider_sid !== service_provider_sid) { | ||
throw new DbErrorForbidden('Insufficient privileges'); | ||
} | ||
results = await GoogleCustomVoice.retrieveAllBySpeechCredentialSid(speech_credential_sid); | ||
} else { | ||
if (!account_sid && !service_provider_sid) { | ||
throw new DbErrorBadRequest('missing account_sid or service_provider_sid in query parameters'); | ||
} | ||
results = await GoogleCustomVoice.retrieveAllByLabel(service_provider_sid, account_sid, label); | ||
} | ||
res.status(200).json(results); | ||
} catch (err) { | ||
sysError(logger, res, err); | ||
} | ||
}); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.