-
Notifications
You must be signed in to change notification settings - Fork 19
/
consortia-service.js
84 lines (74 loc) · 1.98 KB
/
consortia-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
/**
* @module consortia-service
*/
const common = require('coinstac-common');
const uuid = require('uuid');
const toArray = require('lodash/toArray');
const Consortium = common.models.Consortium;
const ModelService = require('../model-service');
class ConsortiaService extends ModelService {
constructor(opts) {
super(opts);
this.auth = opts.client.auth;
/* istanbul ignore next */
if (!this.auth) {
throw new ReferenceError('auth instance');
}
}
modelServiceHooks() { // eslint-disable-line class-methods-use-this
return {
dbName: 'consortia',
ModelType: Consortium,
};
}
/**
* Get a consortium's active run ID.
*
* @param {string} consortiumId
* @returns {Promise} Resolves to the run ID string
*/
getActiveRunId(consortiumId) {
return this.client.dbRegistry.get(`remote-consortium-${consortiumId}`)
.find({
selector: {
complete: false,
},
})
.then((docs) => {
return docs.length ? docs[0]._id : undefined;
});
}
/**
* Get consortia that a user has joined.
*
* @param {string} username
* @returns {Promise}
*/
getUserConsortia(username) {
/* istanbul ignore if */
if (typeof username !== 'string') {
throw new TypeError('expected string username');
}
return this.all()
.then((consortia) => { // eslint-disable-line
return consortia.filter((consortium) => { // eslint-disable-line
// users is arr of [ usernames ]. :/
return consortium.users.some(uname => (uname === username));
});
});
}
/**
* proxy ModelService.save call and assert valid consortium `_id`s are added
* @see ModelService.save
*/
save() {
const args = toArray(arguments); // eslint-disable-line
const tium = args[0];
if (!tium._id) {
tium._id = uuid.v4().replace(/-/ig, '');
}
return ModelService.prototype.save.apply(this, args);
}
}
module.exports = ConsortiaService;