Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Commit 622edd8

Browse files
committed
adding support for bulk order info request
1 parent 8ef1f26 commit 622edd8

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ example:
4949
duffle --auth-token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyasdllkflkasdflaslljfjlJ1c2VyX2lkIjo2NzYsInVzZXJuYW1lIjoiZHlsYW5iYXRodXJzdCIsImV4cCI6MTU1MzE5MzUwMiwiZW1haWwiOiJkeWxhbmJhdGh1cnN0QGdtYWlsLmNvbSIsInNkIjoiZ2Z4M081T0kiLCJyIjo.iNkd4cmFoRWYiLCJkcyI6ImZVM cancel-orders examples/files/cancel.csv
5050
```
5151
Your console output will show the number of successful and unsuccessfully canceled orders, as well as the response text for each order.
52+
53+
## Order Info
54+
To get a bulk set of order information on Purse, create a `.csv` file and format it exactly like <a href="./examples/files/cancel.csv">this file</a> in the examples directory. Then run:
55+
56+
```
57+
duffle --auth-token YOUR_PURSE_TOKEN orders-info /PATH/TO/YOUR/CSV/FILE.csv
58+
```
59+
example:
60+
```
61+
duffle --auth-token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyasdllkflkasdflaslljfjlJ1c2VyX2lkIjo2NzYsInVzZXJuYW1lIjoiZHlsYW5iYXRodXJzdCIsImV4cCI6MTU1MzE5MzUwMiwiZW1haWwiOiJkeWxhbmJhdGh1cnN0QGdtYWlsLmNvbSIsInNkIjoiZ2Z4M081T0kiLCJyIjo.iNkd4cmFoRWYiLCJkcyI6ImZVM orders-info examples/files/orders.csv
62+
```
63+
Your console output will show the number of successful and unsuccessful order info requests, as well as the order info for each order.

bin/duffle

+27
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,31 @@ program
101101
console.log(`${JSON.stringify(bulkCancel.failedOrders)}`);
102102
});
103103

104+
program
105+
.command('orders-info <path>')
106+
.description('Gets a bulk list of order info from a CSV file')
107+
.action(async (path, options) => {
108+
let Api;
109+
try {
110+
Api = new apiRequest(options.parent.authToken);
111+
} catch (e) {
112+
console.log('*********************');
113+
console.log(e.message);
114+
console.log('*********************');
115+
return;
116+
}
117+
let parsedFile;
118+
try {
119+
parsedFile = await parseCSV(path);
120+
} catch (e) {
121+
console.log(e);
122+
return;
123+
}
124+
const bulkInfo = await Api.getBulkInfo(parsedFile);
125+
console.log(`${bulkInfo.successfulOrders.length} successful orders:`);
126+
console.log(`${JSON.stringify(bulkInfo.successfulOrders)}`);
127+
console.log(`${bulkInfo.failedOrders.length} failed orders:`);
128+
console.log(`${JSON.stringify(bulkInfo.failedOrders)}`);
129+
});
130+
104131
program.parse(process.argv);

examples/files/orders.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id
2+
SP-GJHY8ET5
3+
SP-WZEUCGBR

lib/api.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class apiRequest {
1515
this.createOrder = this.createOrder.bind(this);
1616
this.modifyOrder = this.modifyOrder.bind(this);
1717
this.cancelOrder = this.cancelOrder.bind(this);
18+
this.getOrderInfo = this.getOrderInfo.bind(this);
1819
}
1920
async bulkChange(bulkMethod, orders) {
2021
const successfulOrders = [];
@@ -41,6 +42,9 @@ class apiRequest {
4142
async cancelBulkOrders(orders) {
4243
return this.bulkChange(this.cancelOrder, orders);
4344
}
45+
async getBulkInfo(orders) {
46+
return this.bulkChange(this.getOrderInfo, orders);
47+
}
4448

4549
async createOrder(order) {
4650
const validOrder = this.buildCreateOrder(order);
@@ -114,7 +118,7 @@ class apiRequest {
114118
}
115119
}
116120
async cancelOrder(order) {
117-
const validCancelOrder = this.buildCancelOrder(order);
121+
const validCancelOrder = this.validateBasicOrder(order);
118122

119123
if (!validCancelOrder) {
120124
order.success = false;
@@ -149,6 +153,42 @@ class apiRequest {
149153
}
150154
}
151155
}
156+
async getOrderInfo(order) {
157+
const validOrder = this.validateBasicOrder(order);
158+
159+
if (!validOrder) {
160+
order.success = false;
161+
order.errorMessage = 'This was not a valid order';
162+
} else {
163+
const opts = {
164+
method: 'GET',
165+
uri: `${this.apiUrl}/orders/${validOrder}`,
166+
resolveWithFullResponse: true,
167+
headers: { Authorization: `JWT ${this.jwt}` },
168+
};
169+
170+
let infoRequest;
171+
try {
172+
console.log(opts)
173+
infoRequest = await request(opts);
174+
} catch (e) {
175+
console.log(e)
176+
order.success = false;
177+
order.errorMessage = e.message;
178+
return order;
179+
}
180+
181+
if (infoRequest.statusCode !== 200) {
182+
order.success = false;
183+
order.errorMessage = infoRequest.body;
184+
return order;
185+
} else {
186+
order.success = true;
187+
order.body = JSON.parse(infoRequest.body);
188+
return order;
189+
}
190+
}
191+
}
152192

153193
buildCreateOrder(order) {
154194
const { asin, quantity, discount, full_name, street1,
@@ -184,7 +224,7 @@ class apiRequest {
184224
};
185225
return modifyModel;
186226
}
187-
buildCancelOrder(order) {
227+
validateBasicOrder(order) {
188228
const { id } = order;
189229
if (!id) {
190230
return false;

0 commit comments

Comments
 (0)