]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const map = require('lodash/map') | |
4 | ||
5 | const constants = require('../initializers/constants') | |
6 | ||
7 | // --------------------------------------------------------------------------- | |
8 | ||
9 | module.exports = function (sequelize, DataTypes) { | |
10 | const Pod = sequelize.define('Pod', | |
11 | { | |
12 | host: { | |
13 | type: DataTypes.STRING | |
14 | }, | |
15 | publicKey: { | |
16 | type: DataTypes.STRING(5000) | |
17 | }, | |
18 | score: { | |
19 | type: DataTypes.INTEGER, | |
20 | defaultValue: constants.FRIEND_SCORE.BASE | |
21 | } | |
22 | }, | |
23 | { | |
24 | classMethods: { | |
25 | associate, | |
26 | ||
27 | countAll, | |
28 | incrementScores, | |
29 | list, | |
30 | listAllIds, | |
31 | listBadPods, | |
32 | load, | |
33 | loadByHost, | |
34 | removeAll | |
35 | }, | |
36 | instanceMethods: { | |
37 | toFormatedJSON | |
38 | } | |
39 | } | |
40 | ) | |
41 | ||
42 | return Pod | |
43 | } | |
44 | ||
45 | // TODO: max score -> constants.FRIENDS_SCORE.MAX | |
46 | // TODO: validation | |
47 | // PodSchema.path('host').validate(validator.isURL) | |
48 | // PodSchema.path('publicKey').required(true) | |
49 | // PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | |
50 | ||
51 | // ------------------------------ METHODS ------------------------------ | |
52 | ||
53 | function toFormatedJSON () { | |
54 | const json = { | |
55 | id: this.id, | |
56 | host: this.host, | |
57 | score: this.score, | |
58 | createdAt: this.createdAt | |
59 | } | |
60 | ||
61 | return json | |
62 | } | |
63 | ||
64 | // ------------------------------ Statics ------------------------------ | |
65 | ||
66 | function associate (models) { | |
67 | this.belongsToMany(models.Request, { | |
68 | foreignKey: 'podId', | |
69 | through: models.RequestToPod, | |
70 | onDelete: 'cascade' | |
71 | }) | |
72 | } | |
73 | ||
74 | function countAll (callback) { | |
75 | return this.count().asCallback(callback) | |
76 | } | |
77 | ||
78 | function incrementScores (ids, value, callback) { | |
79 | if (!callback) callback = function () {} | |
80 | ||
81 | const update = { | |
82 | score: this.sequelize.literal('score +' + value) | |
83 | } | |
84 | ||
85 | const query = { | |
86 | where: { | |
87 | id: { | |
88 | $in: ids | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | return this.update(update, query).asCallback(callback) | |
94 | } | |
95 | ||
96 | function list (callback) { | |
97 | return this.findAll().asCallback(callback) | |
98 | } | |
99 | ||
100 | function listAllIds (callback) { | |
101 | const query = { | |
102 | attributes: [ 'id' ] | |
103 | } | |
104 | ||
105 | return this.findAll(query).asCallback(function (err, pods) { | |
106 | if (err) return callback(err) | |
107 | ||
108 | return callback(null, map(pods, 'id')) | |
109 | }) | |
110 | } | |
111 | ||
112 | function listBadPods (callback) { | |
113 | const query = { | |
114 | where: { | |
115 | score: { $lte: 0 } | |
116 | } | |
117 | } | |
118 | ||
119 | return this.findAll(query).asCallback(callback) | |
120 | } | |
121 | ||
122 | function load (id, callback) { | |
123 | return this.findById(id).asCallback(callback) | |
124 | } | |
125 | ||
126 | function loadByHost (host, callback) { | |
127 | const query = { | |
128 | where: { | |
129 | host: host | |
130 | } | |
131 | } | |
132 | ||
133 | return this.findOne(query).asCallback(callback) | |
134 | } | |
135 | ||
136 | function removeAll (callback) { | |
137 | return this.destroy().asCallback(callback) | |
138 | } |