-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShipmentRecreate.js
147 lines (135 loc) · 3.47 KB
/
ShipmentRecreate.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
const process = require("process");
process.chdir("./EasyPost NodeJS Scripts");
const EasyPost = require("@easypost/api");
const dotenv = require("dotenv");
dotenv.config();
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const { exec } = require("child_process");
const ship = require("./misc.json");
// const client = new EasyPost(process.env.PROD_KEY);
const client = new EasyPost(process.env.TEST_KEY);
let properties = [
"created_at",
"messages",
"status",
"tracking_code",
"updated_at",
"batch_id",
"batch_status",
"batch_message",
"id",
"order_id",
"postage_label",
"tracker",
"selected_rate",
"scan_form",
"usps_zone",
"refund_status",
"mode",
"fees",
"object",
"rates",
"insurance",
"forms",
"verifications",
];
let nest = [
ship.to_address,
ship.from_address,
ship.return_address,
ship.buyer_address,
ship.parcel,
];
properties.forEach((item) => {
delete ship[item];
});
nest.forEach((obj) => {
properties.forEach((item) => {
delete obj[item];
});
});
if (ship.customs_info != null) {
properties.forEach((item) => {
delete ship.customs_info[item];
});
ship["customs_info"]["customs_items"].forEach((custItem) => {
properties.forEach((item) => {
delete custItem[item];
});
});
}
if (ship["options"]["print_custom"]) {
delete ship["options"]["print_custom"];
}
try {
(async () => {
let shipment = await client.Shipment.create({
to_address: ship.to_address,
from_address: ship.from_address,
parcel: ship.parcel,
customs_info: ship.customs_info,
options: ship.options,
is_return: ship.is_return,
reference: ship.reference,
carrier_accounts: ["ca_0bac9dd43e924320a6317a3121390a53"],
service: "DomesticExpress1200"
});
if (shipment.postage_label == null) {
if (shipment["messages"].length > 0) {
shipment["messages"].forEach((message) => {
console.log(message["carrier"] + ": " + message["type"]);
console.log(message["message"]);
console.log();
});
} else {
console.log("No rate_errors returned!");
}
if (shipment["rates"].length > 0) {
shipment["rates"].forEach((rate) => {
console.log(rate["carrier"] + ": " + rate["service"]);
console.log(rate["rate"] + " - " + rate["id"]);
console.log();
});
} else {
console.log("No rates returned!");
console.log(shipment.id);
process.exit(0);
}
rl.question(
"Please enter the rate you wish to purchase, or press enter to quit: ",
async (user) => {
let boughtShipment;
if (user.includes("rate_")) {
try {
boughtShipment = await client.Shipment.buy(
shipment.id,
(rate = { id: user })
);
} catch (error) {
console.log(error.code+": "+error.message);
console.log(shipment.id);
process.exit(0);
}
//data
exec("open " + boughtShipment.postage_label["label_url"]);
rl.close();
} else {
//data
process.exit(0);
}
}
);
} else {
//data
exec("open " + shipment.postage_label["label_url"]);
process.exit(0);
}
})();
} catch (error) {
console.log(error.code+": "+error.message);
process.exit(0);
}