]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
00057e85 | 3 | const map = require('lodash/map') |
9f10b292 | 4 | |
f0f5567b | 5 | const constants = require('../initializers/constants') |
67bf9b96 | 6 | const customPodsValidators = require('../helpers/custom-validators').pods |
9f10b292 C |
7 | |
8 | // --------------------------------------------------------------------------- | |
9 | ||
feb4bdfd C |
10 | module.exports = function (sequelize, DataTypes) { |
11 | const Pod = sequelize.define('Pod', | |
12 | { | |
13 | host: { | |
67bf9b96 C |
14 | type: DataTypes.STRING, |
15 | allowNull: false, | |
16 | validate: { | |
17 | isHost: function (value) { | |
18 | const res = customPodsValidators.isHostValid(value) | |
19 | if (res === false) throw new Error('Host not valid.') | |
20 | } | |
21 | } | |
feb4bdfd C |
22 | }, |
23 | publicKey: { | |
67bf9b96 C |
24 | type: DataTypes.STRING(5000), |
25 | allowNull: false | |
feb4bdfd C |
26 | }, |
27 | score: { | |
28 | type: DataTypes.INTEGER, | |
67bf9b96 C |
29 | defaultValue: constants.FRIEND_SCORE.BASE, |
30 | allowNull: false, | |
31 | validate: { | |
32 | isInt: true, | |
33 | max: constants.FRIEND_SCORE.MAX | |
34 | } | |
feb4bdfd | 35 | } |
feb4bdfd C |
36 | }, |
37 | { | |
319d072e C |
38 | indexes: [ |
39 | { | |
40 | fields: [ 'host' ] | |
41 | }, | |
42 | { | |
43 | fields: [ 'score' ] | |
44 | } | |
45 | ], | |
feb4bdfd C |
46 | classMethods: { |
47 | associate, | |
48 | ||
49 | countAll, | |
50 | incrementScores, | |
51 | list, | |
52 | listAllIds, | |
53 | listBadPods, | |
54 | load, | |
55 | loadByHost, | |
56 | removeAll | |
57 | }, | |
58 | instanceMethods: { | |
59 | toFormatedJSON | |
60 | } | |
61 | } | |
62 | ) | |
63 | ||
64 | return Pod | |
53572423 C |
65 | } |
66 | ||
53572423 C |
67 | // ------------------------------ METHODS ------------------------------ |
68 | ||
69 | function toFormatedJSON () { | |
70 | const json = { | |
feb4bdfd | 71 | id: this.id, |
49abbbbe | 72 | host: this.host, |
53572423 | 73 | score: this.score, |
feb4bdfd | 74 | createdAt: this.createdAt |
53572423 C |
75 | } |
76 | ||
77 | return json | |
78 | } | |
79 | ||
a3ee6fa2 C |
80 | // ------------------------------ Statics ------------------------------ |
81 | ||
feb4bdfd C |
82 | function associate (models) { |
83 | this.belongsToMany(models.Request, { | |
84 | foreignKey: 'podId', | |
85 | through: models.RequestToPod, | |
7920c273 | 86 | onDelete: 'cascade' |
feb4bdfd C |
87 | }) |
88 | } | |
89 | ||
a3ee6fa2 | 90 | function countAll (callback) { |
feb4bdfd | 91 | return this.count().asCallback(callback) |
9f10b292 | 92 | } |
45239549 | 93 | |
9f10b292 C |
94 | function incrementScores (ids, value, callback) { |
95 | if (!callback) callback = function () {} | |
feb4bdfd C |
96 | |
97 | const update = { | |
98 | score: this.sequelize.literal('score +' + value) | |
99 | } | |
100 | ||
67bf9b96 | 101 | const options = { |
feb4bdfd C |
102 | where: { |
103 | id: { | |
104 | $in: ids | |
105 | } | |
67bf9b96 C |
106 | }, |
107 | // In this case score is a literal and not an integer so we do not validate it | |
108 | validate: false | |
feb4bdfd C |
109 | } |
110 | ||
67bf9b96 | 111 | return this.update(update, options).asCallback(callback) |
9f10b292 | 112 | } |
45239549 | 113 | |
9f10b292 | 114 | function list (callback) { |
feb4bdfd | 115 | return this.findAll().asCallback(callback) |
9f10b292 | 116 | } |
8c308c2b | 117 | |
ed04d94f C |
118 | function listAllIds (transaction, callback) { |
119 | if (!callback) { | |
120 | callback = transaction | |
121 | transaction = null | |
122 | } | |
123 | ||
feb4bdfd C |
124 | const query = { |
125 | attributes: [ 'id' ] | |
126 | } | |
127 | ||
ed04d94f C |
128 | if (transaction) query.transaction = transaction |
129 | ||
feb4bdfd | 130 | return this.findAll(query).asCallback(function (err, pods) { |
00057e85 C |
131 | if (err) return callback(err) |
132 | ||
feb4bdfd | 133 | return callback(null, map(pods, 'id')) |
00057e85 | 134 | }) |
528a9efa C |
135 | } |
136 | ||
a3ee6fa2 | 137 | function listBadPods (callback) { |
feb4bdfd C |
138 | const query = { |
139 | where: { | |
140 | score: { $lte: 0 } | |
141 | } | |
142 | } | |
143 | ||
144 | return this.findAll(query).asCallback(callback) | |
9f10b292 | 145 | } |
8c308c2b | 146 | |
a3ee6fa2 | 147 | function load (id, callback) { |
feb4bdfd | 148 | return this.findById(id).asCallback(callback) |
9f10b292 | 149 | } |
c45f7f84 | 150 | |
49abbbbe | 151 | function loadByHost (host, callback) { |
feb4bdfd C |
152 | const query = { |
153 | where: { | |
154 | host: host | |
155 | } | |
156 | } | |
157 | ||
158 | return this.findOne(query).asCallback(callback) | |
9f10b292 | 159 | } |
c45f7f84 | 160 | |
a3ee6fa2 | 161 | function removeAll (callback) { |
feb4bdfd | 162 | return this.destroy().asCallback(callback) |
a3ee6fa2 | 163 | } |