diff options
Diffstat (limited to 'server/models/request.ts')
-rw-r--r-- | server/models/request.ts | 69 |
1 files changed, 41 insertions, 28 deletions
diff --git a/server/models/request.ts b/server/models/request.ts index 672f79d11..18fa291fa 100644 --- a/server/models/request.ts +++ b/server/models/request.ts | |||
@@ -1,11 +1,25 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as Sequelize from 'sequelize' | ||
2 | 3 | ||
3 | import { REQUEST_ENDPOINTS } from '../initializers' | 4 | import { REQUEST_ENDPOINTS } from '../initializers' |
4 | 5 | ||
5 | // --------------------------------------------------------------------------- | 6 | import { addMethodsToModel } from './utils' |
7 | import { | ||
8 | RequestClass, | ||
9 | RequestInstance, | ||
10 | RequestAttributes, | ||
11 | |||
12 | RequestMethods | ||
13 | } from './request-interface' | ||
14 | |||
15 | let Request: Sequelize.Model<RequestInstance, RequestAttributes> | ||
16 | let countTotalRequests: RequestMethods.CountTotalRequests | ||
17 | let listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom | ||
18 | let removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo | ||
19 | let removeAll: RequestMethods.RemoveAll | ||
6 | 20 | ||
7 | module.exports = function (sequelize, DataTypes) { | 21 | export default function (sequelize, DataTypes) { |
8 | const Request = sequelize.define('Request', | 22 | Request = sequelize.define('Request', |
9 | { | 23 | { |
10 | request: { | 24 | request: { |
11 | type: DataTypes.JSON, | 25 | type: DataTypes.JSON, |
@@ -15,27 +29,27 @@ module.exports = function (sequelize, DataTypes) { | |||
15 | type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)), | 29 | type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)), |
16 | allowNull: false | 30 | allowNull: false |
17 | } | 31 | } |
18 | }, | ||
19 | { | ||
20 | classMethods: { | ||
21 | associate, | ||
22 | |||
23 | listWithLimitAndRandom, | ||
24 | |||
25 | countTotalRequests, | ||
26 | removeAll, | ||
27 | removeWithEmptyTo | ||
28 | } | ||
29 | } | 32 | } |
30 | ) | 33 | ) |
31 | 34 | ||
35 | const classMethods = [ | ||
36 | associate, | ||
37 | |||
38 | listWithLimitAndRandom, | ||
39 | |||
40 | countTotalRequests, | ||
41 | removeAll, | ||
42 | removeWithEmptyTo | ||
43 | ] | ||
44 | addMethodsToModel(Request, classMethods) | ||
45 | |||
32 | return Request | 46 | return Request |
33 | } | 47 | } |
34 | 48 | ||
35 | // ------------------------------ STATICS ------------------------------ | 49 | // ------------------------------ STATICS ------------------------------ |
36 | 50 | ||
37 | function associate (models) { | 51 | function associate (models) { |
38 | this.belongsToMany(models.Pod, { | 52 | Request.belongsToMany(models.Pod, { |
39 | foreignKey: { | 53 | foreignKey: { |
40 | name: 'requestId', | 54 | name: 'requestId', |
41 | allowNull: false | 55 | allowNull: false |
@@ -45,19 +59,18 @@ function associate (models) { | |||
45 | }) | 59 | }) |
46 | } | 60 | } |
47 | 61 | ||
48 | function countTotalRequests (callback) { | 62 | countTotalRequests = function (callback) { |
49 | // We need to include Pod because there are no cascade delete when a pod is removed | 63 | // 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 | 64 | // So we could count requests that do not have existing pod anymore |
51 | const query = { | 65 | const query = { |
52 | include: [ this.sequelize.models.Pod ] | 66 | include: [ Request['sequelize'].models.Pod ] |
53 | } | 67 | } |
54 | 68 | ||
55 | return this.count(query).asCallback(callback) | 69 | return Request.count(query).asCallback(callback) |
56 | } | 70 | } |
57 | 71 | ||
58 | function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | 72 | listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { |
59 | const self = this | 73 | const Pod = Request['sequelize'].models.Pod |
60 | const Pod = this.sequelize.models.Pod | ||
61 | 74 | ||
62 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) { | 75 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) { |
63 | if (err) return callback(err) | 76 | if (err) return callback(err) |
@@ -73,7 +86,7 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
73 | ], | 86 | ], |
74 | include: [ | 87 | include: [ |
75 | { | 88 | { |
76 | model: self.sequelize.models.Pod, | 89 | model: Request['sequelize'].models.Pod, |
77 | where: { | 90 | where: { |
78 | id: { | 91 | id: { |
79 | $in: podIds | 92 | $in: podIds |
@@ -83,7 +96,7 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
83 | ] | 96 | ] |
84 | } | 97 | } |
85 | 98 | ||
86 | self.findAll(query).asCallback(function (err, requests) { | 99 | Request.findAll(query).asCallback(function (err, requests) { |
87 | if (err) return callback(err) | 100 | if (err) return callback(err) |
88 | 101 | ||
89 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | 102 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) |
@@ -92,25 +105,25 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
92 | }) | 105 | }) |
93 | } | 106 | } |
94 | 107 | ||
95 | function removeAll (callback) { | 108 | removeAll = function (callback) { |
96 | // Delete all requests | 109 | // Delete all requests |
97 | this.truncate({ cascade: true }).asCallback(callback) | 110 | Request.truncate({ cascade: true }).asCallback(callback) |
98 | } | 111 | } |
99 | 112 | ||
100 | function removeWithEmptyTo (callback) { | 113 | removeWithEmptyTo = function (callback) { |
101 | if (!callback) callback = function () { /* empty */ } | 114 | if (!callback) callback = function () { /* empty */ } |
102 | 115 | ||
103 | const query = { | 116 | const query = { |
104 | where: { | 117 | where: { |
105 | id: { | 118 | id: { |
106 | $notIn: [ | 119 | $notIn: [ |
107 | this.sequelize.literal('SELECT "requestId" FROM "RequestToPods"') | 120 | Sequelize.literal('SELECT "requestId" FROM "RequestToPods"') |
108 | ] | 121 | ] |
109 | } | 122 | } |
110 | } | 123 | } |
111 | } | 124 | } |
112 | 125 | ||
113 | this.destroy(query).asCallback(callback) | 126 | Request.destroy(query).asCallback(callback) |
114 | } | 127 | } |
115 | 128 | ||
116 | // --------------------------------------------------------------------------- | 129 | // --------------------------------------------------------------------------- |