-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (72 loc) · 2.71 KB
/
index.js
File metadata and controls
87 lines (72 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import axios from 'axios'
import dotenv from 'dotenv'
import { map, pipe, filter, pluck, sum, groupBy, prop, values, sortWith, descend, has } from 'ramda'
dotenv.config()
const TOKEN = process.env.TOKEN
const instance = axios.create({
timeout: 1000,
headers: {
'Authorization': `Bearer ${TOKEN}`
}
})
const formatIntToFloat = (value) => parseFloat(`${value.slice(0, value.length - 2).join('')}.${value.slice(value.length - 2, value.length).join('')}`)
const getConcluded = filter(o => o.lastStatus === 'CONCLUDED')
const getByMonthAndYear = (monthAndYear) => filter(o => {
if (!monthAndYear) return true
const monthAndYearOfBuy = `${new Date(o.updatedAt).getUTCMonth() + 1 < 10 ? `0${new Date(o.updatedAt).getUTCMonth() + 1}` : new Date(o.updatedAt).getUTCMonth() + 1}/${new Date(o.updatedAt).getFullYear()}`
return monthAndYearOfBuy === monthAndYear
})
const filters = pipe(
getConcluded,
getByMonthAndYear(process.argv[2]),
)
const pipeSum = pipe(
filters,
pluck('bag'),
map(o => o.total.valueWithDiscount),
sum,
String,
Array.from,
formatIntToFloat,
(v) => v.toLocaleString('pt-br', { style: 'currency', currency: 'BRL'})
)
const getMerchants = pipe(
filters,
pluck('merchant'),
groupBy(prop('id'))
)
const getMerchantMoreDemanded = pipe(
getMerchants,
map(d => ({ merchant: { ...d[0] }, qtd: d.length })),
values,
sortWith([
descend(prop('qtd'))
])
)
const errors = {
ERR_BAD_REQUEST: "Seu token expirou, coloque um novo token no seu .env"
}
const formatDate = (date) => {
const dateSplitted = date.split('T')[0].split('-')
return `${dateSplitted[2]}/${dateSplitted[1]}/${dateSplitted[0]}`
}
const get = async () => {
try {
let orders = []
for (let i=0;i<999;i++) {
const response = await instance.get(`https://marketplace.ifood.com.br/v4/customers/me/orders?page=${i}&size=25`)
if (response.data.length === 0) {
i = 999
}
orders = [...orders, ...response.data]
}
const moreDemandedMerchants = getMerchantMoreDemanded(orders)
console.log(`Último pedido feito em: ${formatDate(orders[0].createdAt)} (${Math.floor(Math.abs(new Date(orders[0].createdAt.split('T')[0]) - new Date()) / (1000 * 60 * 60 * 24))} dias)`)
console.log(`Restaurante mais pedido: ${moreDemandedMerchants[0].merchant.name} (${moreDemandedMerchants[0].qtd})`)
console.log(`Total de pedidos concluidos: ${filters(orders).length}`)
console.log(`Valor total gasto: ${pipeSum(orders)}`)
} catch (err) {
console.log(has(err.code)(errors) ? errors[err.code] : `Undefined error: ${err.message}`)
}
}
get()