src/shipping-fee.tsTypeScript
@@ -0,0 +1,52 @@
1+
export interface ShipmentItem {2+
weightKg: number;3+
}4+
5+
export interface ShippingQuoteInput {6+
items: readonly ShipmentItem[];7+
destinationRegion: string;8+
merchandiseSubtotalCents: number;9+
discountCents: number;10+
}11+
12+
function baseFeeCents(totalWeightKg: number): number {13+
if (totalWeightKg < 1) {14+
return 500;15+
}16+
17+
if (totalWeightKg < 5) {18+
return 900;19+
}20+
21+
return 1_800;22+
}23+
24+
function surchargeCentsPerKg(region: string): number {25+
if (region === "europe") {26+
return 125;27+
}28+
29+
if (region === "international") {30+
return 275;31+
}32+
33+
return 0;34+
}35+
36+
export function calculateShippingFee(input: ShippingQuoteInput): number {37+
if (input.merchandiseSubtotalCents >= 10_000) {38+
return 0;39+
}40+
41+
const totalWeightKg = input.items.reduce(42+
(total, item) => total + item.weightKg,43+
0,44+
);45+
const rate = surchargeCentsPerKg(input.destinationRegion);46+
const surcharge = input.items.reduce(47+
(total, item) => total + Math.round(item.weightKg * rate),48+
0,49+
);50+
51+
return baseFeeCents(totalWeightKg) + surcharge;52+
}