Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. export function loadJsonDataToLocalStorage(url, localStorageKey) {
  2. return $.ajax({
  3. type: 'Get',
  4. url,
  5. dataType: 'json',
  6. success: (data) => {
  7. localStorage.setItem(localStorageKey, JSON.stringify(data));
  8. },
  9. error: (e) => {
  10. console.log(e);
  11. },
  12. });
  13. }
  14.  
  15. export function getData(key) {
  16. const data = localStorage.getItem(key);
  17.  
  18. return data !== null ? JSON.parse(data) : null;
  19. }
  20.  
  21. function setData(key, data) {
  22. localStorage.setItem(key, JSON.stringify(data));
  23. }
  24.  
  25. function getNewId(data) {
  26. return data
  27. .map((item) => item.ID)
  28. .reduce((maxValue, currentValue) => Math.max(maxValue, currentValue), 0) + 1;
  29. }
  30.  
  31. function addNewClient() {
  32. const clients = getData('line-clients');
  33.  
  34. const maxQueueNumber = clients
  35. .map((client) => client.number)
  36. .reduce((maxValue, currentValue) => Math.max(maxValue, currentValue), 0) + 1;
  37.  
  38. const newId = getNewId(clients);
  39. const creationTimestamp = new Date().getTime();
  40.  
  41. clients.push({
  42. ID: newId,
  43. number: maxQueueNumber,
  44. createdAt: creationTimestamp,
  45. serviced: false,
  46. });
  47.  
  48. setData('line-clients', clients);
  49.  
  50. return newId;
  51. }
  52.  
  53. function addNewService(clientId, specialistId) {
  54. const service = getData('line-data');
  55. const newId = getNewId(service);
  56.  
  57. service.push({
  58. ID: newId,
  59. client_id: clientId,
  60. specialist_id: specialistId,
  61. });
  62.  
  63. setData('line-data', service);
  64.  
  65. return newId;
  66. }
  67.  
  68. export function getClientsBySpecialist(specialistId, serviced = false) {
  69. const serviceLine = getData('line-data');
  70.  
  71. if (serviceLine !== null) {
  72. const serviceEntries = serviceLine.filter(
  73. (service) => service.specialist_id === specialistId,
  74. );
  75.  
  76. if (serviceEntries.length > 0) {
  77. const clients = getData('line-clients');
  78.  
  79. if (clients !== null) {
  80. return clients
  81. .filter(
  82. (client) => serviceEntries.findIndex(
  83. (service) => service.client_id === client.ID,
  84. ) !== -1 && client.serviced === serviced,
  85. );
  86. }
  87. }
  88. }
  89.  
  90. return [];
  91. }
  92.  
  93. export function addNewEntry(specialistId) {
  94. const specialists = getData('line-specialists');
  95. const specialistById = specialists.find(
  96. (specialist) => specialist.ID === specialistId,
  97. );
  98.  
  99. if (typeof specialistById !== 'undefined') {
  100. const newClientId = addNewClient();
  101.  
  102. addNewService(newClientId, specialistById.ID);
  103. }
  104. }
  105.  
  106. export function markAsServiced(clientId) {
  107. const clients = getData('line-clients');
  108. const clientListId = clients.findIndex(
  109. (client) => client.ID === clientId,
  110. );
  111.  
  112. if (clientListId !== -1) {
  113. clients[clientListId].serviced = true;
  114. clients[clientListId].endedAt = new Date().getTime();
  115.  
  116. setData('line-clients', clients);
  117. }
  118. }
  119.  
  120. export function normalizeImportedClientsData() {
  121. const creationTimestamp = new Date().getTime();
  122. const clients = getData('line-clients');
  123. const updatedClients = clients.map((client) => {
  124. const updatedClient = {
  125. ...client,
  126. createdAt: creationTimestamp,
  127. serviced: false,
  128. };
  129.  
  130. return updatedClient;
  131. });
  132.  
  133. setData('line-clients', updatedClients);
  134. }
  135.  
  136. export function getAverageTimeOfWaiting(specialistId) {
  137. const servicedClients = getClientsBySpecialist(specialistId, true);
  138. const averageTime = servicedClients
  139. .reduce(
  140. (total, currentClient) => {
  141. if (typeof currentClient.endedAt !== 'undefined') {
  142. return total + (currentClient.endedAt - currentClient.createdAt);
  143. }
  144.  
  145. return total;
  146. },
  147. 0,
  148. ) / servicedClients.length;
  149.  
  150. if (!Number.isNaN(averageTime)) {
  151. return Math.round(averageTime);
  152. }
  153.  
  154. return 0;
  155. }
  156.  
  157. export function getTimeRemainingByClientNumber(clientNumber) {
  158. const clients = getData('line-clients');
  159.  
  160. const requiredClient = clients.find((client) => client.number === clientNumber);
  161.  
  162. if (typeof requiredClient !== 'undefined') {
  163. const serviceEntry = getData('line-data').find((entry) => entry.client_id === requiredClient.ID);
  164.  
  165. if (typeof serviceEntry !== 'undefined') {
  166. const averageTimeOfWaiting = getAverageTimeOfWaiting(serviceEntry.specialist_id);
  167. const requiredClientIndexInLine = getClientsBySpecialist(serviceEntry.specialist_id)
  168. .sort((a, b) => a.number - b.number)
  169. .findIndex((client) => client.ID === requiredClient.ID);
  170.  
  171. if (requiredClientIndexInLine !== -1) {
  172. return averageTimeOfWaiting * requiredClientIndexInLine;
  173. }
  174. }
  175. }
  176.  
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement