3 const map
= require('lodash/map')
5 const constants
= require('../initializers/constants')
6 const customPodsValidators
= require('../helpers/custom-validators').pods
8 // ---------------------------------------------------------------------------
10 module
.exports = function (sequelize
, DataTypes
) {
11 const Pod
= sequelize
.define('Pod',
14 type: DataTypes
.STRING
,
17 isHost: function (value
) {
18 const res
= customPodsValidators
.isHostValid(value
)
19 if (res
=== false) throw new Error('Host not valid.')
24 type: DataTypes
.STRING(5000),
28 type: DataTypes
.INTEGER
,
29 defaultValue: constants
.FRIEND_SCORE
.BASE
,
33 max: constants
.FRIEND_SCORE
.MAX
53 listRandomPodIdsWithRequest
,
68 // ------------------------------ METHODS ------------------------------
70 function toFormatedJSON () {
75 createdAt: this.createdAt
81 // ------------------------------ Statics ------------------------------
83 function associate (models
) {
84 this.belongsToMany(models
.Request
, {
86 through: models
.RequestToPod
,
91 function countAll (callback
) {
92 return this.count().asCallback(callback
)
95 function incrementScores (ids
, value
, callback
) {
96 if (!callback
) callback = function () {}
99 score: this.sequelize
.literal('score +' + value
)
108 // In this case score is a literal and not an integer so we do not validate it
112 return this.update(update
, options
).asCallback(callback
)
115 function list (callback
) {
116 return this.findAll().asCallback(callback
)
119 function listAllIds (transaction
, callback
) {
121 callback
= transaction
129 if (transaction
) query
.transaction
= transaction
131 return this.findAll(query
).asCallback(function (err
, pods
) {
132 if (err
) return callback(err
)
134 return callback(null, map(pods
, 'id'))
138 function listRandomPodIdsWithRequest (limit
, callback
) {
141 self
.count().asCallback(function (err
, count
) {
142 if (err
) return callback(err
)
145 if (count
=== 0) return callback(null, [])
147 let start
= Math
.floor(Math
.random() * count
) - limit
148 if (start
< 0) start
= 0
151 attributes: [ 'id' ],
160 this.sequelize
.literal('SELECT "podId" FROM "RequestToPods"')
166 return this.findAll(query
).asCallback(function (err
, pods
) {
167 if (err
) return callback(err
)
169 return callback(null, map(pods
, 'id'))
174 function listBadPods (callback
) {
181 return this.findAll(query
).asCallback(callback
)
184 function load (id
, callback
) {
185 return this.findById(id
).asCallback(callback
)
188 function loadByHost (host
, callback
) {
195 return this.findOne(query
).asCallback(callback
)
198 function removeAll (callback
) {
199 return this.destroy().asCallback(callback
)