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
37 type: DataTypes
.STRING(400),
58 listRandomPodIdsWithRequest
,
73 // ------------------------------ METHODS ------------------------------
75 function toFormatedJSON () {
81 createdAt: this.createdAt
87 // ------------------------------ Statics ------------------------------
89 function associate (models
) {
90 this.belongsToMany(models
.Request
, {
92 through: models
.RequestToPod
,
97 function countAll (callback
) {
98 return this.count().asCallback(callback
)
101 function incrementScores (ids
, value
, callback
) {
102 if (!callback
) callback = function () {}
105 score: this.sequelize
.literal('score +' + value
)
114 // In this case score is a literal and not an integer so we do not validate it
118 return this.update(update
, options
).asCallback(callback
)
121 function list (callback
) {
122 return this.findAll().asCallback(callback
)
125 function listAllIds (transaction
, callback
) {
127 callback
= transaction
135 if (transaction
) query
.transaction
= transaction
137 return this.findAll(query
).asCallback(function (err
, pods
) {
138 if (err
) return callback(err
)
140 return callback(null, map(pods
, 'id'))
144 function listRandomPodIdsWithRequest (limit
, callback
) {
147 self
.count().asCallback(function (err
, count
) {
148 if (err
) return callback(err
)
151 if (count
=== 0) return callback(null, [])
153 let start
= Math
.floor(Math
.random() * count
) - limit
154 if (start
< 0) start
= 0
157 attributes: [ 'id' ],
166 this.sequelize
.literal('SELECT "podId" FROM "RequestToPods"')
172 return this.findAll(query
).asCallback(function (err
, pods
) {
173 if (err
) return callback(err
)
175 return callback(null, map(pods
, 'id'))
180 function listBadPods (callback
) {
187 return this.findAll(query
).asCallback(callback
)
190 function load (id
, callback
) {
191 return this.findById(id
).asCallback(callback
)
194 function loadByHost (host
, callback
) {
201 return this.findOne(query
).asCallback(callback
)
204 function removeAll (callback
) {
205 return this.destroy().asCallback(callback
)