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