-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·244 lines (206 loc) · 6.6 KB
/
index.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env node
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { Low, JSONFile } from 'lowdb'
import chalk from 'chalk';
import { usage, errorLog, prompt } from './utilities.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')
const adapter = new JSONFile(file)
const db = new Low(adapter)
const args = process.argv;
const invalidArgType = (a, et, pt) => {
errorLog(`
Invalid argument type for ${a}:
Expected type: ${et}
Provided type: ${pt}
`);
}
const addProduct = async () => {
const q = chalk.blue('type in the name of the product you want to add');
const name = await prompt(q);
if (typeof(name) !== 'string') {
invalidArgType('product name', 'string', typeof(name));
return;
}
const q2 = chalk.blue('now type in the SKU of the product you want to add');
const SKU = await prompt(q2);
if (typeof(SKU) !== 'string') {
invalidArgType('SKU', 'string', typeof(SKU));
return;
}
await db.read();
const { products } = db.data;
if (products[SKU]) {
errorLog(`Product ${SKU} already exists`);
return;
}
products[SKU] = name;
await db.write();
}
const addWarehouse = async () => {
const idQ = chalk.blue(`type in the id of the warehouse you want to add`);
const id = parseInt(await prompt(idQ));
if (typeof(id) !== 'number') {
invalidArgType('warehouse #', 'number', typeof(id));
return;
}
const stockLimitQ = chalk.blue(`type in the stock limit of the warehouse you want to add or press enter for no limit`);
const stockLimit = parseInt(await prompt(stockLimitQ)) || null;
if (stockLimit && typeof(stockLimit) !== 'number') {
invalidArgType('stock limit', 'number', typeof(stockLimit));
return;
}
await db.read();
const { warehouses } = db.data;
warehouses[id] = { stockLimit, products: {} };
await db.write();
}
const stock = async () => {
const skuQ = chalk.blue(`type in the SKU of the product you want to stock`);
const SKU = await prompt(skuQ);
if (typeof(SKU) !== 'string') {
invalidArgType('SKU', 'string', typeof(SKU));
return;
}
const idQ = chalk.blue(`type in the id of the warehouse you want to stock product(s) in`);
const id = parseInt(await prompt(idQ));
if (typeof(id) !== 'number') {
invalidArgType('warehouse #', 'number', typeof(id));
return;
}
const quantityQ = chalk.blue(`type in the quantity of the product you want to stock`);
const q = parseInt(await prompt(quantityQ));
if (typeof(q) !== 'number') {
invalidArgType('quantity', 'number', typeof(q));
return;
}
await db.read();
const { warehouses } = db.data;
if (!warehouses[id]) {
errorLog(`Warehouse ${warehouseId} does not exist`);
return;
} else if (!warehouses[id].products) {
warehouses[id].products = {
[SKU]: { quantity: q }
};
} else if (!warehouses[id].products[SKU]) {
warehouses[id].products = { ...warehouses[id].products, [SKU]: { quantity: q } };
} else {
warehouses[warehouseId].products[SKU] = {
...warehouses[warehouseId].products[SKU],
quantity: warehouses[warehouseId].products[SKU].quantity + q
};
}
await db.write();
}
const unstock = async () => {
const skuQ = chalk.blue(`type in the SKU of the product you want to add`);
const SKU = await prompt(skuQ);
if (typeof(SKU) !== 'string') {
invalidArgType('SKU', 'string', typeof(SKU));
return;
}
const idQ = chalk.blue(`type in the id of the warehouse you want to add`);
const id = parseInt(await prompt(idQ));
if (typeof(id) !== 'number') {
invalidArgType('warehouse #', 'number', typeof(id));
return;
}
const quantityQ = chalk.blue(`type in the quantity of the product you want to add`);
const q = parseInt(await prompt(quantityQ));
if (typeof(q) !== 'number') {
invalidArgType('quantity', 'number', typeof(q));
return;
}
await db.read();
const { warehouses } = db.data;
if (!warehouses[id]) {
errorLog(`Warehouse ${warehouseId} does not exist`);
return;
} else if (!warehouses[id].products){
errorLog(`Warehouse ${warehouseId} has no products`);
return;
} else if (!warehouses[id].products[SKU]) {
errorLog(`Product ${SKU} does not exist in warehouse ${id}`);
return;
} else {
const unstockEffect = warehouses[id].products[SKU].quantity - q;
const newQuantity = unstockEffect < 0 ? 0 : unstockEffect;
warehouses[id].products[SKU] = {
...warehouses[warehouseId].products[SKU],
quantity: newQuantity
};
}
await db.write();
}
const listProducts = async () => {
await db.read();
const { products } = db.data;
for (const product in products) {
console.log(`${product} - ${products[product]}`);
}
}
const listWarehouses = async () => {
await db.read();
const { warehouses } = db.data;
console.log('WAREHOUSES');
for (const warehouse in warehouses) {
console.log(`Warehouse #${warehouse} with stock limit of ${warehouse.stockLimit}`);
}
}
const listWarehouse = async () => {
const idQ = chalk.blue(`type in the id of the warehouse you want to view`);
const id = parseInt(await prompt(idQ));
if (typeof(id) !== 'number') {
invalidArgType('warehouse #', 'number', typeof(id));
return;
}
await db.read();
const { warehouses } = db.data;
if (!warehouses[id]) {
errorLog(`Warehouse ${id} does not exist`);
return;
}
for (const product in warehouses[id].products) {
console.log(`
Item name: ${db.data.products[product]}
Item SKU: ${product}
QTY: ${warehouses[id].products[product].quantity}
`);
}
}
if(args.length > 3) {
errorLog('only one command can be accepted')
usage();
}
switch(args[2]) {
case 'help':
usage();
break;
case 'add_product':
addProduct();
break;
case 'add_warehouse':
addWarehouse();
break;
case 'stock':
stock();
break;
case 'unstock':
unstock();
break;
case 'list_products':
listProducts();
break;
case 'list_warehouses':
listWarehouses();
break;
case 'list_warehouse':
listWarehouse();
break;
default:
errorLog('Invalid command passed');
usage();
break;
}