Proyecto en colaboración con OPASO

manage-personnel.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Authors : Carlos C. Corrada-Bravo
  2. David J. Ortiz-Rivera
  3. Organization : Centro de Desarrollo y Consultoria Computacional
  4. Project : OPASO Material Registry
  5. File : manage-personnel.js
  6. Description : Generate personnel table */
  7. /* global values */
  8. var auth = {};
  9. var labs = {};
  10. var access_levels = {};
  11. var current_person_id;
  12. var next;
  13. var current_lab_id;
  14. /* set listeners when document is ready */
  15. $(document).ready(fetch_personnel);
  16. /* display_form(type: string,transaction: string) - configure/display form */
  17. function display_form(type,transaction){
  18. let form = `.${type}-form`;
  19. let submit = `#${type}-submit`;
  20. $(".form-shader").addClass("fade-in");
  21. $(form).addClass("c-form");
  22. $(form).addClass("slide-down");
  23. $(submit).attr("value",transaction);
  24. }
  25. /* hideForm(clear: bool) - hide/clear form */
  26. function hide_form(clear){
  27. $(".c-form").removeClass("slide-down");
  28. $(".c-form").removeClass("c-form");
  29. /* wait for animation */
  30. setTimeout(function(){
  31. $(".form-shader").removeClass("fade-in");
  32. $(".laboratories").html("");
  33. if(clear){
  34. $(".ap-field").val("");
  35. }
  36. },250);
  37. return false;
  38. }
  39. /* fetch_access_level(action) - extract user's acess level for lab */
  40. function fetch_access_level(action){
  41. /* extract access level */
  42. let lab_id = $(".laboratories").val();
  43. if(current_person_id in access_levels){
  44. if(lab_id in access_levels[current_person_id]){
  45. var access_level = access_levels[current_person_id][lab_id]["access_level"];
  46. }
  47. else{
  48. var access_level = "none";
  49. }
  50. }
  51. else{
  52. var access_level = "none";
  53. }
  54. /* restrict options */
  55. if(action === "restrict" && access_level === "technician"){
  56. $("option[value=investigator]").attr("disabled",true);
  57. }
  58. else{
  59. $("option[value=investigator]").attr("disabled",false);
  60. }
  61. if(action === "authorize" && access_level === "investigator"){
  62. $("option[value=technician]").attr("disabled",true);
  63. }
  64. else{
  65. $("option[value=technician]").attr("disabled",false);
  66. }
  67. $(".access_level").val(access_level);
  68. }
  69. /* action - handle material transactions */
  70. function action(){
  71. /* extract args */
  72. let action = $(this).attr("value");
  73. let r = $(this).parent().parent().attr("id");
  74. let s = `#${r}`;
  75. var restrict = true;
  76. let name = $(s).find("td[value=person_name]").text();
  77. current_person_id = $(s).find("td[value=person_name]").attr("person_id");
  78. /* set user info */
  79. $(".person").text(name);
  80. $(".main-header").text(`${action} user`);
  81. /* authorize */
  82. if(action === "authorize"){
  83. for(var lab in labs){
  84. var option = element_gen("option",{class: "option",text: labs[lab]["lab_room"],value: labs[lab]["lab_id"]});
  85. $(".laboratories").append(option);
  86. }
  87. $("option[value=none]").attr("disabled",true);
  88. }
  89. /* restrict */
  90. else{
  91. if(current_person_id in access_levels){
  92. var access_level = access_levels[current_person_id];
  93. for(var l in labs){
  94. for(var lab_id in access_level){
  95. if(labs[l]["lab_id"] == lab_id){
  96. var option = element_gen("option",{class: "option",text: access_level[lab_id]["lab_room"],value: lab_id});
  97. $(".laboratories").append(option);
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. /* no authorized labs */
  104. else{
  105. restrict = false;
  106. }
  107. $("option[value=none]").attr("disabled",false);
  108. }
  109. /* user is being authorized/has authorized labs */
  110. if(restrict){
  111. fetch_access_level(action);
  112. display_form("main",action);
  113. }
  114. else{
  115. set_alert("warning","No authorized labs found");
  116. }
  117. }
  118. /* manage_personnel() - authorize/restrict labs for a user */
  119. function manage_personnel(){
  120. let action = $(this).attr("value");
  121. let access_level = $(".access_level").val();
  122. current_lab_id = $(".laboratories").val();
  123. let lab_room = $(".laboratories option:selected").text();
  124. /* validate and submit */
  125. if(is_valid(current_lab_id) && is_valid(access_level) && is_valid(action) && is_valid(current_person_id)){
  126. $.post("/scripts/opaso",{query: 16,lab_id: current_lab_id,access_level: access_level,action: action,person_id: current_person_id},function(data){
  127. // console.log(data);
  128. /* extract response */
  129. let response = JSON.parse(data);
  130. let status = response["status"];
  131. /* handle by status */
  132. switch(status){
  133. case "expired": /* on expired, redirect to index */
  134. window.location.href = "/?error=session_expired";
  135. break;
  136. case "success": /* on success, update access level */
  137. /* determine authorized labs/highest access level */
  138. if(current_person_id in access_levels){
  139. var authorized = [];
  140. var highest_access_level = "technician";
  141. if(action === "restrict" && access_level === "none"){
  142. delete access_levels[current_person_id][current_lab_id];
  143. }
  144. else{
  145. access_levels[current_person_id][current_lab_id] = {lab_room: lab_room,access_level: access_level};
  146. }
  147. for(var lab_id in access_levels[current_person_id]){
  148. authorized.push(access_levels[current_person_id][lab_id]["lab_room"]);
  149. if(access_levels[current_person_id][lab_id]["access_level"] === "investigator"){
  150. highest_access_level = "investigator";
  151. }
  152. }
  153. if(!authorized.length){
  154. authorized = "-";
  155. }
  156. else{
  157. authorized = authorized.join(", ");
  158. }
  159. }
  160. /* no authorized labs */
  161. else{
  162. var authorized = lab_room;
  163. var highest_access_level = access_level;
  164. access_levels[current_person_id] = {};
  165. access_levels[current_person_id][current_lab_id] = {lab_room: lab_room,access_level: access_level};
  166. }
  167. $(`td[person_id=${current_person_id}]`).parent().find("td[value=authorized]").text(authorized);
  168. $(`td[person_id=${current_person_id}]`).parent().find("td[value=access_level]").text(highest_access_level);
  169. hide_form();
  170. default: /* on success/error, display message */
  171. /* display alert */
  172. set_alert(status,response["message"]);
  173. break;
  174. }
  175. });
  176. }
  177. /* missing args */
  178. else{
  179. set_alert("error","One or more missing args");
  180. }
  181. }
  182. /* set_listeners() - listen for events */
  183. function set_listeners(){
  184. /* authorize/restrict access */
  185. $("#main-submit").click(manage_personnel);
  186. /* register user */
  187. $("#add-submit").click(register_user);
  188. /* fetch access level for lab */
  189. $(".laboratories").change(function(){
  190. let action = $("#main-submit").attr("value");
  191. fetch_access_level(action);
  192. });
  193. /* handle actions */
  194. $("tbody").on("click",".action",action);
  195. /* add material to inventory */
  196. $("#add").click(function(){
  197. display_form("add","add-material");
  198. });
  199. /* close form */
  200. $(".close-form").click(function(event){
  201. if(event.target === event.currentTarget){
  202. hide_form(false);
  203. }
  204. });
  205. }
  206. /* table_gen(data: dictionary) - generate table body */
  207. function table_gen(data){
  208. for(var d in data){
  209. /* generate person row */
  210. let person_id = data[d]["person_id"];
  211. let tr = element_gen("tr",{id: d});
  212. let person = element_gen("td",{person_id: person_id,text: data[d]["person_name"],value: "person_name"});
  213. /* determine authorized labs/highest access level */
  214. var authorized = [];
  215. let highest_access_level = "technician";
  216. if(person_id in access_levels){
  217. for(var lab_id in access_levels[person_id]){
  218. authorized.push(access_levels[person_id][lab_id]["lab_room"]);
  219. if(access_levels[person_id][lab_id]["access_level"] === "investigator"){
  220. highest_access_level = "investigator";
  221. }
  222. }
  223. }
  224. if(!authorized.length){
  225. authorized = "-";
  226. }
  227. else{
  228. authorized = authorized.join(", ");
  229. }
  230. let labs = element_gen("td",{text: authorized,value: "authorized"});
  231. let access_level = element_gen("td",{text: highest_access_level,value: "access_level"});
  232. tr.append(person,labs,access_level);
  233. /* generate actions */
  234. let actions = {authorize:{class: "t-icon material-icons",icon: "add"},restrict: {class: "t-icon material-icons",icon: "remove"}};
  235. var actions_cell = element_gen("td",{class: "actions-cell"});
  236. for(var action in actions){
  237. var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
  238. var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
  239. actions_cell.append(button);
  240. }
  241. /* append */
  242. tr.append(actions_cell);
  243. $("tbody").append(tr);
  244. }
  245. $(".processing").hide();
  246. setTimeout(function(){
  247. set_listeners();
  248. $(".main-wrapper").show();
  249. },250);
  250. }
  251. /* fetch_personnel() - fetch personnel list */
  252. function fetch_personnel(){
  253. $.post("/scripts/opaso",{query: 15},function(data){
  254. // console.log(data);
  255. /* extract reponse */
  256. let response = JSON.parse(data);
  257. let status = response["status"];
  258. /* handle by status */
  259. switch(status){
  260. case "success": /* on success, generate table */
  261. /* extract args */
  262. let personnel = response["personnel"];
  263. auth = response["authorized"];
  264. access_levels = response["access_levels"];
  265. labs = response["labs"];
  266. next = Object.keys(personnel).length;
  267. /* generate personnel table */
  268. table_gen(personnel);
  269. $(".table-total").text(`Total: ${next}`);
  270. break;
  271. case "expired": /* on expired, redirect to index */
  272. window.location.href = "/?error=session_expired";
  273. break;
  274. default: /* on error, display message */
  275. set_alert(status,response["message"]);
  276. break;
  277. }
  278. });
  279. }
  280. /* register_user() - validate input and register user */
  281. function register_user(){
  282. /* extract args */
  283. let email = $("#email").val();
  284. let person_name = $("#person_name").val();
  285. var phone_number = $("#phone_number").val();
  286. /* validate user data */
  287. if(is_valid(person_name,"text") && is_valid(email,"email")){
  288. /* validate phone number */
  289. if(!is_valid(phone_number,"number")){
  290. phone_number = "n/a"
  291. }
  292. /* reset fields */
  293. $(".invalid-feedback").hide();
  294. $(".invalid").removeClass("invalid");
  295. /* query server */
  296. $.post("/scripts/opaso",{query: 0,person_name: person_name,email: email,phone_number: phone_number},function(data){
  297. // console.log(data);
  298. /* extract response */
  299. let response = JSON.parse(data);
  300. let status = response["status"];
  301. /* handle by status */
  302. switch(status){
  303. case "expired": /* on expired, redirect to index */
  304. window.location.href = "/?error=session_expired";
  305. break;
  306. case "success": /* on success, add user */
  307. /* extract person id */
  308. let person_id = response["person_id"];
  309. /* generate table row */
  310. let tr = element_gen("tr",{class: "personnel-row",id: next});
  311. let person = element_gen("td",{class: "personnell-cell",person_id: person_id,text: person_name,value: "person_name"});
  312. let labs = element_gen("td",{class: "personnell-cell",text: "-",value: "authorized"});
  313. let access_level = element_gen("td",{class: "personnell-cell",text: "technician",value: "access_level"});
  314. tr.append(person,labs,access_level);
  315. /* generate actions */
  316. let actions = {authorize:{class: "t-icon material-icons",icon: "add"},restrict: {class: "t-icon material-icons",icon: "remove"}};
  317. var actions_cell = element_gen("td",{class: "actions-cell"});
  318. for(var action in actions){
  319. var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
  320. var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
  321. actions_cell.append(button);
  322. }
  323. /* append */
  324. tr.append(actions_cell);
  325. $("tbody").append(tr);
  326. /* sort */
  327. next += 1;
  328. full_sort(next,"person_name");
  329. $(".table-total").text(`Total: ${next}`);
  330. hide_form(true);
  331. default: /* on success/error, display message */
  332. set_alert(status,response["message"]);
  333. break;
  334. }
  335. });
  336. }
  337. /* invalid user info */
  338. else{
  339. $(".required").each(function(){
  340. /* extract id */
  341. var id = $(this).attr("id");
  342. var s = `.${id}`;
  343. /* highlight */
  344. if(!is_valid($(this).val(),id)){
  345. $(s).show();
  346. $(this).addClass("invalid");
  347. }
  348. /* unhighlight */
  349. else{
  350. $(s).hide();
  351. $(this).removeClass("invalid");
  352. }
  353. });
  354. set_alert("warning","Please correct highlighted fields");
  355. }
  356. }