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),
61 listRandomPodIdsWithRequest
,
76 // ------------------------------ METHODS ------------------------------
78 function toFormatedJSON () {
84 createdAt: this.createdAt
90 // ------------------------------ Statics ------------------------------
92 function associate (models
) {
93 this.belongsToMany(models
.Request
, {
95 through: models
.RequestToPod
,
100 function countAll (callback
) {
101 return this.count().asCallback(callback
)
104 function incrementScores (ids
, value
, callback
) {
105 if (!callback
) callback = function () {}
108 score: this.sequelize
.literal('score +' + value
)
117 // In this case score is a literal and not an integer so we do not validate it
121 return this.update(update
, options
).asCallback(callback
)
124 function list (callback
) {
125 return this.findAll().asCallback(callback
)
128 function listAllIds (transaction
, callback
) {
130 callback
= transaction
138 if (transaction
) query
.transaction
= transaction
140 return this.findAll(query
).asCallback(function (err
, pods
) {
141 if (err
) return callback(err
)
143 return callback(null, map(pods
, 'id'))
147 function listRandomPodIdsWithRequest (limit
, callback
) {
150 self
.count().asCallback(function (err
, count
) {
151 if (err
) return callback(err
)
154 if (count
=== 0) return callback(null, [])
156 let start
= Math
.floor(Math
.random() * count
) - limit
157 if (start
< 0) start
= 0
160 attributes: [ 'id' ],
169 this.sequelize
.literal('SELECT "podId" FROM "RequestToPods"')
175 return this.findAll(query
).asCallback(function (err
, pods
) {
176 if (err
) return callback(err
)
178 return callback(null, map(pods
, 'id'))
183 function listBadPods (callback
) {
190 return this.findAll(query
).asCallback(callback
)
193 function load (id
, callback
) {
194 return this.findById(id
).asCallback(callback
)
197 function loadByHost (host
, callback
) {
204 return this.findOne(query
).asCallback(callback
)
207 function removeAll (callback
) {
208 return this.destroy().asCallback(callback
)