diff options
Diffstat (limited to 'server/models/request.ts')
-rw-r--r-- | server/models/request.ts | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/server/models/request.ts b/server/models/request.ts index 18fa291fa..71f81ae66 100644 --- a/server/models/request.ts +++ b/server/models/request.ts | |||
@@ -1,15 +1,16 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as Sequelize from 'sequelize' | 2 | import * as Sequelize from 'sequelize' |
3 | 3 | ||
4 | import { database as db } from '../initializers/database' | ||
4 | import { REQUEST_ENDPOINTS } from '../initializers' | 5 | import { REQUEST_ENDPOINTS } from '../initializers' |
5 | |||
6 | import { addMethodsToModel } from './utils' | 6 | import { addMethodsToModel } from './utils' |
7 | import { | 7 | import { |
8 | RequestClass, | 8 | RequestClass, |
9 | RequestInstance, | 9 | RequestInstance, |
10 | RequestAttributes, | 10 | RequestAttributes, |
11 | 11 | ||
12 | RequestMethods | 12 | RequestMethods, |
13 | RequestsGrouped | ||
13 | } from './request-interface' | 14 | } from './request-interface' |
14 | 15 | ||
15 | let Request: Sequelize.Model<RequestInstance, RequestAttributes> | 16 | let Request: Sequelize.Model<RequestInstance, RequestAttributes> |
@@ -59,7 +60,7 @@ function associate (models) { | |||
59 | }) | 60 | }) |
60 | } | 61 | } |
61 | 62 | ||
62 | countTotalRequests = function (callback) { | 63 | countTotalRequests = function (callback: RequestMethods.CountTotalRequestsCallback) { |
63 | // We need to include Pod because there are no cascade delete when a pod is removed | 64 | // We need to include Pod because there are no cascade delete when a pod is removed |
64 | // So we could count requests that do not have existing pod anymore | 65 | // So we could count requests that do not have existing pod anymore |
65 | const query = { | 66 | const query = { |
@@ -69,10 +70,11 @@ countTotalRequests = function (callback) { | |||
69 | return Request.count(query).asCallback(callback) | 70 | return Request.count(query).asCallback(callback) |
70 | } | 71 | } |
71 | 72 | ||
72 | listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { | 73 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestMethods.ListWithLimitAndRandomCallback) { |
73 | const Pod = Request['sequelize'].models.Pod | 74 | const Pod = db.Pod |
75 | const tableJoin = '' | ||
74 | 76 | ||
75 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) { | 77 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', '', function (err, podIds) { |
76 | if (err) return callback(err) | 78 | if (err) return callback(err) |
77 | 79 | ||
78 | // We don't have friends that have requests | 80 | // We don't have friends that have requests |
@@ -105,12 +107,12 @@ listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { | |||
105 | }) | 107 | }) |
106 | } | 108 | } |
107 | 109 | ||
108 | removeAll = function (callback) { | 110 | removeAll = function (callback: RequestMethods.RemoveAllCallback) { |
109 | // Delete all requests | 111 | // Delete all requests |
110 | Request.truncate({ cascade: true }).asCallback(callback) | 112 | Request.truncate({ cascade: true }).asCallback(callback) |
111 | } | 113 | } |
112 | 114 | ||
113 | removeWithEmptyTo = function (callback) { | 115 | removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallback) { |
114 | if (!callback) callback = function () { /* empty */ } | 116 | if (!callback) callback = function () { /* empty */ } |
115 | 117 | ||
116 | const query = { | 118 | const query = { |
@@ -128,8 +130,8 @@ removeWithEmptyTo = function (callback) { | |||
128 | 130 | ||
129 | // --------------------------------------------------------------------------- | 131 | // --------------------------------------------------------------------------- |
130 | 132 | ||
131 | function groupAndTruncateRequests (requests, limitRequestsPerPod) { | 133 | function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) { |
132 | const requestsGrouped = {} | 134 | const requestsGrouped: RequestsGrouped = {} |
133 | 135 | ||
134 | requests.forEach(function (request) { | 136 | requests.forEach(function (request) { |
135 | request.Pods.forEach(function (pod) { | 137 | request.Pods.forEach(function (pod) { |