aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request/request.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/request/request.ts')
-rw-r--r--server/models/request/request.ts28
1 files changed, 11 insertions, 17 deletions
diff --git a/server/models/request/request.ts b/server/models/request/request.ts
index 66e7da845..c3ce2cd4e 100644
--- a/server/models/request/request.ts
+++ b/server/models/request/request.ts
@@ -5,7 +5,6 @@ import { database as db } from '../../initializers/database'
5import { REQUEST_ENDPOINTS } from '../../initializers' 5import { REQUEST_ENDPOINTS } from '../../initializers'
6import { addMethodsToModel } from '../utils' 6import { addMethodsToModel } from '../utils'
7import { 7import {
8 RequestClass,
9 RequestInstance, 8 RequestInstance,
10 RequestAttributes, 9 RequestAttributes,
11 10
@@ -60,25 +59,23 @@ function associate (models) {
60 }) 59 })
61} 60}
62 61
63countTotalRequests = function (callback: RequestMethods.CountTotalRequestsCallback) { 62countTotalRequests = function () {
64 // 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
65 // 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
66 const query = { 65 const query = {
67 include: [ Request['sequelize'].models.Pod ] 66 include: [ Request['sequelize'].models.Pod ]
68 } 67 }
69 68
70 return Request.count(query).asCallback(callback) 69 return Request.count(query)
71} 70}
72 71
73listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestMethods.ListWithLimitAndRandomCallback) { 72listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
74 const Pod = db.Pod 73 const Pod = db.Pod
75 const tableJoin = '' 74 const tableJoin = ''
76 75
77 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', '', function (err, podIds) { 76 return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', tableJoin).then(podIds => {
78 if (err) return callback(err)
79
80 // We don't have friends that have requests 77 // We don't have friends that have requests
81 if (podIds.length === 0) return callback(null, []) 78 if (podIds.length === 0) return []
82 79
83 // The first x requests of these pods 80 // The first x requests of these pods
84 // It is very important to sort by id ASC to keep the requests order! 81 // It is very important to sort by id ASC to keep the requests order!
@@ -98,23 +95,20 @@ listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: numbe
98 ] 95 ]
99 } 96 }
100 97
101 Request.findAll(query).asCallback(function (err, requests) { 98 return Request.findAll(query).then(requests => {
102 if (err) return callback(err)
103 99
104 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) 100 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
105 return callback(err, requestsGrouped) 101 return requestsGrouped
106 }) 102 })
107 }) 103 })
108} 104}
109 105
110removeAll = function (callback: RequestMethods.RemoveAllCallback) { 106removeAll = function () {
111 // Delete all requests 107 // Delete all requests
112 Request.truncate({ cascade: true }).asCallback(callback) 108 return Request.truncate({ cascade: true })
113} 109}
114 110
115removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallback) { 111removeWithEmptyTo = function () {
116 if (!callback) callback = function () { /* empty */ }
117
118 const query = { 112 const query = {
119 where: { 113 where: {
120 id: { 114 id: {
@@ -125,7 +119,7 @@ removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallbac
125 } 119 }
126 } 120 }
127 121
128 Request.destroy(query).asCallback(callback) 122 return Request.destroy(query)
129} 123}
130 124
131// --------------------------------------------------------------------------- 125// ---------------------------------------------------------------------------