-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-usage.ts
More file actions
94 lines (78 loc) · 3.61 KB
/
Copy pathexample-usage.ts
File metadata and controls
94 lines (78 loc) · 3.61 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
import { readFile } from 'fs/promises';
import type {
BootstrapInventoryMessage,
EnhancedBootstrapInventoryMessage,
Product,
ProductVariant
} from './message-types.js';
async function processMessage(filePath: string): Promise<void> {
try {
// Read and parse a message file
const content = await readFile(filePath, 'utf-8');
const message: BootstrapInventoryMessage = JSON.parse(content);
console.log(`Processing message for shop: ${message.shopSlug}`);
console.log(`Shop domain: ${message.shopDomain}`);
console.log(`Inventory system: ${message.shopInventorySystem}`);
// Access the payload data with full type safety
const products = message.data.payload;
console.log(`Found ${products.length} products in this batch`);
// Process each product
for (const product of products) {
console.log(`\nProduct: ${product.title}`);
console.log(` ID: ${product.id}`);
console.log(` Vendor: ${product.vendor}`);
console.log(` Type: ${product.productType}`);
console.log(` Status: ${product.status}`);
// Handle tags safely since they can be string[] | unknown[]
const tags = Array.isArray(product.tags) ? product.tags.filter((tag): tag is string => typeof tag === 'string') : [];
console.log(` Tags: ${tags.join(', ')}`);
// Process variants
console.log(` Variants (${product.variants.length}):`);
for (const variant of product.variants) {
console.log(` - ${variant.title}: $${variant.price} (SKU: ${variant.sku || 'N/A'})`);
console.log(` Inventory: ${variant.inventoryQuantity}`);
console.log(` Tracked: ${variant.inventoryItem.tracked}`);
}
}
// Additional data fields that were discovered during analysis
if ('batchSize' in message.data) {
console.log(`\nBatch info:`);
console.log(` Batch size: ${message.data.batchSize}`);
console.log(` Total variants: ${message.data.totalVariants}`);
console.log(` Total items: ${message.data.totalItems}`);
console.log(` Timestamp: ${message.data.timestamp}`);
}
} catch (error) {
console.error('Error processing message:', error);
}
}
// Type-safe helper functions using the auto-generated types
function getProductsByVendor(message: BootstrapInventoryMessage, vendor: string) {
return message.data.payload.filter(product => product.vendor === vendor);
}
function getVariantsWithInventory(product: BootstrapInventoryMessage['data']['payload'][0]) {
return product.variants.filter(variant => variant.inventoryQuantity > 0);
}
function calculateTotalInventoryValue(products: BootstrapInventoryMessage['data']['payload']): number {
return products.reduce((total, product) => {
const productValue = product.variants.reduce((variantTotal, variant) => {
const price = parseFloat(variant.price);
return variantTotal + (price * variant.inventoryQuantity);
}, 0);
return total + productValue;
}, 0);
}
// Example usage
async function example() {
const sampleFile = './messages/BOOTSTRAP_INVENTORY-1284.json';
await processMessage(sampleFile);
// Demonstrate type-safe operations
const content = await readFile(sampleFile, 'utf-8');
const message: BootstrapInventoryMessage = JSON.parse(content);
const ultraProProducts = getProductsByVendor(message, 'Ultra PRO');
console.log(`\nFound ${ultraProProducts.length} Ultra PRO products`);
const totalValue = calculateTotalInventoryValue(message.data.payload);
console.log(`Total inventory value: $${totalValue.toFixed(2)}`);
}
// Uncomment to run the example
// example().catch(console.error);