feat: save T&C hash to the user_config
This commit is contained in:
parent
6c43f7536d
commit
7b951f961f
2 changed files with 28 additions and 2 deletions
|
|
@ -1,8 +1,11 @@
|
|||
const crypto = require('crypto')
|
||||
|
||||
const _ = require('lodash/fp')
|
||||
const db = require('./db')
|
||||
const migration = require('./config-migration')
|
||||
const { asyncLocalStorage } = require('./async-storage')
|
||||
const { getOperatorId } = require('./operator')
|
||||
const { getTermsConditions, setTermsConditions } = require('./new-config-manager')
|
||||
|
||||
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
|
||||
const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2
|
||||
|
|
@ -23,6 +26,30 @@ const SECRET_FIELDS = [
|
|||
'twilio.authToken'
|
||||
]
|
||||
|
||||
/*
|
||||
* JSON.stringify isn't necessarily deterministic so this function may compute
|
||||
* different hashes for the same object.
|
||||
*/
|
||||
const md5hash = obj =>
|
||||
crypto
|
||||
.createHash('MD5')
|
||||
.update(JSON.stringify(obj))
|
||||
.digest('hex')
|
||||
|
||||
const addTermsHash = configs => {
|
||||
configs = _.omit('termsConditions_hash', configs)
|
||||
const terms = getTermsConditions(configs)
|
||||
return _.isEmpty(terms) ?
|
||||
configs :
|
||||
_.flow(
|
||||
_.omit(['hash']),
|
||||
md5hash,
|
||||
hash => _.set('hash', hash, terms),
|
||||
setTermsConditions,
|
||||
_.assign(configs),
|
||||
)(terms)
|
||||
}
|
||||
|
||||
const accountsSql = `update user_config set data = $2, valid = $3, schema_version = $4 where type = $1;
|
||||
insert into user_config (type, data, valid, schema_version)
|
||||
select $1, $2, $3, $4 where $1 not in (select type from user_config)`
|
||||
|
|
@ -74,7 +101,7 @@ const configSql = 'insert into user_config (type, data, valid, schema_version) v
|
|||
function saveConfig (config) {
|
||||
return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')])
|
||||
.then(([currentConfig, operatorId]) => {
|
||||
const newConfig = _.assign(currentConfig, config)
|
||||
const newConfig = addTermsHash(_.assign(currentConfig, config))
|
||||
return db.tx(t => {
|
||||
return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
.then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue