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.ts144
1 files changed, 0 insertions, 144 deletions
diff --git a/server/models/request/request.ts b/server/models/request/request.ts
deleted file mode 100644
index 71118a947..000000000
--- a/server/models/request/request.ts
+++ /dev/null
@@ -1,144 +0,0 @@
1import { values } from 'lodash'
2import * as Sequelize from 'sequelize'
3
4import { database as db } from '../../initializers/database'
5import { REQUEST_ENDPOINTS } from '../../initializers'
6import { addMethodsToModel } from '../utils'
7import {
8 RequestInstance,
9 RequestAttributes,
10
11 RequestMethods,
12 RequestsGrouped
13} from './request-interface'
14
15let Request: Sequelize.Model<RequestInstance, RequestAttributes>
16let countTotalRequests: RequestMethods.CountTotalRequests
17let listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom
18let removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo
19let removeAll: RequestMethods.RemoveAll
20
21export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
22 Request = sequelize.define<RequestInstance, RequestAttributes>('Request',
23 {
24 request: {
25 type: DataTypes.JSON,
26 allowNull: false
27 },
28 endpoint: {
29 type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)),
30 allowNull: false
31 }
32 }
33 )
34
35 const classMethods = [
36 associate,
37
38 listWithLimitAndRandom,
39
40 countTotalRequests,
41 removeAll,
42 removeWithEmptyTo
43 ]
44 addMethodsToModel(Request, classMethods)
45
46 return Request
47}
48
49// ------------------------------ STATICS ------------------------------
50
51function associate (models) {
52 Request.belongsToMany(models.Pod, {
53 foreignKey: {
54 name: 'requestId',
55 allowNull: false
56 },
57 through: models.RequestToPod,
58 onDelete: 'CASCADE'
59 })
60}
61
62countTotalRequests = function () {
63 // 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 const query = {
66 include: [ Request['sequelize'].models.Pod ]
67 }
68
69 return Request.count(query)
70}
71
72listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
73 const Pod = db.Pod
74 const tableJoin = ''
75
76 return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', tableJoin).then(podIds => {
77 // We don't have friends that have requests
78 if (podIds.length === 0) return []
79
80 // The first x requests of these pods
81 // It is very important to sort by id ASC to keep the requests order!
82 const query = {
83 order: [
84 [ 'id', 'ASC' ]
85 ],
86 include: [
87 {
88 model: Request['sequelize'].models.Pod,
89 where: {
90 id: {
91 [Sequelize.Op.in]: podIds
92 }
93 }
94 }
95 ]
96 }
97
98 return Request.findAll(query).then(requests => {
99
100 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
101 return requestsGrouped
102 })
103 })
104}
105
106removeAll = function () {
107 // Delete all requests
108 return Request.truncate({ cascade: true })
109}
110
111removeWithEmptyTo = function () {
112 const query = {
113 where: {
114 id: {
115 [Sequelize.Op.notIn]: [
116 Sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
117 ]
118 }
119 }
120 }
121
122 return Request.destroy(query)
123}
124
125// ---------------------------------------------------------------------------
126
127function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) {
128 const requestsGrouped: RequestsGrouped = {}
129
130 requests.forEach(request => {
131 request.Pods.forEach(pod => {
132 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
133
134 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
135 requestsGrouped[pod.id].push({
136 request,
137 pod
138 })
139 }
140 })
141 })
142
143 return requestsGrouped
144}