Nessuna descrizione

view.customers.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. let users_table = null;
  2. let cases_table = null;
  3. let assets_table = null;
  4. function edit_contact(contact_id, customer_id) {
  5. url = '/manage/customers/' + customer_id + '/contacts/' + contact_id + '/modal' + case_param();
  6. $('#modal_add_contact_content').load(url, function (response, status, xhr) {
  7. if (status !== "success") {
  8. ajax_notify_error(xhr, url);
  9. return false;
  10. }
  11. $('#form_new_contact').on("submit", preventFormDefaultBehaviourOnSubmit);
  12. $('#submit_new_contact').on("click", function () {
  13. const form = $('#form_new_contact').serializeObject();
  14. post_request_api(`/manage/customers/${customer_id}/contacts/${contact_id}/update`, JSON.stringify(form), true)
  15. .done((data) => {
  16. if(notify_auto_api(data)) {
  17. window.location.reload();
  18. }
  19. });
  20. return false;
  21. });
  22. $('#submit_delete_contact').on("click", function () {
  23. post_request_api(`/manage/customers/${customer_id}/contacts/${contact_id}/delete`)
  24. .done((data) => {
  25. if(notify_auto_api(data)) {
  26. window.location.reload();
  27. }
  28. });
  29. return false;
  30. });
  31. });
  32. $('#modal_add_contact').modal({show: true});
  33. }
  34. function add_new_contact(customer_id) {
  35. url = '/manage/customers/' + customer_id + '/contacts/add/modal' + case_param();
  36. $('#modal_add_contact_content').load(url, function (response, status, xhr) {
  37. if (status !== "success") {
  38. ajax_notify_error(xhr, url);
  39. return false;
  40. }
  41. $('#form_new_contact').on("submit", preventFormDefaultBehaviourOnSubmit);
  42. $('#submit_new_contact').on("click", function () {
  43. const form = $('#form_new_contact').serializeObject();
  44. post_request_api(`/manage/customers/${customer_id}/contacts/add`, JSON.stringify(form), true)
  45. .done((data) => {
  46. if(notify_auto_api(data)) {
  47. window.location.reload();
  48. }
  49. });
  50. return false;
  51. })
  52. });
  53. $('#modal_add_contact').modal({show: true});
  54. }
  55. function load_customer_stats(customer_id) {
  56. get_request_api(`/manage/customers/${customer_id}/cases`)
  57. .done((data) => {
  58. if (api_request_failed(data)) {
  59. return false;
  60. }
  61. $('#last_month_cases').text(data.data.stats.cases_last_month);
  62. $('#last_year_cases').text(data.data.stats.cases_last_year);
  63. $('#cases_last_month').text(data.data.stats.cases_last_month);
  64. $('#cases_current_month').text(data.data.stats.cases_current_month);
  65. $('#cases_current_year').text(data.data.stats.cases_current_year);
  66. $('#current_open_cases').text(data.data.stats.open_cases);
  67. $('#cases_total').text(data.data.stats.cases_total);
  68. $('#ratio_year').text(data.data.stats.ratio_year);
  69. $('#average_case_duration').text(data.data.stats.average_case_duration);
  70. if (data.data.stats.ratio_year > 0) {
  71. $('#ratio_year').addClass('text-warning');
  72. $('#ratio_year').html(`+${data.data.stats.ratio_year}% <i class="ml-1 fa fa-chevron-up"></i>`);
  73. } else if (data.data.stats.ratio_year < 0) {
  74. $('#ratio_year').addClass('text-success');
  75. $('#ratio_year').html(`${data.data.stats.ratio_year}% <i class="ml-1 fa fa-chevron-down"></i>`);
  76. }
  77. if (data.data.stats.ratio_month > 0) {
  78. $('#ratio_month').addClass('text-warning');
  79. $('#ratio_month').html(`+${data.data.stats.ratio_month}% <i class="ml-1 fa fa-chevron-up"></i>`);
  80. } else if (data.data.stats.ratio_month < 0) {
  81. $('#ratio_month').addClass('text-success');
  82. $('#ratio_month').html(`${data.data.stats.ratio_month}% <i class="ml-1 fa fa-chevron-down"></i>`);
  83. }
  84. $('#last_year').text(data.data.stats.last_year);
  85. });
  86. }
  87. function refresh_client_users(customer_id) {
  88. get_raw_request_api(`/manage/users/filter?customer_id=${customer_id}`)
  89. .done((data) => {
  90. if (api_request_failed(data)) {
  91. return;
  92. }
  93. users_table.api().clear().rows.add(data.data.users).draw();
  94. })
  95. }
  96. $(document).ready(function() {
  97. let customer_id = $('#customer_id').val();
  98. load_customer_stats(customer_id);
  99. $('#collapse_client_users_view').on('show.bs.collapse', function() {
  100. refresh_client_users(customer_id)
  101. });
  102. users_table = $('#client_users_table').dataTable({
  103. "order": [[ 1, "asc" ]],
  104. "autoWidth": false,
  105. "columns": [
  106. {
  107. "data": "user_id",
  108. "render": function(data, type, row) {
  109. return data;
  110. }
  111. },
  112. {
  113. "data": "user_name",
  114. "render": function(data, type, row) {
  115. return data;
  116. }
  117. },
  118. {
  119. "data": "user_login",
  120. "render": function(data, type, row) {
  121. return data;
  122. }
  123. },
  124. {
  125. "data": "is_service_account",
  126. "render": function(data, type, row) {
  127. return data;
  128. }
  129. }
  130. ]
  131. });
  132. assets_table = $('#client_assets_table').dataTable({
  133. "order": [[ 1, "asc" ]],
  134. "autoWidth": false,
  135. "columns": [
  136. {
  137. "data": "asset_name",
  138. "render": function(data, type, row) {
  139. return data;
  140. }
  141. },
  142. {
  143. "data": "asset_description",
  144. "render": function(data, type, row) {
  145. return data;
  146. }
  147. },
  148. {
  149. "data": "asset_type",
  150. "render": function(data, type, row) {
  151. return data.asset_name;
  152. }
  153. },
  154. {
  155. "data": "asset_ip",
  156. "render": function(data, type, row) {
  157. return data;
  158. }
  159. },
  160. {
  161. "data": "case_id",
  162. "render": function(data, type, row) {
  163. if (type === 'display' && data !== null) {
  164. let a_anchor = $('<a></a>');
  165. a_anchor.attr('href', '/case?cid=' + data);
  166. a_anchor.attr('target', '_blank');
  167. a_anchor.attr('rel', 'noopener');
  168. a_anchor.text('#' + data);
  169. return a_anchor.prop('outerHTML');
  170. }
  171. return data;
  172. }
  173. }
  174. ],
  175. "serverSide": true,
  176. "ajax": {
  177. "url": "/manage/assets/filter",
  178. "type": "GET",
  179. "data": function (d) {
  180. let page = Math.floor(d.start / d.length) + 1;
  181. d.page = page;
  182. d.per_page = d.length;
  183. d.customer_id = customer_id;
  184. d.order_by = d.columns[d.order[0].column].data;
  185. d.sort_dir = d.order[0].dir;
  186. },
  187. "dataSrc": function (json) {
  188. json.recordsTotal = json.data.total;
  189. json.recordsFiltered = json.data.total;
  190. json.draw = json.data.draw
  191. json.data = json.data.assets;
  192. return json.data;
  193. }
  194. }
  195. });
  196. /*
  197. Debugging notes
  198. It is possible to show information about the state of the table by typing in the console cases_table.page.info()
  199. see: https://datatables.net/reference/api/page.info()
  200. */
  201. cases_table = $('#client_cases_table').DataTable({
  202. "order": [[ 1, "asc" ]],
  203. "autoWidth": false,
  204. "columns": [
  205. {
  206. "data": "case_name",
  207. "render": function(data, type, row) {
  208. if (type === 'display') {
  209. let a_anchor = $('<a></a>');
  210. a_anchor.attr('href', '/case?cid=' + row['case_id']);
  211. a_anchor.attr('target', '_blank');
  212. a_anchor.attr('rel', 'noopener');
  213. a_anchor.text(data);
  214. return a_anchor.prop('outerHTML');
  215. }
  216. return data;
  217. }
  218. },
  219. {
  220. "data": "open_date",
  221. "render": function(data, type, row) {
  222. return data;
  223. }
  224. },
  225. {
  226. "data": "state",
  227. "render": function(data, type, row) {
  228. if (data !== null) {
  229. return data.state_name;
  230. } else {
  231. return 'Unknown';
  232. }
  233. }
  234. },
  235. {
  236. "data": "owner",
  237. "render": function(data, type, row) {
  238. return data.user_name;
  239. }
  240. }
  241. ],
  242. // https://datatables.net/manual/server-side
  243. "serverSide": true,
  244. "ajax": {
  245. "url": "/api/v2/cases",
  246. "type": "GET",
  247. "data": function(d) {
  248. d.page = Math.floor(d.start / d.length) + 1;
  249. d.per_page = d.length;
  250. d.case_customer_id = customer_id;
  251. d.order_by = d.columns[d.order[0].column].data;
  252. d.sort_dir = d.order[0].dir;
  253. d.case_name = d.search.value;
  254. },
  255. // https://datatables.net/reference/option/ajax.dataSrc
  256. "dataSrc": function(json) {
  257. json.recordsTotal = json.total;
  258. // According to the documentation (https://datatables.net/manual/server-side#Returned-data),
  259. // recordsFiltered is the number of records after filtering has been applied
  260. // since there are no filters on this table, it should be OK
  261. json.recordsFiltered = json.total;
  262. return json.data;
  263. }
  264. }
  265. });
  266. });