2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2016-06-28 13:10:32 -05:00
|
|
|
const map = require('lodash/map')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const constants = require('../initializers/constants')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
const Pod = sequelize.define('Pod',
|
|
|
|
{
|
|
|
|
host: {
|
|
|
|
type: DataTypes.STRING
|
|
|
|
},
|
|
|
|
publicKey: {
|
|
|
|
type: DataTypes.STRING(5000)
|
|
|
|
},
|
|
|
|
score: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
defaultValue: constants.FRIEND_SCORE.BASE
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
classMethods: {
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countAll,
|
|
|
|
incrementScores,
|
|
|
|
list,
|
|
|
|
listAllIds,
|
|
|
|
listBadPods,
|
|
|
|
load,
|
|
|
|
loadByHost,
|
|
|
|
removeAll
|
|
|
|
},
|
|
|
|
instanceMethods: {
|
|
|
|
toFormatedJSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return Pod
|
2016-08-26 11:55:10 -05:00
|
|
|
}
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
// TODO: max score -> constants.FRIENDS_SCORE.MAX
|
|
|
|
// TODO: validation
|
|
|
|
// PodSchema.path('host').validate(validator.isURL)
|
|
|
|
// PodSchema.path('publicKey').required(true)
|
|
|
|
// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2016-08-26 11:55:10 -05:00
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
const json = {
|
2016-12-11 14:50:51 -06:00
|
|
|
id: this.id,
|
2016-11-14 13:03:04 -06:00
|
|
|
host: this.host,
|
2016-08-26 11:55:10 -05:00
|
|
|
score: this.score,
|
2016-12-11 14:50:51 -06:00
|
|
|
createdAt: this.createdAt
|
2016-08-26 11:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
// ------------------------------ Statics ------------------------------
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
function associate (models) {
|
|
|
|
this.belongsToMany(models.Request, {
|
|
|
|
foreignKey: 'podId',
|
|
|
|
through: models.RequestToPod,
|
2016-12-24 09:59:17 -06:00
|
|
|
onDelete: 'cascade'
|
2016-12-11 14:50:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function countAll (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.count().asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-12-11 14:50:51 -06:00
|
|
|
|
|
|
|
const update = {
|
|
|
|
score: this.sequelize.literal('score +' + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: ids
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.update(update, query).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function list (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.findAll().asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-18 09:13:54 -05:00
|
|
|
function listAllIds (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
attributes: [ 'id' ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findAll(query).asCallback(function (err, pods) {
|
2016-06-28 13:10:32 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return callback(null, map(pods, 'id'))
|
2016-06-28 13:10:32 -05:00
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function listBadPods (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
score: { $lte: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findAll(query).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function load (id, callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.findById(id).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-11-14 13:03:04 -06:00
|
|
|
function loadByHost (host, callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
host: host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findOne(query).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function removeAll (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.destroy().asCallback(callback)
|
2016-06-30 15:39:08 -05:00
|
|
|
}
|