-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-status.js
More file actions
59 lines (50 loc) · 1.92 KB
/
Copy pathcheck-status.js
File metadata and controls
59 lines (50 loc) · 1.92 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
// Check the status of a postcard or bulk job
// Usage: POSTCARDBOT_API_KEY=pk_live_your_key node check-status.js <postcard_id or bulk_id>
const API_KEY = process.env.POSTCARDBOT_API_KEY;
const BASE_URL = process.env.POSTCARDBOT_API_URL || "https://postcard.bot";
if (!API_KEY) {
console.error("Set POSTCARDBOT_API_KEY environment variable");
process.exit(1);
}
const id = process.argv[2];
if (!id) {
console.error("Usage: node check-status.js <postcard_id or bulk_id>");
process.exit(1);
}
async function checkStatus() {
// Determine if this is a bulk job or single postcard
const isBulk = id.startsWith("bulk_");
const endpoint = isBulk
? `${BASE_URL}/api/v1/postcards/bulk/${encodeURIComponent(id)}`
: `${BASE_URL}/api/v1/postcards/${encodeURIComponent(id)}`;
const response = await fetch(endpoint, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
const data = await response.json();
if (!response.ok) {
console.error("Error:", data.error);
process.exit(1);
}
if (isBulk) {
console.log("Bulk Job Status:");
console.log(` ID: ${data.id || id}`);
console.log(` Status: ${data.status}`);
console.log(` Total: ${data.total}`);
console.log(` Processed: ${data.processed}`);
console.log(` Succeeded: ${data.succeeded}`);
console.log(` Failed: ${data.failed}`);
if (data.failures?.length) {
console.log("\nFailures:");
data.failures.forEach((f) => console.log(` ${f.index}: ${f.error}`));
}
} else {
console.log("Postcard Status:");
console.log(` ID: ${data.id}`);
console.log(` Status: ${data.status}`);
console.log(` Delivery: ${data.delivery_status || "pending"}`);
console.log(` Service: ${data.service || "unknown"}`);
console.log(` Expected delivery: ${data.expected_delivery_date || "TBD"}`);
console.log(` To: ${data.to?.name}, ${data.to?.city}, ${data.to?.country}`);
}
}
checkStatus().catch(console.error);