|
@@ -5,9 +5,8 @@ import * as moment from 'moment';
|
|
|
import { CurrencyPipe } from '@angular/common';
|
|
|
|
|
|
export class Transaction {
|
|
|
- itens = [];
|
|
|
- custom_data = {}
|
|
|
header: { value: string, icon: string }[] = [];
|
|
|
+ itens: { name: string, value: string }[] = [];
|
|
|
time: moment.Moment;
|
|
|
};
|
|
|
|
|
@@ -16,9 +15,16 @@ export class Transaction {
|
|
|
})
|
|
|
export class ApiEventsService {
|
|
|
|
|
|
+ // Registro de todos os tratadores de eventos
|
|
|
+ protected eventHandler = new Map<string, (trans, event) => void>([
|
|
|
+ ['comprou-produto', (trans, event) => this.comprouProdutoHandler(trans, event)],
|
|
|
+ ['comprou', (trans, event) => this.comprouHandler(trans, event)],
|
|
|
+ ]);
|
|
|
+
|
|
|
// public apiURL = 'https://storage.googleapis.com/dito-questions/events.json';
|
|
|
public apiURL = 'https://dito-questions.storage.googleapis.com/events.json';
|
|
|
public _events: any;
|
|
|
+ protected currencyPipe = new CurrencyPipe('pt-BR');
|
|
|
|
|
|
constructor(
|
|
|
protected $http: HttpClient
|
|
@@ -40,96 +46,115 @@ export class ApiEventsService {
|
|
|
"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" }]
|
|
|
}
|
|
|
]
|
|
|
- console.log(this._events)
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public transactions() {
|
|
|
|
|
|
return new Observable<any>(ob => {
|
|
|
+
|
|
|
+ // Realiza uma requisição para o endpoint determinado
|
|
|
// this.$http.get(this.apiURL).subscribe(results => {
|
|
|
- // const transations = this.parseEventResults(results);
|
|
|
+ // ob.next(this.parseEventResults(results))
|
|
|
+ // })
|
|
|
+
|
|
|
+ // Simula a requisição ajax
|
|
|
setTimeout(() => {
|
|
|
ob.next(this.parseEventResults(this._events))
|
|
|
})
|
|
|
- // console.log(results)
|
|
|
- // })
|
|
|
})
|
|
|
}
|
|
|
- protected parseEventResults(events) {
|
|
|
- let currencyPipe = new CurrencyPipe('pt-BR')
|
|
|
- , transactions = []
|
|
|
+ // Retona o id da transação do evento
|
|
|
+ protected findTransactionId(event): string {
|
|
|
+ let id;
|
|
|
+ event && event.custom_data.find(data => {
|
|
|
+ if (data.key === 'transaction_id') {
|
|
|
+ id = data.value;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Converte a lista de eventos em um lista de transações
|
|
|
+ protected parseEventResults(events): Transaction[] {
|
|
|
+ let transactions = []
|
|
|
, transactionsMap = new Map<string, Transaction>();
|
|
|
|
|
|
+ // Percorre a lista de eventos e realiza os agrupamentos
|
|
|
events.map(event => {
|
|
|
- let id;
|
|
|
- const found = event.custom_data.find(data => {
|
|
|
- if (data.key === 'transaction_id') {
|
|
|
- id = data.value;
|
|
|
- return true;
|
|
|
- }
|
|
|
- })
|
|
|
+ // Busca o id da transação contido no evento
|
|
|
+ let id = this.findTransactionId(event);
|
|
|
|
|
|
- if (!found) {
|
|
|
+ if (!id) {
|
|
|
+ // Não tratado por falta de definição
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // Cria uma nova instancia para o id caso ainda não exista
|
|
|
if (!transactionsMap.has(id)) {
|
|
|
transactionsMap.set(id, new Transaction());
|
|
|
}
|
|
|
-
|
|
|
+ // Recupera a instancia da transação correspondente ao id
|
|
|
let trans = transactionsMap.get(id);
|
|
|
|
|
|
- switch (event.event) {
|
|
|
- case "comprou-produto":
|
|
|
- let add = { name: "", value: "" }
|
|
|
- // Atribui os valores do nome do produto e o preço para o objeto da listagem
|
|
|
- event.custom_data.find(item => {
|
|
|
- if (item.key === "product_name") {
|
|
|
- add.name = item.value;
|
|
|
- } else if (item.key === "product_price") {
|
|
|
- // Formata o valor com a moeda brasileira
|
|
|
- add.value = currencyPipe.transform(item.value, 'BRL', true, '1.2-2');
|
|
|
- }
|
|
|
- })
|
|
|
- trans.itens.push(add)
|
|
|
- break;
|
|
|
- case "comprou":
|
|
|
- // Converte o timestamp para um moment object
|
|
|
- trans.time = moment(event.timestamp);
|
|
|
- let place = "";
|
|
|
-
|
|
|
- trans.custom_data = event;
|
|
|
- // Busca o nome do estabelecimento
|
|
|
- event.custom_data.find(item => {
|
|
|
- if (item.key === "store_name") {
|
|
|
- place = item.value;
|
|
|
- return true;
|
|
|
- }
|
|
|
- })
|
|
|
- // Cria um array contendo os elementos do header de cada evento
|
|
|
- // Cada elemento é mapeado para um item do array
|
|
|
- trans.header = [
|
|
|
- { icon: "icon-calendar", value: trans.time.format("DD/MM/YYYY") },
|
|
|
- { icon: "icon-clock", value: trans.time.format("HH:mm") },
|
|
|
- { icon: "icon-place", value: place },
|
|
|
- { icon: "icon-money", value: currencyPipe.transform(event.revenue, 'BRL', true, '1.2-2') },
|
|
|
- ];
|
|
|
-
|
|
|
- break;
|
|
|
- }
|
|
|
+ // Verifica o tipo de evento para tratamento
|
|
|
+ this.eventHandler.get(event.event)(trans, event)
|
|
|
})
|
|
|
- // Converte o map
|
|
|
+ // Converte o map em lista
|
|
|
transactionsMap.forEach(t => transactions.push(t))
|
|
|
|
|
|
// Ordena as transações pela data
|
|
|
transactions.sort((t1, t2) => {
|
|
|
- // if (t1) return 1;
|
|
|
- // if ( ) return -1;
|
|
|
return (t1.time - t2.time);
|
|
|
});
|
|
|
- console.log('transactions', transactions)
|
|
|
+
|
|
|
return transactions;
|
|
|
}
|
|
|
|
|
|
+ // Realiza o tratamento do evento comprou
|
|
|
+ // * Extra os dados do cabeçalho
|
|
|
+ // * Seta o atributo header da transação
|
|
|
+
|
|
|
+ protected comprouHandler(trans, event) {
|
|
|
+ let place = "";
|
|
|
+ // Converte o timestamp para um moment object
|
|
|
+ trans.time = moment(event.timestamp);
|
|
|
+ // Busca o nome do estabelecimento
|
|
|
+ event.custom_data.find(item => {
|
|
|
+ if (item.key === "store_name") {
|
|
|
+ place = item.value;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // Cria um array contendo os elementos do header de cada evento
|
|
|
+ // Cada elemento é mapeado para um item do array
|
|
|
+ trans.header = [
|
|
|
+ { icon: "icon-calendar", value: trans.time.format("DD/MM/YYYY") },
|
|
|
+ { icon: "icon-clock", value: trans.time.format("HH:mm") },
|
|
|
+ { icon: "icon-place", value: place },
|
|
|
+ { icon: "icon-money", value: this.currencyPipe.transform(event.revenue, 'BRL', true, '1.2-2') },
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ // Realiza o tratamento do evento comprou-produto
|
|
|
+ // * Extra os nomes e os preços dos itens comprados
|
|
|
+ // * Adiciona o novo item na lista de itens da transação
|
|
|
+ protected comprouProdutoHandler(trans, event) {
|
|
|
+ let add = { name: "", value: "" }, control = 0;
|
|
|
+ // Atribui os valores do nome do produto e o preço para o objeto da listagem
|
|
|
+ event.custom_data.find(item => {
|
|
|
+ if (item.key === "product_name") {
|
|
|
+ add.name = item.value;
|
|
|
+ control++;
|
|
|
+ } else if (item.key === "product_price") {
|
|
|
+ // Formata o valor com a moeda brasileira
|
|
|
+ add.value = this.currencyPipe.transform(item.value, 'BRL', true, '1.2-2');
|
|
|
+ control++;
|
|
|
+ }
|
|
|
+ // Se ja encontrou os dois dados evita de percorrer o array todo.
|
|
|
+ if (control > 1) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ trans.itens.push(add)
|
|
|
+ }
|
|
|
}
|