-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
125 lines (100 loc) · 3.19 KB
/
script.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const $provisionNumber = $('#provision_number');
const $configureWebhook = $('#configure_webhook');
const $apiUsername = $('#api_username');
const $apiPassword = $('#api_password');
const $accountId = $('#account_id');
const $realPhoneNumber = $('#real_phone_number');
const $webhookUrl = $('#webhook_pathname');
const $infoSection = $('#info_section');
const $virtualNumberText = $('#virtual_number_text');
const account = {
virtualNumber: localStorage.getItem('virtual_number') || ''
};
const API_URL = 'https://tadhack-berlin.herokuapp.com';
const internalApi = {
api_url: 'https://markustoepfer.com:8080'
};
$accountId.val(localStorage.getItem('account_id'));
$realPhoneNumber.val(localStorage.getItem('real_phone_number'));
$apiUsername.val(localStorage.getItem('api_username'));
$apiPassword.val(localStorage.getItem('api_password'));
function saveToLocalStorage() {
localStorage.setItem('account_id', getAccId());
localStorage.setItem('real_phone_number', getRealPhoneNumber());
localStorage.setItem('api_username', getApiUsername());
localStorage.setItem('api_password', getApiPassword());
}
function getAccId() {
return $accountId.val();
}
function getRealPhoneNumber() {
return $realPhoneNumber.val();
}
function getApiUsername() {
return $apiUsername.val();
}
function getApiPassword() {
return $apiPassword.val();
}
function getAPIHeaders() {
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(getApiUsername() + ":" + getApiPassword())
};
}
function getWebhookPathnameAbsUrl() {
return new URL($webhookUrl.val(), getApiConfig().api_url);
}
function getWebhookPathname() {
const pathname = (new URL(getWebhookPathnameAbsUrl())).pathname;
return pathname;
}
async function talkToInternalApi() {
const apiConfig = getApiConfig();
const options = {
method: 'POST',
headers: getAPIHeaders(),
body: JSON.stringify({
account_id: getAccId(),
real_phone_number: getRealPhoneNumber(),
virtual_number: account.virtualNumber,
webhook_pathname: getWebhookPathname(),
api_username: getApiUsername(),
api_password: getApiPassword()
})
};
await fetch(apiConfig.api_url + '/configure', options);
}
$provisionNumber.on('click', async function() {
saveToLocalStorage();
const accountId = getAccId();
const options = {
method: 'POST',
headers: getAPIHeaders(),
body: JSON.stringify({
'account_id': accountId
})
};
const responseObject = await fetch(`${API_URL}/provision-number`, options);
const response = await responseObject.json();
account.virtualNumber = response.number;
$virtualNumberText.text(account.virtualNumber);
$infoSection.removeClass('hidden');
});
$configureWebhook.on('click', async function() {
const accountId = getAccId();
const options = {
method: 'POST',
headers: getAPIHeaders(),
body: JSON.stringify({
'account_id': accountId,
number: account.virtualNumber,
endpoint: getWebhookPathnameAbsUrl()
})
};
const responseObject = await fetch(`${API_URL}/webhook`, options);
const response = await responseObject.json();
await talkToInternalApi();
alert('Webhook configured!');
});