-
Notifications
You must be signed in to change notification settings - Fork 44
/
subscription.controller.js
77 lines (65 loc) · 2.64 KB
/
subscription.controller.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
class SubscriptionController {
constructor($rootScope, $location, openmrsRest, openmrsNotification) {
"ngInject"
$rootScope.links = {};
$rootScope.links["Open Concept Lab"] = "";
$rootScope.links["Subscription"] = "subscription";
const NUMBER_OF_SLASHES_AFTER_BASE_URL = 5;
var vm = this;
vm.cancel = cancel;
vm.subscribe = subscribe;
vm.unSubscribe = unSubscribe;
vm.isVersionAdded = isVersionAdded;
function cancel(){
$location.path('/');
}
function unSubscribe() {
openmrsRest.retire("openconceptlab/subscription", vm.subscription).then(handleUnSubscribeSuccess, handleUnSubscribeException);
}
function handleUnSubscribeSuccess(success) {
openmrsNotification.success("Unsubscribed successfully");
getSubscription();
}
function handleUnSubscribeException(exception) {
openmrsNotification.error(exception.data.error.message);
}
function subscribe() {
if (angular.isUndefined(vm.subscription.uuid) || vm.subscription.uuid === "") {
openmrsRest.create("openconceptlab/subscription", vm.subscription).then(handleSubscribeSuccess, handleSubscribeException);
} else {
openmrsRest.update("openconceptlab/subscription", vm.subscription).then(handleSubscribeSuccess, handleSubscribeException);
}
}
function handleSubscribeSuccess(success) {
$location.path('/').search({successToast: "Subscription saved"});
}
function handleSubscribeException(exception) {
openmrsNotification.error(exception.data.error.message);
}
function getSubscription() {
openmrsRest.getFull("openconceptlab/subscription").then(function (response) {
vm.subscription = response.results[0];
})
}
/*
*This checks if collection version has been passed to subscription url by checking number of forward slashes after base url
*If the number is 5, such as with https://api.openconceptlab.org/users/username/collections/collectionname/v1.0
*that means collection version was passed and isVersionAdded() will return true
*/
function isVersionAdded(subscriptionUrl) {
if (subscriptionUrl.endsWith("/")) {
subscriptionUrl = subscriptionUrl.substring(0, subscriptionUrl.lastIndexOf('/'));
}
var url = new URL(subscriptionUrl);
let subUrlAfterBaseUrl = url.pathname;
let count = subUrlAfterBaseUrl.length - subUrlAfterBaseUrl.replace(/[\/\\]/g, '').length;
if (count == NUMBER_OF_SLASHES_AFTER_BASE_URL) {
vm.subscription.subscribedToSnapshot = false;
return true;
} else {
return false
}
}
}
}
export default SubscriptionController;