(function () {
'use strict';
var refreshToken = false;
var app = angular.module('dataCollectorApp', ['chart.js']);
app.controller('dataCollectorCtrl', ['$scope', '$http', '$timeout', '$interval', '$sce',
function($scope, $http, $timeout, $interval, $sce) {
let tmpTimeout;
$scope.data = {};
$scope.data.refresh = {};
$scope.data.refresh.districts = [];
$scope.api_url = "https://cc-api-qa.metasolutions.net/";
$scope.reports = [];
$scope.debug = false;
/* Verify Authorization */
// let auth = getCookie("access_token");
// console.log("auth", auth)
// if (!auth) location.href = $scope.logoff();
// $http.get(`${$scope.api_url}verify`).then(function(response) {
// console.log("GET success response", response);
// }, function(response){
// console.log("GET verify error", response);
// if (response.data.message == "Token expired") {
//console.log("old access token", getCookie("access_token"));
refreshToken = true;
$http.post(`${$scope.api_url}refresh`).then(function(response) {
refreshToken = false;
//if ($scope.debug) console.log("Set new tokens!", response);
setCookie("access_token", response.data.access_token);
setCookie("refresh_token", response.data.refresh_token);
//if ($scope.debug) console.log("new access token", getCookie("access_token"));
$scope.init();
// auth = response.data.access_token;
}, function(response){
$scope.logoff();
});
//} else {
// $scope.logoff();
//}
//});
/* Set up defaults */
$scope.data.selectedMenu = "cd";
$scope.data.username = getCookie("username");
$scope.data.selected = {};
$scope.data.display_name = getCookie("display_name");
if (!$scope.data.display_name) $scope.data.display_name = $scope.data.username;
// need to verify that this works similarly to display_name
$scope.data.initials = getCookie("initials");
if (!$scope.data.initials) $scope.data.initials = $scope.data.username;
//
$scope.data.welcomeMessage = "Please select a report from the menu to continue.";
$scope.data.menu = [];
$scope.data.adminAccess = false;
if ( getCookie("crosscheck") == 1 ) $scope.data.adminAccess = true;
$scope.data.importing = false;
$scope.data.beta_users = ["coreyhilliard", "ccadmin-ekubiak"];
$scope.data.betauser = true;//$scope.data.beta_users.includes($scope.data.username);
$scope.data.sync = {"msg":"Waiting to initiate Synchronization..."};
$scope.data.timeout = 0;
// Year selection for reports will grow by 1 each year
$scope.data.years = [{"year" : "2021"},{"year" : "2022"},{"year" : "2023"}, {"year": "2024"}, {"year:": "2025"}, {"year": "2026"}];
// set default year to current year - will need to be updated each year
$scope.data.selectedYear = {"year" : "2025"};
var syncPromise;
/* set blurStudentData */
let b = getCookie("blur");
$scope.data.blur = b==1?true:false;
// set scope for district names (used during blur)
$scope.hideDistrictName = false;
$scope.init = function() {
$http.get(`${$scope.api_url}cc/access`).then(function(response) {
$scope.data.connections = response.data;
if (response.data.orgs.length==1) $scope.data.selected.org = response.data.orgs[0];
if ($scope.data.selected?.org?.datacollectors.length==1) $scope.data.selected.dc = $scope.data.selected.org.datacollectors[0];
if ($scope.data.selected?.dc?.districts.length==1) {
$scope.data.selected.dist = $scope.data.selected.dc.districts[0];
$scope.selectDistrict($scope.data.selected.dist);
}
//if ($scope.debug) console.log("data.connections", $scope.data.connections);
/* Preload Org */
if (!$scope.data.selected.org && getCookie("setOrg")) $scope.data.selected.org = response.data.orgs.find(function(value) { if (value.name == getCookie("setOrg")) return true; });
if (!$scope.data.selected.dc && getCookie("setDC")) $scope.data.selected.dc = $scope.data.selected.org.datacollectors.find(function(value) { if (value.name == getCookie("setDC")) return true; });
if (!$scope.data.selected.dist && getCookie("setDist")) {
$scope.data.selected.dist = $scope.data.selected.dc.districts.find(function(value) { if (value.irn == getCookie("setDist")) return true; });
$scope.selectDistrict($scope.data.selected.dist);
}
});
$scope.loadData();
}
/* Manually created Welcome Back Modal */
// if (!$scope.data.year && $scope.data.selectedDistrict) {
// jQuery("#welcomeBackModal").modal({backdrop: 'static', keyboard: false});
// }
$scope.reSync = function() {
//if ($scope.debug) console.log("reSync()");
$scope.data.selectedMenu = "m1";
$scope.data.sync.results = {};
$scope.data.sync.hasRan = false;
$scope.autoSync() || $scope.adminSync();
}
/* Auto Sync */
$scope.autoSync = function() {
//if ($scope.debug) console.log("autoSync()");
$scope.feathers();
if ($scope.data.adminAccess) return;
if ($scope.data.sync.hasRan) {
//if ($scope.debug) console.log("Already running a sync, exiting");
return true;
}
// store the interval promise
$scope.data.sync.msg = "Synchronizing...";
$scope.data.sync.status = true;
$scope.data.sync.hasRan = true;
$scope.data.sync.seconds = 0;
syncPromise = $interval($scope.setSyncTimer, 1000);
$http.post(`${$scope.api_url}cc/sync/${$scope.data.selected.dist.irn}`).then(function(response) {
//if ($scope.debug) console.log("autoSync()", response);
$scope.data.sync.msg = "Synchronization Complete";
$scope.data.sync.status = false;
$interval.cancel(syncPromise);
if (response.data.message) {
$scope.data.sync.results = {"key":"", "table":"", "success":response.data.success, "message": response.data.message};
} else {
$scope.data.sync.results = response.data.records;
}
if ($scope.data.sync.results.length > 0) $scope.initDistricts();
$scope.feathers();
},
function(response) { // optional
//if ($scope.debug) console.log("autoSync()", "error");
$scope.data.sync.msg = "Synchronization Error";
$scope.data.sync.status = false;
$interval.cancel(syncPromise);
$scope.data.sync.results = {"key":"", "table":"", "success":false, "message": response};
});
}
/* Admin Sync */
$scope.adminSync = function() {
//if ($scope.debug) console.log("adminSync()");
$scope.feathers();
if (!$scope.data.adminAccess) return;
if ($scope.data.sync.hasRan) {
//if ($scope.debug) console.log("Already running a sync, exiting");
return true;
}
// store the interval promise
$scope.data.sync.msg = "Synchronizing...";
$scope.data.sync.status = true;
$scope.data.sync.hasRan = true;
$scope.data.sync.seconds = 0;
syncPromise = $interval($scope.setSyncTimer, 1000);
$http.post(`${$scope.api_url}cc/sync/${$scope.data.selected.dist.irn}`).then(function(response) {
//if ($scope.debug) console.log("autoSync()", response);
$scope.data.sync.msg = "Sync Complete: CrossCheckAdmin users must run a manual sync for each new district";
$scope.data.sync.status = false;
$interval.cancel(syncPromise);
if (response.data.message) {
$scope.data.sync.results = {"key":"", "table":"", "success":response.data.success, "message": response.data.message};
} else {
$scope.data.sync.results = response.data.records;
}
if ($scope.data.sync.results.length > 0) $scope.initDistricts();
$scope.feathers();
},
function(response) { // optional
//if ($scope.debug) console.log("autoSync()", "error");
$scope.data.sync.msg = "Synchronization Error";
$scope.data.sync.status = false;
$interval.cancel(syncPromise);
$scope.data.sync.results = {"key":"", "table":"", "success":false, "message": response};
});
}
/* Check if user has been logged off due to inactivity */
$scope.updateTime = function() {
$scope.data.timeout++;
if ($scope.data.timeout >= 18) {
let auth = getCookie("access_token");
refreshToken = true;
$http.post(`${$scope.api_url}refresh`).then(function(response) {
refreshToken = false;
setCookie("access_token", response.data.access_token);
setCookie("refresh_token", response.data.refresh_token);
//if ($scope.debug) console.log("new access token", getCookie("access_token"));
auth = response.data.access_token;
$scope.data.timeout = 0;
}, function(response){
$scope.logoff();
alert("session expired, troubleshooting auth tokens. Please use /login to Login to CrossCheck QA")
});
}
}
/* Setup 1 minute interval */
var checkMinute;
checkMinute = $interval($scope.updateTime, 60000);
$scope.updateTime();
/*$scope.loadData = function(setWelcome = true) {
if (setWelcome) $scope.data.selectedMenu = "m1";
/* Get Menu and Reports List */
//let admin = $scope.data?.selected?.org?.itc_admin;
//let all = admin ? "/all" : "/all";
/* $http.get(`${$scope.api_url}cc/menu`).then(function(response) {
$scope.data.menu = response.data;
/* When refreshing, enable the loading indiator for each specific report
$scope.data.menu.forEach(function(menu){
if ($scope.data.selectedMenu && `m${menu.menu_id}` == $scope.data.selectedMenu) {
$scope.data.selectedMenu2 = menu;
}
if (menu.reports) {
menu.active = true;
for (var r=0;r
-1) err = err.substring(1,sql_location);
// $scope.import_error += `${property} - ${ data[property].error}
`;
// if (err) $scope.import_error += `Details: ${err}`;
// //$scope.import_error += ": " + jQuery('#fileToUpload').val().split('\\').pop() + "
";
// } else {
// $scope.import_success += `${property} - ${data[property].message}
`;
$scope.getLog();
// }
//}
},
error: function(data) {
if (data.responseText) {
$scope.import_error = data.responseText;
} else {
$scope.import_error = "Upload Failed";
}
$scope.import_success = "";
$scope.data.importing = false;
$scope.$apply();
}
}).done(function() {
$scope.data.importing = false;
$scope.$apply();
angular.element('#dataCollectorCtrl').scope().loadData(false);
jQuery("#fileToUpload").val('');
});
}
$scope.runReport = function(report, select) {
let dist = $scope.data.selected.dist;
$http.post(`${$scope.api_url}cc/run/${dist.irn}/report/${report.report_id}/year/${$scope.data.selectedYear.year}`).then(function(response) {
$scope.reports[response.data.report.report_id] = response.data;
report.loading = false;
$scope.reports[response.data.report.report_id].sort1 = 'reviewed.initreview';
$scope.reports[report.report_id].pk = [];
if (response.data.data)
response.data?.data.forEach(function(row) {
if (row.reviewed) {
if (row.reviewed.reviewed) {
row.reviewed.initreview = true;
} else {
row.reviewed.initreview = false;
}
}
})
if (response.data.report && response.data.report.columns)
response.data?.report?.columns.forEach(function(col) {
if (col.is_pk > 0) $scope.reports[report.report_id].pk.push(col);
})
if (select) {
$scope.tableData = $scope.reports[report.report_id];
$scope.feathers();
}
},
function(response) {
report.loading = false;
$scope.reports[response.data.report_id] = response.data;
});
}
$scope.initDistricts = function() {
//console.log($scope.data.selectedYear);
/* Loop through each menu, running each report to load the latest results */
let dist = $scope.data.selected.dist;
//if ($scope.debug) console.log("selectedDistrict", dist)
if (!dist) return false;
$http.get(`${$scope.api_url}cc/dataset/district/${$scope.data.selected.dist.irn}`).then(function(response) {
$scope.data.sources = response.data;
$scope.feathers();
});
$http.get(`${$scope.api_url}cc/check_role/${$scope.data.selected.dist.irn}`).then(function(response) {
$scope.data.user_role = response.data;
});
$http.get(`${$scope.api_url}cc/users/${$scope.data.selected.dist.irn}`).then(function(response) {
//if ($scope.debug) console.log("users", response.data);
$scope.data.users = response.data;
if (!$scope.data.users.ad) {
let expirationDate = new Date($scope.data.users.users[0].password_expiration);
if (expirationDate < new Date()) {
$scope.data.expiration = true;
$scope.displayUpdatePasswordModal();
} else {
$scope.populateReports();
}
} else {
$scope.populateReports();
}
$scope.feathers();
});
}
$scope.populateReports = function() {
$scope.reports = [];
$scope.data.menu.forEach(function(menu){
if (menu.reports)
menu.reports.forEach(function(report){
$scope.runReport(report);
$scope.feathers();
});
});
$scope.feathers();
$timeout(function() { $scope.autoSync(); });
}
/* feathers() - Replace the icon tags with