const query = {
where: {
name: username,
- podId: pod.id
+ podId: pod.id,
+ userId: null
},
defaults: {
name: username,
- podId: pod.id
+ podId: pod.id,
+ userId: null
},
transaction: t
}
},
function findOrCreateAuthor (t, callback) {
- const username = res.locals.oauth.token.user.username
+ const user = res.locals.oauth.token.User
const query = {
where: {
- name: username,
- podId: null
+ name: user.username,
+ podId: null,
+ userId: user.id
},
defaults: {
- name: username,
- podId: null // null because it is OUR pod
+ name: user.username,
+ podId: null, // null because it is OUR pod
+ userId: user.id
},
transaction: t
}
db.Author.findOrCreate(query).asCallback(function (err, result) {
- // [ instance, wasCreated ]
- return callback(err, t, result[0])
+ const authorInstance = result[0]
+
+ return callback(err, t, authorInstance)
})
},
}
function clientsExist (callback) {
- db.OAuthClient.list(function (err, clients) {
+ db.OAuthClient.countTotal(function (err, totalClients) {
if (err) return callback(err)
- return callback(null, clients.length !== 0)
+ return callback(null, totalClients !== 0)
})
}
},
{
fields: [ 'podId' ]
+ },
+ {
+ fields: [ 'userId' ]
}
],
classMethods: {
},
onDelete: 'cascade'
})
+
+ this.belongsTo(models.User, {
+ foreignKey: {
+ name: 'userId',
+ allowNull: true
+ },
+ onDelete: 'cascade'
+ })
}
}
],
classMethods: {
+ countTotal,
getByIdAndSecret,
- list,
loadFirstClient
}
}
// ---------------------------------------------------------------------------
-function list (callback) {
- return this.findAll().asCallback(callback)
+function countTotal (callback) {
+ return this.count().asCallback(callback)
}
function loadFirstClient (callback) {
// ------------------------------ STATICS ------------------------------
function associate (models) {
+ this.hasOne(models.Author, {
+ foreignKey: 'userId',
+ onDelete: 'cascade'
+ })
+
this.hasMany(models.OAuthToken, {
foreignKey: 'userId',
onDelete: 'cascade'