Skip to content

Instantly share code, notes, and snippets.

@raphaklaus
Last active June 24, 2016 20:33
Show Gist options
  • Save raphaklaus/5514c465027b0a56a36a7d0e79fbf6ff to your computer and use it in GitHub Desktop.
Save raphaklaus/5514c465027b0a56a36a7d0e79fbf6ff to your computer and use it in GitHub Desktop.
class Inviter {
constructor() {
// this.groupId = groupId;
// this.userId = userId;
this.GruposPool = bookshelf.Model.extend({
tableName: 'MensageriaExternaGrupoPool'
});
bookshelf.transaction((t) => {
new this.GruposPool().where({disponivel: true}).orderBy('id', 'ASC')
.fetchAll().then(grupos => {
if (grupos.toJSON().length > 0) {
let grupo = grupos.last().toJSON();
return new this.GruposPool({id: grupo.id})
.save({disponivel: false},
{transacting: t, patch: true});
} else {
logger.info('There are no groups available. ' +
'Update not executed.');
}
})
.then((savedModel) => {
logger.info('Done!');
t.commit(savedModel);
})
.catch(error => {
logger.info(`Rollback! ${error}`);
t.rollback(error);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment