-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
154 lines (141 loc) · 3.89 KB
/
Copy pathclient.js
File metadata and controls
154 lines (141 loc) · 3.89 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
const os = require("os")
const fs = require("fs")
const qs = require("querystring")
const util = require("util")
const path = require("path")
const { CookieJar } = require("tough-cookie")
const fetch = require("node-fetch")
const log = require("debug")("cg:client")
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
const BASE_URL = "https://admin.quilsoft.com/chef-gourmet/public/ws"
const CHEF_PASS = "e5c720cafc0eb5976128e674c2eac68e"
const STORED_CREDENTIALS_PATH = path.join(os.homedir(), ".cg")
let cookieJar = new CookieJar(undefined, { looseMode: true })
async function callAPI(resource, params = {}) {
const options = {}
const uri = `${BASE_URL}${resource}?${qs.stringify(params)}`
const cookieString = cookieJar.getCookieStringSync(uri, {})
if (cookieString) {
options.headers = { cookie: cookieString }
}
log("calling api %j", { uri, options })
return fetch(uri, options)
}
async function login(username, password, company) {
const params = {
username,
password,
company,
chef_pass: CHEF_PASS
}
const response = await callAPI("/login", params)
const { result } = await response.json()
if (result && result.id) {
await saveCookies(response)
return {
id: result.id,
username: result.username
}
}
throw new Error("Could not login")
}
async function saveCookies(response) {
const headers = response.headers.raw()
if (headers["set-cookie"]) {
headers["set-cookie"].forEach(cookie =>
cookieJar.setCookieSync(cookie, response.url)
)
}
}
async function getOrders(employeeId, dateFrom, dateTo) {
const params = {
employee_id: employeeId,
date_from: dateFrom,
date_to: dateTo,
chef_pass: CHEF_PASS
}
const response = await callAPI("/get-orders", params)
const { result } = await response.json()
log("orders result", JSON.stringify(result, null, 2))
return result.orders
}
async function getOrder(employeeId, orderId, menuId, date) {
const params = {
employee_id: employeeId,
menu_id: menuId,
date: date,
chef_pass: CHEF_PASS
}
if (orderId) {
params.order_id = orderId
}
const response = await callAPI("/get-order", params)
const { result } = await response.json()
log("order result", JSON.stringify(result, null, 2))
return result
}
async function makeOrder(
employeeId,
orderId,
menuId,
date,
foodSelectionParams
) {
const params = {
employee_id: employeeId,
menu_id: menuId,
date: date,
absent: false,
chef_pass: CHEF_PASS,
...foodSelectionParams
}
if (orderId) {
params.order_id = orderId
}
const response = await callAPI("/make-order", params)
const { result } = await response.json()
log("order result", JSON.stringify(result, null, 2))
return result
}
async function storeCredentials(profile) {
const serializedCredentials = JSON.stringify({
profile,
cookies: Buffer.from(JSON.stringify(cookieJar.serializeSync())).toString(
"base64"
)
})
await writeFile(STORED_CREDENTIALS_PATH, serializedCredentials)
log(`stored credentials in ${STORED_CREDENTIALS_PATH}`)
}
async function loadStoredCredentials() {
log(`loading credentials from ${STORED_CREDENTIALS_PATH}`)
const serializedCredentials = await readFile(STORED_CREDENTIALS_PATH)
const credentials = JSON.parse(serializedCredentials)
cookieJar = CookieJar.deserializeSync(
JSON.parse(Buffer.from(credentials.cookies, "base64"))
)
return credentials.profile
}
async function hasStoredCredentials() {
try {
const serializedCredentials = await readFile(STORED_CREDENTIALS_PATH)
const credentials = JSON.parse(serializedCredentials)
return !!(
credentials.profile &&
credentials.profile.id &&
credentials.cookies
)
} catch (e) {
return false
}
}
module.exports = {
storeCredentials,
hasStoredCredentials,
loadStoredCredentials,
login,
getOrders,
getOrder,
makeOrder
}