-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmethods.js
More file actions
70 lines (66 loc) · 2.28 KB
/
methods.js
File metadata and controls
70 lines (66 loc) · 2.28 KB
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
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/tap:i18n';
import { ApiKeys } from '/api_keys/collection';
import { ProxyBackends } from '/proxy_backends/collection';
import { Proxies } from '/proxies/collection';
Meteor.methods({
createApiKey (idApi) {
// Get logged in user
const currentUser = Meteor.user();
// Check currentUser exists
if (currentUser) {
const proxyBackend = ProxyBackends.findOne({ apiId: idApi });
// Check proxyBackend is defined, and it has proxyId
if (proxyBackend && proxyBackend.proxyId) {
// Get Proxy by proxyId of proxyBackend
const proxyId = proxyBackend.proxyId;
const proxy = Proxies.findOne({ _id: proxyId });
// Check type & call appropriate function
if (proxy && proxy.type === 'apiUmbrella') {
// Call Umbrella method to create user with API key
Meteor.call('createApiUmbrellaUser', currentUser, proxyId, (error, umbrellaUser) => {
if (error) {
// Log error for server
console.log(error);
// Throw apiumbrellauser error for client
throw new Meteor.Error(
'apinf-apiumbrellauser-error',
TAPi18n.__('apinf_apiumbrellauser_error')
);
} else {
// Construct apiKey object
const apiKey = {
apiUmbrella: {
id: umbrellaUser.id,
apiKey: umbrellaUser.api_key,
},
userId: currentUser._id,
proxyId: proxy._id,
};
// Insert apiKey
ApiKeys.insert(apiKey);
}
});
} else {
// Throw no proxy error for client
throw new Meteor.Error(
'apinf-noproxy-error',
TAPi18n.__('apinf_noproxy_error')
);
}
} else {
// Throw no proxybackend error for client
throw new Meteor.Error(
'apinf-noproxybackend-error',
TAPi18n.__('apinf_noproxybackend_error')
);
}
} else {
// Throw usernotloggedin error for client
throw new Meteor.Error(
'apinf-usernotloggedin-error',
TAPi18n.__('apinf_usernotloggedin_error')
);
}
},
});