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),
57 listRandomPodIdsWithRequest
,
72 // ------------------------------ METHODS ------------------------------
74 function toFormatedJSON () {
80 createdAt: this.createdAt
86 // ------------------------------ Statics ------------------------------
88 function associate (models
) {
89 this.belongsToMany(models
.Request
, {
91 through: models
.RequestToPod
,
96 function countAll (callback
) {
97 return this.count().asCallback(callback
)
100 function incrementScores (ids
, value
, callback
) {
101 if (!callback
) callback = function () {}
104 score: this.sequelize
.literal('score +' + value
)
113 // In this case score is a literal and not an integer so we do not validate it
117 return this.update(update
, options
).asCallback(callback
)
120 function list (callback
) {
121 return this.findAll().asCallback(callback
)
124 function listAllIds (transaction
, callback
) {
126 callback
= transaction
134 if (transaction
) query
.transaction
= transaction
136 return this.findAll(query
).asCallback(function (err
, pods
) {
137 if (err
) return callback(err
)
139 return callback(null, map(pods
, 'id'))
143 function listRandomPodIdsWithRequest (limit
, callback
) {
146 self
.count().asCallback(function (err
, count
) {
147 if (err
) return callback(err
)
150 if (count
=== 0) return callback(null, [])
152 let start
= Math
.floor(Math
.random() * count
) - limit
153 if (start
< 0) start
= 0
156 attributes: [ 'id' ],
165 this.sequelize
.literal('SELECT "podId" FROM "RequestToPods"')
171 return this.findAll(query
).asCallback(function (err
, pods
) {
172 if (err
) return callback(err
)
174 return callback(null, map(pods
, 'id'))
179 function listBadPods (callback
) {
186 return this.findAll(query
).asCallback(callback
)
189 function load (id
, callback
) {
190 return this.findById(id
).asCallback(callback
)
193 function loadByHost (host
, callback
) {
200 return this.findOne(query
).asCallback(callback
)
203 function removeAll (callback
) {
204 return this.destroy().asCallback(callback
)