Skip to content

Commit

Permalink
Merge branch 'master' into imc_profile_api
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramshankar committed Aug 2, 2017
2 parents 6a74cb2 + 22f95c6 commit 57a900f
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 115 deletions.
12 changes: 3 additions & 9 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8052,10 +8052,8 @@ paths:
type: boolean
description: If the roomType is active
settings:
type: array
type: object
description: Default settings for roomType
items:
type: object
rules:
type: array
description: Default rules for roomType
Expand Down Expand Up @@ -8184,10 +8182,8 @@ paths:
type: boolean
description: If the roomType is active
settings:
type: array
type: object
description: Default settings for roomType
items:
type: object
rules:
type: array
description: Default rules for roomType
Expand Down Expand Up @@ -10570,9 +10566,7 @@ definitions:
isEnabled:
type: boolean
settings:
type: array
items:
type: object
type: object
rules:
type: array
items:
Expand Down
26 changes: 26 additions & 0 deletions db/helpers/botUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const parameterArraySchema = Joi.alternatives().try(
)
);

const settingsArraySchema = Joi.array().items(
Joi.object().keys({
key: Joi.string().regex(/^[0-9a-z_-]+$/i).max(254).required(),
helpText: Joi.string().regex(/^\w+(\s\w+)*$/i).required(),
})
);

const actionArraySchema = Joi.array().items(
Joi.object().keys({
name: Joi.string().regex(/^[0-9a-z_-]+$/i).required(),
Expand Down Expand Up @@ -98,8 +105,27 @@ function validateDataArray(arr) {
}
} // validateDataArray

/**
* Custom validation rule that checks the settings array to have
* all valid entries. Meaning each element contains key and a value.
*
* @param {Array} arr - The array to test
* @returns {undefined} - OK
* @throws {validationError} - Invalid settings array
*/
function validateSettingsArray(arr) {
const result = Joi.validate(arr, settingsArraySchema);

if (result.error !== null) {
throw new ValidationError({
message: result.error.details,
});
}
} // validateSettings

module.exports = {
arrayHasValidParameters,
validateActionArray,
validateDataArray,
validateSettingsArray,
}; // exports
8 changes: 8 additions & 0 deletions db/model/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ module.exports = function bot(seq, dataTypes) {
},
comment: 'List of actions a Bot can take',
},
settings: {
type: dataTypes.ARRAY(dataTypes.JSONB),
allowNull: true,
validate: {
contains: u.validateSettingsArray,
},
comment: 'Array[ {key: name of key, helpText: describe the value it is looking for},]',
},
data: {
type: dataTypes.ARRAY(dataTypes.JSON),
allowNull: true,
Expand Down
22 changes: 20 additions & 2 deletions db/model/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

const constants = require('../constants');
const u = require('../helpers/roomTypeUtils');

const assoc = {};

Expand All @@ -33,6 +34,11 @@ module.exports = function room(seq, dataTypes) {
},
comment: 'Create a named room ',
},
settings: {
type: dataTypes.JSON,
allowNull: true,
comment: 'Key/Value pairs for user specific settings',
},
active: {
type: dataTypes.BOOLEAN,
defaultValue: false,
Expand All @@ -46,8 +52,11 @@ module.exports = function room(seq, dataTypes) {

postImport(models) {
assoc.type = Room.belongsTo(models.RoomType, {
foreignKey: 'type',
allowNull: false,
foreignKey: {
name: 'type',
allowNull: false,
},
onDelete: 'CASCADE',
});
assoc.writers = Room.belongsToMany(models.User, {
as: 'writers',
Expand All @@ -56,6 +65,15 @@ module.exports = function room(seq, dataTypes) {
});
},
},
hooks: {
afterCreate: (instance) => {
const RoomType = seq.models.RoomType;
return RoomType.findById(instance.type)
.then((roomType) => {
instance.settings = roomType.settings;
});
},
},
});
return Room;
};
Expand Down
5 changes: 1 addition & 4 deletions db/model/roomType.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ module.exports = function roomType(seq, dataTypes) {
comment: 'Determines if room type is still enabled for use',
},
settings: {
type: dataTypes.ARRAY(dataTypes.JSONB),
type: dataTypes.JSON,
allowNull: true,
validate: {
contains: u.validateSettingsArray,
},
comment: 'Key/Value pairs for user specific settings',
},
rules: {
Expand Down
43 changes: 43 additions & 0 deletions migrations/20170731100411-settings-array-to-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
'use strict';
const u = require('../db/helpers/roomTypeUtils');

module.exports = {
up: function (qi, Sequelize) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return qi.changeColumn('RoomType', 'settings', {
type: Sequelize.JSON,
allowNull: true,
});
},

down: function (qi, Sequelize) {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/

return qi.changeColumn('RoomType', 'settings', {
type: Sequelize.ARRAY(Sequelize.JSONB),
allowNull: true,
validate: {
contains: u.validateSettingsArray,
},
});
},
};
28 changes: 8 additions & 20 deletions tests/api/v1/roomTypes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ const n2 = n+'NonActive';
const standard = {
name: n,
isEnabled: true,
settings: [
{
key: 'Key1',
value: 'Value1',
},
{
key: 'Key2',
value: 'Value2',
},
],
settings: {
Key1: 'Value1',
Key2: 'Value2',
},
rules: [
{
rule: {
Expand Down Expand Up @@ -92,16 +86,10 @@ const standard = {
const nonActive = {
name: n2,
isEnabled: false,
settings: [
{
key: 'Key1',
value: 'Value1',
},
{
key: 'Key2',
value: 'Value2',
},
],
settings: {
Key1: 'Value1',
Key2: 'Value2',
},
rules: [
{
rule: {
Expand Down
1 change: 1 addition & 0 deletions tests/api/v1/rooms/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const RoomType = tu.db.RoomType;
const v = require('../roomTypes/utils');

describe(`api: DELETE ${path}`, () => {
let testRoomType;
let testRoom;
let token;

Expand Down
1 change: 1 addition & 0 deletions tests/api/v1/rooms/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const RoomType = tu.db.RoomType;
const v = require('../roomTypes/utils');

describe(`api: GET ${path}`, () => {
let testRoomType;
let testRoom;
let token;

Expand Down
1 change: 1 addition & 0 deletions tests/api/v1/rooms/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const RoomType = tu.db.RoomType;
const v = require('../roomTypes/utils');

describe(`api: PATCH ${path}`, () => {
let testRoomType;
let testRoom;
let token;

Expand Down
2 changes: 0 additions & 2 deletions tests/api/v1/rooms/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ const roomTypeSchema = {
],
};

const roomType = tu.db.RoomType.create(roomTypeSchema);

const standard = {
name: n,
active: true,
Expand Down
2 changes: 2 additions & 0 deletions tests/db/model/bot/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('db: bot: create: ', () => {
expect(o.actions[0].parameters.length).to.equal(4);
expect(o).to.have.property('data');
expect(o.data.length).to.equal(5);
expect(o).to.have.property('settings');
expect(o.settings.length).to.equal(1);
done();
})
.catch(done);
Expand Down
6 changes: 6 additions & 0 deletions tests/db/model/bot/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const standard = {
url: 'http://www.bar.com',
ui: uiBlob,
active: true,
settings: [
{
key: 'key1',
helpText: 'help Text 1'
}
],
actions: [
{
name: 'Action1',
Expand Down
31 changes: 31 additions & 0 deletions tests/db/model/room/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ describe('db: room: create: ', () => {
.catch(done);
});

it('ok, room created settings default', (done) => {
RoomType.create(v.getStandard())
.then((roomType) => {
const room = u.getStandard();
room.type = roomType.id;
return Room.create(room);
})
.then((o) => {
expect(o).to.have.property('settings');
expect(o.settings.Key1).to.equal('Value1');
done();
})
.catch(done);
});

it('ok, room created active false', (done) => {
RoomType.create(v.getStandard())
.then((roomType) => {
Expand Down Expand Up @@ -70,5 +85,21 @@ describe('db: room: create: ', () => {
})
.catch(done);
});

it('fail, room type null', (done) => {
RoomType.create(v.getStandard())
.then((roomType) => {
const room = u.getStandard();
room.type = null;
return Room.create(room);
})
.then(() => done(tu.valError))
.catch((err) => {
expect(err.message.toLowerCase()).to.contain('notnull violation');
done();
})
.catch(done);
});

});
});

0 comments on commit 57a900f

Please sign in to comment.