diff --git a/lib/compliance_overrides.js b/lib/compliance_overrides.js new file mode 100644 index 00000000..3dce29a6 --- /dev/null +++ b/lib/compliance_overrides.js @@ -0,0 +1,22 @@ +const db = require('./db') +const uuid = require('uuid') + +function add (complianceOverride) { + const sql = `insert into compliance_overrides + (id, + customer_id, + compliance_type, + override_at, + override_by, + verification) + values ($1, $2, $3, now(), $4, $5) returning *` + return db.one(sql, [ + uuid.v4(), + complianceOverride.customerId, + complianceOverride.complianceType, + complianceOverride.overrideBy, + complianceOverride.verification + ]) +} + +module.exports = { add } diff --git a/lib/customers.js b/lib/customers.js index 343a8005..159b62b0 100644 --- a/lib/customers.js +++ b/lib/customers.js @@ -6,6 +6,7 @@ const anonymous = require('../lib/constants').anonymousCustomer const NUM_RESULTS = 20 const camelize = require('camelize') const Pgp = require('pg-promise')() +const complianceOverrides = require('./compliance_overrides') function add (customer) { const sql = 'insert into customers (id, phone, phone_at) values ($1, $2, now()) returning *' @@ -38,9 +39,10 @@ function get (phone) { function update (id, data, userToken) { const formattedData = _.omit(['id'], _.mapKeys(_.snakeCase, data)) const updateData = addOverrideUser(formattedData, userToken) + addComplianceOverrides(id, updateData, userToken) const sql = Pgp.helpers.update(updateData, _.keys(updateData), 'customers') + ' where id=$1 returning *' - return db.oneOrNone(sql, [id]) + return db.one(sql, [id]) .then(customer => customer ? format(customer) : null) } @@ -89,6 +91,58 @@ function addOverrideUser (customer, userToken) { return customer } +/** + * Save new compliance override records + * + * Take the override fields that are modified in customer and create + * a compliance override record in db for each compliance type. + * + * @name addComplianceOverrides + * @function + * + * @param {string} id Customer's id + * @param {object} customer Customer that is updating + * @param {string} userToken Acting user's token + * + * @returns {promise} Result from compliance_overrides creation + */ +function addComplianceOverrides (id, customer, userToken) { + // Compliance override field mappings + const overrideFields = [{ + name: 'sms_override', + complianceType: 'sms' + }, { + name: 'id_card_data_override', + complianceType: 'id_card_data' + }, { + name: 'id_card_photo_override', + complianceType: 'id_card_photo' + }, { + name: 'front_facing_cam_override', + complianceType: 'front_camera' + }, { + name: 'sanctions_check_override', + complianceType: 'sanctions' + }, { + name: 'authorized_override', + complianceType: 'authorized' + }] + + // Prepare compliance overrides to save + const overrides = _.map(field => { + return (customer[field.name]) ? { + customerId: id, + complianceType: field.complianceType, + overrideBy: userToken, + verification: customer[field.name] + } : null + }, overrideFields) + + // Save all the updated compliance override fields + return Promise.all(_.compact(overrides) + .map(override => complianceOverrides.add(override))) +} + /** * Format and populate fields * for customer record