2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2016-12-28 08:49:23 -06:00
|
|
|
const values = require('lodash/values')
|
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-06-28 13:10:32 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
const Request = sequelize.define('Request',
|
|
|
|
{
|
|
|
|
request: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.JSON,
|
|
|
|
allowNull: false
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
|
|
|
endpoint: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)),
|
|
|
|
allowNull: false
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
},
|
2016-11-01 12:47:57 -05:00
|
|
|
{
|
2016-12-11 14:50:51 -06:00
|
|
|
classMethods: {
|
|
|
|
associate,
|
|
|
|
|
2017-02-18 03:29:36 -06:00
|
|
|
listWithLimitAndRandom,
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
countTotalRequests,
|
2017-02-18 03:29:36 -06:00
|
|
|
removeAll,
|
|
|
|
removeWithEmptyTo
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
2016-11-01 12:47:57 -05:00
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
)
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return Request
|
|
|
|
}
|
2016-06-28 13:10:32 -05:00
|
|
|
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
function associate (models) {
|
|
|
|
this.belongsToMany(models.Pod, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'requestId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
through: models.RequestToPod,
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function countTotalRequests (callback) {
|
|
|
|
const query = {
|
|
|
|
include: [ this.sequelize.models.Pod ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.count(query).asCallback(callback)
|
|
|
|
}
|
|
|
|
|
2017-01-10 15:24:42 -06:00
|
|
|
function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
|
2016-10-23 12:14:28 -05:00
|
|
|
const self = this
|
2017-01-10 15:24:42 -06:00
|
|
|
const Pod = this.sequelize.models.Pod
|
2016-10-23 12:14:28 -05:00
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) {
|
2016-10-23 12:14:28 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2017-01-10 15:24:42 -06:00
|
|
|
// We don't have friends that have requests
|
|
|
|
if (podIds.length === 0) return callback(null, [])
|
2016-10-23 12:14:28 -05:00
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
// The first x requests of these pods
|
2017-01-10 15:24:42 -06:00
|
|
|
// It is very important to sort by id ASC to keep the requests order!
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
order: [
|
|
|
|
[ 'id', 'ASC' ]
|
|
|
|
],
|
2017-01-10 15:24:42 -06:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: self.sequelize.models.Pod,
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: podIds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2017-01-10 15:24:42 -06:00
|
|
|
self.findAll(query).asCallback(function (err, requests) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
|
|
|
|
return callback(err, requestsGrouped)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-28 13:10:32 -05:00
|
|
|
function removeAll (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
// Delete all requests
|
2016-12-24 09:59:17 -06:00
|
|
|
this.truncate({ cascade: true }).asCallback(callback)
|
2016-06-28 13:10:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeWithEmptyTo (callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$notIn: [
|
|
|
|
this.sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.destroy(query).asCallback(callback)
|
2016-06-28 13:10:32 -05:00
|
|
|
}
|
2017-02-18 03:29:36 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function groupAndTruncateRequests (requests, limitRequestsPerPod) {
|
|
|
|
const requestsGrouped = {}
|
|
|
|
|
|
|
|
requests.forEach(function (request) {
|
|
|
|
request.Pods.forEach(function (pod) {
|
|
|
|
if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
|
|
|
|
|
|
|
|
if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
|
|
|
|
requestsGrouped[pod.id].push({
|
|
|
|
request,
|
|
|
|
pod
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return requestsGrouped
|
|
|
|
}
|