timeline.service.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Injectable } from '@angular/core';
  2. import { Observable } from 'rxjs';
  3. import { HttpClient } from '@angular/common/http';
  4. import * as moment from 'moment';
  5. import { CurrencyPipe } from '@angular/common';
  6. export class Transaction {
  7. itens = [];
  8. custom_data = {}
  9. header: { value: string, icon: string }[] = [];
  10. time: moment.Moment;
  11. };
  12. @Injectable({
  13. providedIn: 'root'
  14. })
  15. export class ApiEventsService {
  16. // public apiURL = 'https://storage.googleapis.com/dito-questions/events.json';
  17. public apiURL = 'https://dito-questions.storage.googleapis.com/events.json';
  18. public _events: any;
  19. constructor(
  20. protected $http: HttpClient
  21. ) {
  22. this._events = [
  23. {
  24. "event": "comprou-produto", "timestamp": "2016-09-22T13:57:33.2311892-03:00", "custom_data": [{ "key": "product_price", "value": 150 }, { "key": "transaction_id", "value": "3029384" }, { "key": "product_name", "value": "Calça Rosa" }]
  25. },
  26. {
  27. "event": "comprou-produto", "timestamp": "2016-10-02T11:37:35.2300892-03:00", "custom_data": [{ "key": "transaction_id", "value": "3409340" }, { "key": "product_name", "value": "Tenis Preto" }, { "key": "product_price", "value": 120 }]
  28. },
  29. {
  30. "event": "comprou", "timestamp": "2016-10-02T11:37:31.2300892-03:00", "revenue": 120, "custom_data": [{ "key": "transaction_id", "value": "3409340" }, { "key": "store_name", "value": "BH Shopping" }]
  31. },
  32. {
  33. "event": "comprou-produto", "timestamp": "2016-09-22T13:57:32.2311892-03:00", "custom_data": [{ "key": "product_name", "value": "Camisa Azul" }, { "key": "transaction_id", "value": "3029384" }, { "key": "product_price", "value": 100 }]
  34. },
  35. {
  36. "event": "comprou", "timestamp": "2016-09-22T13:57:31.2311892-03:00", "revenue": 250, "custom_data": [{ "key": "store_name", "value": "Patio Savassi" }, { "key": "transaction_id", "value": "3029384" }]
  37. }
  38. ]
  39. console.log(this._events)
  40. }
  41. public transactions() {
  42. return new Observable<any>(ob => {
  43. // this.$http.get(this.apiURL).subscribe(results => {
  44. // const transations = this.parseEventResults(results);
  45. setTimeout(() => {
  46. ob.next(this.parseEventResults(this._events))
  47. })
  48. // console.log(results)
  49. // })
  50. })
  51. }
  52. protected parseEventResults(events) {
  53. let currencyPipe = new CurrencyPipe('pt-BR')
  54. , transactions = []
  55. , transactionsMap = new Map<string, Transaction>();
  56. events.map(event => {
  57. let id;
  58. const found = event.custom_data.find(data => {
  59. if (data.key === 'transaction_id') {
  60. id = data.value;
  61. return true;
  62. }
  63. })
  64. if (!found) {
  65. return;
  66. }
  67. if (!transactionsMap.has(id)) {
  68. transactionsMap.set(id, new Transaction());
  69. }
  70. let trans = transactionsMap.get(id);
  71. switch (event.event) {
  72. case "comprou-produto":
  73. let add = { name: "", value: "" }
  74. // Atribui os valores do nome do produto e o preço para o objeto da listagem
  75. event.custom_data.find(item => {
  76. if (item.key === "product_name") {
  77. add.name = item.value;
  78. } else if (item.key === "product_price") {
  79. // Formata o valor com a moeda brasileira
  80. add.value = currencyPipe.transform(item.value, 'BRL', true, '1.2-2');
  81. }
  82. })
  83. trans.itens.push(add)
  84. break;
  85. case "comprou":
  86. // Converte o timestamp para um moment object
  87. trans.time = moment(event.timestamp);
  88. let place = "";
  89. trans.custom_data = event;
  90. // Busca o nome do estabelecimento
  91. event.custom_data.find(item => {
  92. if (item.key === "store_name") {
  93. place = item.value;
  94. return true;
  95. }
  96. })
  97. // Cria um array contendo os elementos do header de cada evento
  98. // Cada elemento é mapeado para um item do array
  99. trans.header = [
  100. { icon: "icon-calendar", value: trans.time.format("DD/MM/YYYY") },
  101. { icon: "icon-clock", value: trans.time.format("HH:mm") },
  102. { icon: "icon-place", value: place },
  103. { icon: "icon-money", value: currencyPipe.transform(event.revenue, 'BRL', true, '1.2-2') },
  104. ];
  105. break;
  106. }
  107. })
  108. // Converte o map
  109. transactionsMap.forEach(t => transactions.push(t))
  110. // Ordena as transações pela data
  111. transactions.sort((t1, t2) => {
  112. // if (t1) return 1;
  113. // if ( ) return -1;
  114. return (t1.time - t2.time);
  115. });
  116. console.log('transactions', transactions)
  117. return transactions;
  118. }
  119. }