Skip to content

Commit

Permalink
fix(user): prevent dupes
Browse files Browse the repository at this point in the history
  • Loading branch information
balthazar committed Oct 8, 2015
1 parent 5da7860 commit 3593039
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 0 additions & 1 deletion api/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ q.all([
Language.sync(),
])
.then(() => { return Text.sync(); })
.then(() => { console.log('Success'); })
.catch(err => { console.log(err); });

export default {
Expand Down
2 changes: 1 addition & 1 deletion api/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export default db.define('user', {
banned: { type: Sequelize.BOOLEAN, defaultValue: false },
admin: { type: Sequelize.BOOLEAN, defaultValue: false },

githubId: { type: Sequelize.BIGINT }
githubId: { type: Sequelize.BIGINT, unique: true }

});
8 changes: 6 additions & 2 deletions api/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ exports.getByGithub = id => {

exports.updateOrCreate = (githubId, name, avatar) => {

return User.upsert({ name, avatar, githubId }, { fields: ['name', 'avatar'] })
return exports.getByGithub(githubId)
.then(user => {
if (!user) { return User.create({ githubId, name, avatar }); }
return user;
})
.then(user => {
if (user.banned) { throw new Error('You are banned from KeyCode.'); }
return user;
});
})

};

0 comments on commit 3593039

Please sign in to comment.