Skip to content

Commit

Permalink
Adds is_active column on ribot model (#14)
Browse files Browse the repository at this point in the history
* amended water volume

* adds instance methods to deactivate and reactivate a ribot

* responds with 404 when retrieving active ribot
  • Loading branch information
Stefan Pearson committed May 20, 2016
1 parent e83ce18 commit e837dea
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ test/reports
# Development environment
.env
cookbooks

# App
data/jobs/*
4 changes: 3 additions & 1 deletion app/controllers/ribot.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ var requestGetRibotCollection = function requestGetRibotCollection( request, res
options.withRelated = checkInRelations;
}

return Ribot.collection().fetch( options )
return Ribot.collection()
.query( { where: { is_active: true } } )
.fetch( options )
.then( function( ribots ) {
var payload = ribots.map( function( ribot ) {
return createRibotPayload( ribot );
Expand Down
3 changes: 1 addition & 2 deletions app/models/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ var AccessToken = BaseModel.extend( {
'id',
'ribotId',
'lastUsedDate',
'token',
'_sys'
'token'
] ),

virtuals: _.extend( {}, BaseModel.prototype.virtuals, {
Expand Down
11 changes: 1 addition & 10 deletions app/models/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,7 @@ var BaseModel = db.bookshelf.Model.extend( {

hidden: [ 'createdDate', 'updatedDate' ],

virtuals: {

_sys: function _sys() {
return {
createdDate: utils.formatDateTime( this.get( 'createdDate' ) ),
updatedDate: utils.formatDateTime( this.get( 'updatedDate' ) )
};
}

},
virtuals: {},

/**
* Constructor override
Expand Down
3 changes: 1 addition & 2 deletions app/models/beacon-encounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var BeaconEncounter = BaseModel.extend( {

hidden: _.union( BaseModel.prototype.hidden, [
'beaconId',
'checkInId',
'_sys'
'checkInId'
] ),

virtuals: _.extend( {}, BaseModel.prototype.virtuals, {
Expand Down
3 changes: 1 addition & 2 deletions app/models/beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var Beacon = BaseModel.extend( {
tableName: 'beacon',

hidden: _.union( BaseModel.prototype.hidden, [
'zoneId',
'_sys'
'zoneId'
] ),

zone: function zone() {
Expand Down
3 changes: 1 addition & 2 deletions app/models/check-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ var CheckIn = BaseModel.extend( {

hidden: _.union( BaseModel.prototype.hidden, [
'ribotId',
'venueId',
'_sys'
'venueId'
] ),

virtuals: _.extend( {}, BaseModel.prototype.virtuals, {
Expand Down
3 changes: 1 addition & 2 deletions app/models/drink.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var Drink = BaseModel.extend( {

hidden: _.union( BaseModel.prototype.hidden, [
'ribotId',
'ribot',
'_sys'
'ribot'
] ),

ribot: function ribot() {
Expand Down
3 changes: 1 addition & 2 deletions app/models/nfc-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var NfcTag = BaseModel.extend( {

hidden: _.union( BaseModel.prototype.hidden, [
'ribotId',
'ribot',
'_sys'
'ribot'
] ),

ribot: function ribot() {
Expand Down
43 changes: 30 additions & 13 deletions app/models/ribot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ var Ribot = BaseModel.extend( {
'providerCredentials',
'accessTokens',
'checkIns',
'latestCheckIn'
'latestCheckIn',
'isAuthenticated'
] ),

defaults: {
isAuthenticated: false
isAuthenticated: false,
isActive: true
},

virtuals: _.extend( {}, BaseModel.prototype.virtuals, {
Expand Down Expand Up @@ -110,11 +112,6 @@ var Ribot = BaseModel.extend( {

update: function personUpdate( attributes, options ) {

// Remove isAuthenticated (read-only)
if ( attributes.hasOwnProperty( 'isAuthenticated' ) ) {
delete attributes.isAuthenticated;
}

// If person is authenticated
if ( this.get( 'isAuthenticated' ) === true ) {

Expand All @@ -132,11 +129,30 @@ var Ribot = BaseModel.extend( {

this.set( attributes );

return this
.save( null, {
method: 'update',
transacting: options.transacting
} );
return this.save( null, {
method: 'update',
transacting: options.transacting
} );
},

deactivate: function( options ) {

this.set( { active: false } );

return this.save( null, {
method: 'update',
transacting: options.transacting
} );
},

reactivate: function( options ) {

this.set( { active: true } );

return this.save( null, {
method: 'update',
transacting: options.transacting
} );
},

createOrUpdateProviderCredential: function createOrUpdateProviderCredential( query, attributes, options ) {
Expand Down Expand Up @@ -248,7 +264,8 @@ Ribot.findByEmail = function findByEmail( email, options ) {
*/
Ribot.findById = function findById( id, options ) {
return new Ribot( {
id: id
id: id,
isActive: true
} )
.fetch( options )
.then( function( ribot ) {
Expand Down
4 changes: 0 additions & 4 deletions app/models/venue.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ var Venue = BaseModel.extend( {

tableName: 'venue',

hidden: _.union( BaseModel.prototype.hidden, [
'_sys'
] ),

checkIns: function checkIns() {
return this.hasMany( 'CheckIn' );
},
Expand Down
3 changes: 1 addition & 2 deletions app/models/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var Zone = BaseModel.extend( {
tableName: 'zone',

hidden: _.union( BaseModel.prototype.hidden, [
'venueId',
'_sys'
'venueId'
] ),

beacons: function beacons() {
Expand Down
6 changes: 3 additions & 3 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var sortTables = function sortTables( schema ) {

} );

return depGraph.overallOrder();
return depGraph.overallOrder().reverse();
};

/**
Expand Down Expand Up @@ -272,7 +272,7 @@ Db.prototype = {
createTables: function createTables( schema, seed ) {
this.foreignKeyStatements = [];

var sortedTableNames = sortTables( schema.schema ).reverse();
var sortedTableNames = sortTables( schema.schema );

return this.knex.transaction( function( trx ) {
return Promise.all( [
Expand Down Expand Up @@ -365,7 +365,7 @@ Db.prototype = {
* Drop all tables
*/
dropTables: function dropTables( schema ) {
var sortedTableNames = sortTables( schema ).reverse();
var sortedTableNames = sortTables( schema );
return Promise.each( sortedTableNames, this.knex.schema.dropTableIfExists );
},

Expand Down
2 changes: 1 addition & 1 deletion data/migrations/6-add-ibeacon-columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var alterTable = function alterTable( trx ) {


/**
* Adds new default values to all the beacon entires in the database
* Adds new default values to all the beacon entities in the database
*/
var alterData = function alterData( trx ) {
return Beacon.fetchAll( { transacting: trx } )
Expand Down
49 changes: 49 additions & 0 deletions data/migrations/9-add-user-active-column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// External ependancies
var Promise = require( 'bluebird' ),
_ = require( 'lodash' );


// Dependencies
var db = require( '../index' ),
Ribot = require( '../../app/models/ribot' );


/**
* Alters the table by adding the required column
*/
var createColumn = function createColumn( trx ) {
return trx.schema.table( 'ribot', function( table ) {
table.boolean( 'is_active' );
} );
};


/**
* Adds new default values to all the ribot entities in the database
*/
var alterData = function alterData( trx ) {
return Ribot.fetchAll( { transacting: trx } )
.then( function( ribots ) {
return ribots.map( function( ribot ) {
return ribot.save( { isActive: true }, { patch: true, transacting: trx } );
} );
} );
};


/**
* Alters the table by adding the required column
*/
var alterColumn = function alterColumn( trx ) {
return trx.schema.raw( "ALTER TABLE ribot ALTER COLUMN is_active SET NOT NULL" );
};


// Migrate function
var migrate = function migrate( trx ) {
return Promise.all( [ createColumn( trx ), alterData( trx ), alterColumn( trx ) ] );
};


// Exports
module.exports = migrate;
3 changes: 2 additions & 1 deletion data/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
5: require( './5-add-beacon-checkin-tables' ),
6: require( './6-add-ibeacon-columns' ),
7: require( './7-add-drink-table' ),
8: require( './8-add-nfc-tag-table' )
8: require( './8-add-nfc-tag-table' ),
9: require( './9-add-user-active-column' )
};
3 changes: 2 additions & 1 deletion data/schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var version = 8;
var version = 9;

var schema = {

Expand All @@ -22,6 +22,7 @@ var schema = {
hex_color: { type: 'text' },
bio: { type: 'text' },
is_authenticated: { type: 'boolean', nullable: false },
is_active: { type: 'boolean', nullable: false },
created_date: { type: 'dateTime', nullable: false },
updated_date: { type: 'dateTime', nullable: false }
}
Expand Down
19 changes: 0 additions & 19 deletions data/scripts/migrate.js

This file was deleted.

1 change: 0 additions & 1 deletion data/scripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var db = require( '../index' ),


var init = function() {

var args = process.argv[ 2 ],
shouldSeed = ( args && ( args === '--seed' || args === '-s' ) ),
records = ( shouldSeed ) ? seed : null;
Expand Down
5 changes: 3 additions & 2 deletions data/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ var date = moment().format(),
var seedData = {

ribot: [
{ id: uuids[ 0 ], first_name: 'Robert', last_name: 'Douglas', email: '[email protected]', date_of_birth: '1970-01-01', avatar: 'http://example.com/avatar.png', hex_color: '#C0FFEE', is_authenticated: true, created_date: date, updated_date: date },
{ id: uuids[ 1 ], first_name: 'Matt', last_name: 'Oakes', email: '[email protected]', date_of_birth: '1970-02-02', avatar: 'http://example.com/avatar2.png', hex_color: '#C0FFEE', is_authenticated: false, created_date: date, updated_date: date }
{ id: uuids[ 0 ], first_name: 'Robert', last_name: 'Douglas', email: '[email protected]', date_of_birth: '1970-01-01', avatar: 'http://example.com/avatar.png', hex_color: '#C0FFEE', is_authenticated: true, is_active: true, created_date: date, updated_date: date },
{ id: uuids[ 1 ], first_name: 'Matt', last_name: 'Oakes', email: '[email protected]', date_of_birth: '1970-02-02', avatar: 'http://example.com/avatar2.png', hex_color: '#C0FFEE', is_authenticated: false, is_active: true, created_date: date, updated_date: date },
{ id: uuids[ 2 ], first_name: 'Olly', last_name: 'Thomas', email: '[email protected]', date_of_birth: '1970-03-03', avatar: 'http://example.com/avatar3.png', hex_color: '#C0FFEE', is_authenticated: true, is_active: false, created_date: date, updated_date: date }
],

access_token: [
Expand Down
Loading

0 comments on commit e837dea

Please sign in to comment.