chore(eslint): ESLint setup (#6708)
[openemr.git] / interface / modules / custom_modules / oe-module-comlink-telehealth / public / assets / js / src / registration-checker.js
blob978522cd9252061fd5402b0f2e69f06b29d9ba1c
1 /**
2  * Javascript function for checking the registration of the logged in user and registering them for telehealth if they
3  * are not currently registered.
4  *
5  * @package openemr
6  * @link      http://www.open-emr.org
7  * @author    Stephen Nielson <snielson@discoverandchange.com>
8  * @copyright Copyright (c) 2023 Comlink Inc <https://comlinkinc.com/>
9  * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
10  */
11 export function RegistrationChecker(scriptLocation)
13     var checker = this;
14     var timeoutId;
15     var settings;
16     var currentCheckCount = 0;
17     var maxCheck = 10;
19     this.checkRegistration = function()
20     {
21         if (currentCheckCount++ > maxCheck)
22         {
23             console.error("Failed to get a valid telehealth registration for user");
24             return;
25         }
27         let location = scriptLocation + '?action=check_registration';
29         window.top.restoreSession();
30         window.fetch(location)
31             .then(result => {
32                 if (!result.ok)
33                 {
34                     throw new Error("Registration check failed");
35                 }
36                 return result.json();
37             })
38             .then(registrationSettings => {
39                 if (registrationSettings && Object.prototype.hasOwnProperty.call(registrationSettings, 'errorCode')) {
40                     if (registrationSettings.errorCode == 402) {
41                         // user is not enrolled and so we will skip trying to register the user
42                         checker.settings = {};
43                     }
44                 }
45                 checker.settings = registrationSettings;
46             })
47             .catch(error => {
48                 console.error("Failed to execute check_registration action", error);
49                 timeoutId = setTimeout(checker.checkRegistration.bind(checker), 2000);
50             });
51     };
52     return this;