]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
67bf9b96 | 3 | const values = require('lodash/values') |
9f10b292 | 4 | |
f0f5567b | 5 | const constants = require('../initializers/constants') |
9f10b292 | 6 | |
00057e85 | 7 | // --------------------------------------------------------------------------- |
9f10b292 | 8 | |
feb4bdfd C |
9 | module.exports = function (sequelize, DataTypes) { |
10 | const Request = sequelize.define('Request', | |
11 | { | |
12 | request: { | |
67bf9b96 C |
13 | type: DataTypes.JSON, |
14 | allowNull: false | |
feb4bdfd C |
15 | }, |
16 | endpoint: { | |
67bf9b96 C |
17 | type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)), |
18 | allowNull: false | |
feb4bdfd C |
19 | } |
20 | }, | |
4b08096b | 21 | { |
feb4bdfd C |
22 | classMethods: { |
23 | associate, | |
24 | ||
c1a7ab7f C |
25 | listWithLimitAndRandom, |
26 | ||
feb4bdfd | 27 | countTotalRequests, |
c1a7ab7f C |
28 | removeAll, |
29 | removeWithEmptyTo | |
feb4bdfd | 30 | } |
4b08096b | 31 | } |
feb4bdfd | 32 | ) |
528a9efa | 33 | |
feb4bdfd C |
34 | return Request |
35 | } | |
00057e85 C |
36 | |
37 | // ------------------------------ STATICS ------------------------------ | |
38 | ||
feb4bdfd C |
39 | function associate (models) { |
40 | this.belongsToMany(models.Pod, { | |
41 | foreignKey: { | |
42 | name: 'requestId', | |
43 | allowNull: false | |
44 | }, | |
45 | through: models.RequestToPod, | |
46 | onDelete: 'CASCADE' | |
47 | }) | |
48 | } | |
49 | ||
feb4bdfd | 50 | function countTotalRequests (callback) { |
e4c87ec2 C |
51 | // We need to include Pod because there are no cascade delete when a pod is removed |
52 | // So we could count requests that do not have existing pod anymore | |
feb4bdfd C |
53 | const query = { |
54 | include: [ this.sequelize.models.Pod ] | |
55 | } | |
56 | ||
57 | return this.count(query).asCallback(callback) | |
58 | } | |
59 | ||
bd14d16a | 60 | function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { |
43666d61 | 61 | const self = this |
bd14d16a | 62 | const Pod = this.sequelize.models.Pod |
43666d61 | 63 | |
9e167724 | 64 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) { |
43666d61 C |
65 | if (err) return callback(err) |
66 | ||
bd14d16a C |
67 | // We don't have friends that have requests |
68 | if (podIds.length === 0) return callback(null, []) | |
43666d61 | 69 | |
9e167724 | 70 | // The first x requests of these pods |
bd14d16a | 71 | // It is very important to sort by id ASC to keep the requests order! |
feb4bdfd C |
72 | const query = { |
73 | order: [ | |
74 | [ 'id', 'ASC' ] | |
75 | ], | |
bd14d16a C |
76 | include: [ |
77 | { | |
78 | model: self.sequelize.models.Pod, | |
79 | where: { | |
80 | id: { | |
81 | $in: podIds | |
82 | } | |
83 | } | |
84 | } | |
85 | ] | |
feb4bdfd C |
86 | } |
87 | ||
bd14d16a C |
88 | self.findAll(query).asCallback(function (err, requests) { |
89 | if (err) return callback(err) | |
90 | ||
91 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | |
92 | return callback(err, requestsGrouped) | |
93 | }) | |
94 | }) | |
95 | } | |
96 | ||
00057e85 | 97 | function removeAll (callback) { |
feb4bdfd | 98 | // Delete all requests |
7920c273 | 99 | this.truncate({ cascade: true }).asCallback(callback) |
00057e85 C |
100 | } |
101 | ||
102 | function removeWithEmptyTo (callback) { | |
103 | if (!callback) callback = function () {} | |
104 | ||
feb4bdfd C |
105 | const query = { |
106 | where: { | |
107 | id: { | |
108 | $notIn: [ | |
109 | this.sequelize.literal('SELECT "requestId" FROM "RequestToPods"') | |
110 | ] | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | this.destroy(query).asCallback(callback) | |
00057e85 | 116 | } |
c1a7ab7f C |
117 | |
118 | // --------------------------------------------------------------------------- | |
119 | ||
120 | function groupAndTruncateRequests (requests, limitRequestsPerPod) { | |
121 | const requestsGrouped = {} | |
122 | ||
123 | requests.forEach(function (request) { | |
124 | request.Pods.forEach(function (pod) { | |
125 | if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = [] | |
126 | ||
127 | if (requestsGrouped[pod.id].length < limitRequestsPerPod) { | |
128 | requestsGrouped[pod.id].push({ | |
129 | request, | |
130 | pod | |
131 | }) | |
132 | } | |
133 | }) | |
134 | }) | |
135 | ||
136 | return requestsGrouped | |
137 | } |