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