1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
4 import { database as db } from '../initializers/database'
5 import { REQUEST_ENDPOINTS } from '../initializers'
6 import { addMethodsToModel } from './utils'
14 } from './request-interface'
16 let Request: Sequelize.Model<RequestInstance, RequestAttributes>
17 let countTotalRequests: RequestMethods.CountTotalRequests
18 let listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom
19 let removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo
20 let removeAll: RequestMethods.RemoveAll
22 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
23 Request = sequelize.define<RequestInstance, RequestAttributes>('Request',
30 type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)),
36 const classMethods = [
39 listWithLimitAndRandom,
45 addMethodsToModel(Request, classMethods)
50 // ------------------------------ STATICS ------------------------------
52 function associate (models) {
53 Request.belongsToMany(models.Pod, {
58 through: models.RequestToPod,
63 countTotalRequests = function (callback: RequestMethods.CountTotalRequestsCallback) {
64 // 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
67 include: [ Request['sequelize'].models.Pod ]
70 return Request.count(query).asCallback(callback)
73 listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestMethods.ListWithLimitAndRandomCallback) {
77 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', '', function (err, podIds) {
78 if (err) return callback(err)
80 // We don't have friends that have requests
81 if (podIds.length === 0) return callback(null, [])
83 // The first x requests of these pods
84 // It is very important to sort by id ASC to keep the requests order!
91 model: Request['sequelize'].models.Pod,
101 Request.findAll(query).asCallback(function (err, requests) {
102 if (err) return callback(err)
104 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
105 return callback(err, requestsGrouped)
110 removeAll = function (callback: RequestMethods.RemoveAllCallback) {
111 // Delete all requests
112 Request.truncate({ cascade: true }).asCallback(callback)
115 removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallback) {
116 if (!callback) callback = function () { /* empty */ }
122 Sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
128 Request.destroy(query).asCallback(callback)
131 // ---------------------------------------------------------------------------
133 function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) {
134 const requestsGrouped: RequestsGrouped = {}
136 requests.forEach(function (request) {
137 request.Pods.forEach(function (pod) {
138 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
140 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
141 requestsGrouped[pod.id].push({
149 return requestsGrouped