-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·154 lines (139 loc) · 3.78 KB
/
Copy pathcli.js
File metadata and controls
executable file
·154 lines (139 loc) · 3.78 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
const updateNotifier = require('update-notifier')
const inquirer = require("inquirer")
const process = require("process")
const moment = require("moment")
const log = require("debug")("cg:cli")
const client = require("./client")
const pkg = require('./package.json')
const DATE_FORMAT = "YYYY-MM-DD"
async function run() {
updateNotifier({ pkg }).notify({ defer: false })
const profile = await getProfile()
log("user profile %o", profile)
const orders = await getOrders(profile)
const orderSelection = await selectOrder(orders)
const order = await getOrder(profile, orderSelection)
const foodSelection = await selectFood(order)
await makeOrder(profile, orderSelection, foodSelection)
}
async function getProfile() {
const hasCredentials = await client.hasStoredCredentials()
let profile
if (hasCredentials) {
log("using stored credentials")
profile = await client.loadStoredCredentials()
} else {
log("logging in...")
const credentials = await inquirer.prompt([
{ name: "company", message: "Empresa" },
{ name: "username", message: "Usuario" },
{ name: "password", message: "Contraseña", type: "password", mask: true }
])
profile = await client.login(
credentials.username,
credentials.password,
credentials.company
)
await client.storeCredentials(profile)
log("credentials stored")
}
return profile
}
async function getOrders(profile) {
const startMoment = moment()
const endMoment = startMoment.clone().add(30, "days")
const orders = await client.getOrders(
profile.id,
startMoment.format(DATE_FORMAT),
endMoment.format(DATE_FORMAT)
)
const enabledOrders = orders.filter(order => order.enabled)
return enabledOrders
}
async function selectOrder(orders) {
const answer = await inquirer.prompt([
{
name: "order-selection",
message: "Orden",
type: "list",
choices: orders.map(order => ({
name: `${order.date} - [${order.status}]`,
value: order
}))
}
])
log(answer)
return answer["order-selection"]
}
async function getOrder(profile, orderSelection) {
const order = await client.getOrder(
profile.id,
orderSelection.order_id,
orderSelection.menu_id,
orderSelection.date
)
return order
}
async function selectFoodCategory(order) {
const answer = await inquirer.prompt([
{
name: "food-category-selection",
message: "Categoría",
type: "list",
choices: order.foods.map(foodCategory => ({
name: foodCategory.category_name,
value: foodCategory
}))
}
])
log(answer)
return answer["food-category-selection"]
}
async function selectFood(order) {
const answer = await inquirer.prompt(
order.foods.map(foodCategory => ({
name: foodCategory.category_column,
message: foodCategory.category_name,
type: "list",
choices: foods(foodCategory).map(food => ({
name: `${food.selected ? '[*] ' : ''}${food.food_name}`,
value: food
})),
pageSize: 20
}))
)
log(answer)
return answer
}
function foods(foodCategory) {
return Object.keys(foodCategory.food_types).reduce(
(accum, foodTypeKey) => accum.concat(foodCategory.food_types[foodTypeKey]),
[]
)
}
async function makeOrder(profile, orderSelection, foodSelection) {
const foodSelectionParams = Object.keys(foodSelection).reduce(
(accum, foodSelectionKey) => ({
...accum,
[foodSelectionKey]: foodSelection[foodSelectionKey].food_id
}),
{}
)
client.makeOrder(
profile.id,
orderSelection.order_id,
orderSelection.menu_id,
orderSelection.date,
foodSelectionParams
)
}
;(async () => {
try {
await run()
} catch (error) {
log(error)
console.log(error.message || error)
process.exit(1)
}
})()