aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-07-05 13:26:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-07-05 14:14:16 +0200
commit6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch)
tree3365a96d82bc7f00ae504a568725c8e914150cf8 /server/models/request
parent5fe7e898316e18369c3e1aba307b55077adc7bfb (diff)
downloadPeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/models/request')
-rw-r--r--server/models/request/abstract-request-interface.ts12
-rw-r--r--server/models/request/index.ts1
-rw-r--r--server/models/request/request-interface.ts16
-rw-r--r--server/models/request/request-to-pod-interface.ts8
-rw-r--r--server/models/request/request-to-pod.ts7
-rw-r--r--server/models/request/request-video-event-interface.ts22
-rw-r--r--server/models/request/request-video-event.ts27
-rw-r--r--server/models/request/request-video-qadu-interface.ts22
-rw-r--r--server/models/request/request-video-qadu.ts27
-rw-r--r--server/models/request/request.ts28
10 files changed, 82 insertions, 88 deletions
diff --git a/server/models/request/abstract-request-interface.ts b/server/models/request/abstract-request-interface.ts
new file mode 100644
index 000000000..a384f4d27
--- /dev/null
+++ b/server/models/request/abstract-request-interface.ts
@@ -0,0 +1,12 @@
1import * as Promise from 'bluebird'
2
3export interface AbstractRequestClass <T> {
4 countTotalRequests: () => Promise<number>
5 listWithLimitAndRandom: (limitPods: number, limitRequestsPerPod: number) => Promise<T>
6 removeWithEmptyTo: () => Promise<number>
7 removeAll: () => Promise<void>
8}
9
10export interface AbstractRequestToPodClass {
11 removeByRequestIdsAndPod: (ids: number[], podId: number) => Promise<number>
12}
diff --git a/server/models/request/index.ts b/server/models/request/index.ts
index 824c140f5..3dd6aedc2 100644
--- a/server/models/request/index.ts
+++ b/server/models/request/index.ts
@@ -1,3 +1,4 @@
1export * from './abstract-request-interface'
1export * from './request-interface' 2export * from './request-interface'
2export * from './request-to-pod-interface' 3export * from './request-to-pod-interface'
3export * from './request-video-event-interface' 4export * from './request-video-event-interface'
diff --git a/server/models/request/request-interface.ts b/server/models/request/request-interface.ts
index 483850633..7b0ee4df9 100644
--- a/server/models/request/request-interface.ts
+++ b/server/models/request/request-interface.ts
@@ -1,5 +1,7 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import * as Promise from 'bluebird'
2 3
4import { AbstractRequestClass } from './abstract-request-interface'
3import { PodInstance, PodAttributes } from '../pod' 5import { PodInstance, PodAttributes } from '../pod'
4import { RequestEndpoint } from '../../../shared/models/request-scheduler.model' 6import { RequestEndpoint } from '../../../shared/models/request-scheduler.model'
5 7
@@ -11,20 +13,16 @@ export type RequestsGrouped = {
11} 13}
12 14
13export namespace RequestMethods { 15export namespace RequestMethods {
14 export type CountTotalRequestsCallback = (err: Error, total: number) => void 16 export type CountTotalRequests = () => Promise<number>
15 export type CountTotalRequests = (callback: CountTotalRequestsCallback) => void
16 17
17 export type ListWithLimitAndRandomCallback = (err: Error, requestsGrouped?: RequestsGrouped) => void 18 export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number) => Promise<RequestsGrouped>
18 export type ListWithLimitAndRandom = (limitPods, limitRequestsPerPod, callback: ListWithLimitAndRandomCallback) => void
19 19
20 export type RemoveWithEmptyToCallback = (err: Error) => void 20 export type RemoveWithEmptyTo = () => Promise<number>
21 export type RemoveWithEmptyTo = (callback: RemoveWithEmptyToCallback) => void
22 21
23 export type RemoveAllCallback = (err: Error) => void 22 export type RemoveAll = () => Promise<void>
24 export type RemoveAll = (callback: RemoveAllCallback) => void
25} 23}
26 24
27export interface RequestClass { 25export interface RequestClass extends AbstractRequestClass<RequestsGrouped> {
28 countTotalRequests: RequestMethods.CountTotalRequests 26 countTotalRequests: RequestMethods.CountTotalRequests
29 listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom 27 listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom
30 removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo 28 removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo
diff --git a/server/models/request/request-to-pod-interface.ts b/server/models/request/request-to-pod-interface.ts
index 6d75ca6e5..7ca99f4d4 100644
--- a/server/models/request/request-to-pod-interface.ts
+++ b/server/models/request/request-to-pod-interface.ts
@@ -1,11 +1,13 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import * as Promise from 'bluebird'
3
4import { AbstractRequestToPodClass } from './abstract-request-interface'
2 5
3export namespace RequestToPodMethods { 6export namespace RequestToPodMethods {
4 export type RemoveByRequestIdsAndPodCallback = (err: Error) => void 7 export type RemoveByRequestIdsAndPod = (requestsIds: number[], podId: number) => Promise<number>
5 export type RemoveByRequestIdsAndPod = (requestsIds: number[], podId: number, callback?: RemoveByRequestIdsAndPodCallback) => void
6} 8}
7 9
8export interface RequestToPodClass { 10export interface RequestToPodClass extends AbstractRequestToPodClass {
9 removeByRequestIdsAndPod: RequestToPodMethods.RemoveByRequestIdsAndPod 11 removeByRequestIdsAndPod: RequestToPodMethods.RemoveByRequestIdsAndPod
10} 12}
11 13
diff --git a/server/models/request/request-to-pod.ts b/server/models/request/request-to-pod.ts
index 67331be1d..6678ed290 100644
--- a/server/models/request/request-to-pod.ts
+++ b/server/models/request/request-to-pod.ts
@@ -2,7 +2,6 @@ import * as Sequelize from 'sequelize'
2 2
3import { addMethodsToModel } from '../utils' 3import { addMethodsToModel } from '../utils'
4import { 4import {
5 RequestToPodClass,
6 RequestToPodInstance, 5 RequestToPodInstance,
7 RequestToPodAttributes, 6 RequestToPodAttributes,
8 7
@@ -38,9 +37,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
38 37
39// --------------------------------------------------------------------------- 38// ---------------------------------------------------------------------------
40 39
41removeByRequestIdsAndPod = function (requestsIds: number[], podId: number, callback?: RequestToPodMethods.RemoveByRequestIdsAndPodCallback) { 40removeByRequestIdsAndPod = function (requestsIds: number[], podId: number) {
42 if (!callback) callback = function () { /* empty */ }
43
44 const query = { 41 const query = {
45 where: { 42 where: {
46 requestId: { 43 requestId: {
@@ -50,5 +47,5 @@ removeByRequestIdsAndPod = function (requestsIds: number[], podId: number, callb
50 } 47 }
51 } 48 }
52 49
53 RequestToPod.destroy(query).asCallback(callback) 50 return RequestToPod.destroy(query)
54} 51}
diff --git a/server/models/request/request-video-event-interface.ts b/server/models/request/request-video-event-interface.ts
index 3ed03339a..a5032e1b1 100644
--- a/server/models/request/request-video-event-interface.ts
+++ b/server/models/request/request-video-event-interface.ts
@@ -1,5 +1,7 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import * as Promise from 'bluebird'
2 3
4import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface'
3import { VideoInstance } from '../video' 5import { VideoInstance } from '../video'
4import { PodInstance } from '../pod' 6import { PodInstance } from '../pod'
5 7
@@ -16,20 +18,16 @@ export type RequestsVideoEventGrouped = {
16} 18}
17 19
18export namespace RequestVideoEventMethods { 20export namespace RequestVideoEventMethods {
19 export type CountTotalRequestsCallback = (err: Error, total: number) => void 21 export type CountTotalRequests = () => Promise<number>
20 export type CountTotalRequests = (callback: CountTotalRequestsCallback) => void
21 22
22 export type ListWithLimitAndRandomCallback = (err: Error, requestsGrouped?: RequestsVideoEventGrouped) => void 23 export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number) => Promise<RequestsVideoEventGrouped>
23 export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number, callback: ListWithLimitAndRandomCallback) => void
24 24
25 export type RemoveByRequestIdsAndPodCallback = () => void 25 export type RemoveByRequestIdsAndPod = (ids: number[], podId: number) => Promise<number>
26 export type RemoveByRequestIdsAndPod = (ids: number[], podId: number, callback: RemoveByRequestIdsAndPodCallback) => void
27 26
28 export type RemoveAllCallback = () => void 27 export type RemoveAll = () => Promise<void>
29 export type RemoveAll = (callback: RemoveAllCallback) => void
30} 28}
31 29
32export interface RequestVideoEventClass { 30export interface RequestVideoEventClass extends AbstractRequestClass<RequestsVideoEventGrouped>, AbstractRequestToPodClass {
33 countTotalRequests: RequestVideoEventMethods.CountTotalRequests 31 countTotalRequests: RequestVideoEventMethods.CountTotalRequests
34 listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom 32 listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
35 removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod 33 removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
@@ -41,10 +39,12 @@ export interface RequestVideoEventAttributes {
41 count: number 39 count: number
42} 40}
43 41
44export interface RequestVideoEventInstance extends RequestVideoEventClass, RequestVideoEventAttributes, Sequelize.Instance<RequestVideoEventAttributes> { 42export interface RequestVideoEventInstance
43 extends RequestVideoEventClass, RequestVideoEventAttributes, Sequelize.Instance<RequestVideoEventAttributes> {
45 id: number 44 id: number
46 45
47 Video: VideoInstance 46 Video: VideoInstance
48} 47}
49 48
50export interface RequestVideoEventModel extends RequestVideoEventClass, Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> {} 49export interface RequestVideoEventModel
50 extends RequestVideoEventClass, Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> {}
diff --git a/server/models/request/request-video-event.ts b/server/models/request/request-video-event.ts
index f552ef50b..90ea15702 100644
--- a/server/models/request/request-video-event.ts
+++ b/server/models/request/request-video-event.ts
@@ -10,7 +10,6 @@ import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
10import { isVideoEventCountValid } from '../../helpers' 10import { isVideoEventCountValid } from '../../helpers'
11import { addMethodsToModel } from '../utils' 11import { addMethodsToModel } from '../utils'
12import { 12import {
13 RequestVideoEventClass,
14 RequestVideoEventInstance, 13 RequestVideoEventInstance,
15 RequestVideoEventAttributes, 14 RequestVideoEventAttributes,
16 15
@@ -77,23 +76,21 @@ function associate (models) {
77 }) 76 })
78} 77}
79 78
80countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) { 79countTotalRequests = function () {
81 const query = {} 80 const query = {}
82 return RequestVideoEvent.count(query).asCallback(callback) 81 return RequestVideoEvent.count(query)
83} 82}
84 83
85listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) { 84listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
86 const Pod = db.Pod 85 const Pod = db.Pod
87 86
88 // We make a join between videos and authors to find the podId of our video event requests 87 // We make a join between videos and authors to find the podId of our video event requests
89 const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' + 88 const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
90 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"' 89 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
91 90
92 Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) { 91 return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
93 if (err) return callback(err)
94
95 // We don't have friends that have requests 92 // We don't have friends that have requests
96 if (podIds.length === 0) return callback(null, []) 93 if (podIds.length === 0) return []
97 94
98 const query = { 95 const query = {
99 order: [ 96 order: [
@@ -121,16 +118,14 @@ listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: numbe
121 ] 118 ]
122 } 119 }
123 120
124 RequestVideoEvent.findAll(query).asCallback(function (err, requests) { 121 return RequestVideoEvent.findAll(query).then(requests => {
125 if (err) return callback(err)
126
127 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) 122 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
128 return callback(err, requestsGrouped) 123 return requestsGrouped
129 }) 124 })
130 }) 125 })
131} 126}
132 127
133removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoEventMethods.RemoveByRequestIdsAndPodCallback) { 128removeByRequestIdsAndPod = function (ids: number[], podId: number) {
134 const query = { 129 const query = {
135 where: { 130 where: {
136 id: { 131 id: {
@@ -152,12 +147,12 @@ removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: Req
152 ] 147 ]
153 } 148 }
154 149
155 RequestVideoEvent.destroy(query).asCallback(callback) 150 return RequestVideoEvent.destroy(query)
156} 151}
157 152
158removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) { 153removeAll = function () {
159 // Delete all requests 154 // Delete all requests
160 RequestVideoEvent.truncate({ cascade: true }).asCallback(callback) 155 return RequestVideoEvent.truncate({ cascade: true })
161} 156}
162 157
163// --------------------------------------------------------------------------- 158// ---------------------------------------------------------------------------
diff --git a/server/models/request/request-video-qadu-interface.ts b/server/models/request/request-video-qadu-interface.ts
index 805771cda..9a172a4d4 100644
--- a/server/models/request/request-video-qadu-interface.ts
+++ b/server/models/request/request-video-qadu-interface.ts
@@ -1,5 +1,7 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import * as Promise from 'bluebird'
2 3
4import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface'
3import { VideoInstance } from '../video' 5import { VideoInstance } from '../video'
4import { PodInstance } from '../pod' 6import { PodInstance } from '../pod'
5 7
@@ -14,20 +16,16 @@ export type RequestsVideoQaduGrouped = {
14} 16}
15 17
16export namespace RequestVideoQaduMethods { 18export namespace RequestVideoQaduMethods {
17 export type CountTotalRequestsCallback = (err: Error, total: number) => void 19 export type CountTotalRequests = () => Promise<number>
18 export type CountTotalRequests = (callback: CountTotalRequestsCallback) => void
19 20
20 export type ListWithLimitAndRandomCallback = (err: Error, requestsGrouped?: RequestsVideoQaduGrouped) => void 21 export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number) => Promise<RequestsVideoQaduGrouped>
21 export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number, callback: ListWithLimitAndRandomCallback) => void
22 22
23 export type RemoveByRequestIdsAndPodCallback = () => void 23 export type RemoveByRequestIdsAndPod = (ids: number[], podId: number) => Promise<number>
24 export type RemoveByRequestIdsAndPod = (ids: number[], podId: number, callback: RemoveByRequestIdsAndPodCallback) => void
25 24
26 export type RemoveAllCallback = () => void 25 export type RemoveAll = () => Promise<void>
27 export type RemoveAll = (callback: RemoveAllCallback) => void
28} 26}
29 27
30export interface RequestVideoQaduClass { 28export interface RequestVideoQaduClass extends AbstractRequestClass<RequestsVideoQaduGrouped>, AbstractRequestToPodClass {
31 countTotalRequests: RequestVideoQaduMethods.CountTotalRequests 29 countTotalRequests: RequestVideoQaduMethods.CountTotalRequests
32 listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom 30 listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom
33 removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod 31 removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod
@@ -38,11 +36,13 @@ export interface RequestVideoQaduAttributes {
38 type: RequestVideoQaduType 36 type: RequestVideoQaduType
39} 37}
40 38
41export interface RequestVideoQaduInstance extends RequestVideoQaduClass, RequestVideoQaduAttributes, Sequelize.Instance<RequestVideoQaduAttributes> { 39export interface RequestVideoQaduInstance
40 extends RequestVideoQaduClass, RequestVideoQaduAttributes, Sequelize.Instance<RequestVideoQaduAttributes> {
42 id: number 41 id: number
43 42
44 Pod: PodInstance 43 Pod: PodInstance
45 Video: VideoInstance 44 Video: VideoInstance
46} 45}
47 46
48export interface RequestVideoQaduModel extends RequestVideoQaduClass, Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> {} 47export interface RequestVideoQaduModel
48 extends RequestVideoQaduClass, Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> {}
diff --git a/server/models/request/request-video-qadu.ts b/server/models/request/request-video-qadu.ts
index da62239f5..74e28f129 100644
--- a/server/models/request/request-video-qadu.ts
+++ b/server/models/request/request-video-qadu.ts
@@ -16,7 +16,6 @@ import { database as db } from '../../initializers/database'
16import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers' 16import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers'
17import { addMethodsToModel } from '../utils' 17import { addMethodsToModel } from '../utils'
18import { 18import {
19 RequestVideoQaduClass,
20 RequestVideoQaduInstance, 19 RequestVideoQaduInstance,
21 RequestVideoQaduAttributes, 20 RequestVideoQaduAttributes,
22 21
@@ -83,20 +82,18 @@ function associate (models) {
83 }) 82 })
84} 83}
85 84
86countTotalRequests = function (callback: RequestVideoQaduMethods.CountTotalRequestsCallback) { 85countTotalRequests = function () {
87 const query = {} 86 const query = {}
88 return RequestVideoQadu.count(query).asCallback(callback) 87 return RequestVideoQadu.count(query)
89} 88}
90 89
91listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoQaduMethods.ListWithLimitAndRandomCallback) { 90listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
92 const Pod = db.Pod 91 const Pod = db.Pod
93 const tableJoin = '' 92 const tableJoin = ''
94 93
95 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin, function (err, podIds) { 94 return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin).then(podIds => {
96 if (err) return callback(err)
97
98 // We don't have friends that have requests 95 // We don't have friends that have requests
99 if (podIds.length === 0) return callback(null, []) 96 if (podIds.length === 0) return []
100 97
101 const query = { 98 const query = {
102 include: [ 99 include: [
@@ -114,16 +111,14 @@ listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: numbe
114 ] 111 ]
115 } 112 }
116 113
117 RequestVideoQadu.findAll(query).asCallback(function (err, requests) { 114 return RequestVideoQadu.findAll(query).then(requests => {
118 if (err) return callback(err)
119
120 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) 115 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
121 return callback(err, requestsGrouped) 116 return requestsGrouped
122 }) 117 })
123 }) 118 })
124} 119}
125 120
126removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoQaduMethods.RemoveByRequestIdsAndPodCallback) { 121removeByRequestIdsAndPod = function (ids: number[], podId: number) {
127 const query = { 122 const query = {
128 where: { 123 where: {
129 id: { 124 id: {
@@ -133,12 +128,12 @@ removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: Req
133 } 128 }
134 } 129 }
135 130
136 RequestVideoQadu.destroy(query).asCallback(callback) 131 return RequestVideoQadu.destroy(query)
137} 132}
138 133
139removeAll = function (callback: RequestVideoQaduMethods.RemoveAllCallback) { 134removeAll = function () {
140 // Delete all requests 135 // Delete all requests
141 RequestVideoQadu.truncate({ cascade: true }).asCallback(callback) 136 return RequestVideoQadu.truncate({ cascade: true })
142} 137}
143 138
144// --------------------------------------------------------------------------- 139// ---------------------------------------------------------------------------
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// ---------------------------------------------------------------------------