Skip to content

Instantly share code, notes, and snippets.

@erineland
Created June 25, 2019 16:40
Show Gist options
  • Save erineland/52d4ff7fc10cbe9db4221c8b09596f30 to your computer and use it in GitHub Desktop.
Save erineland/52d4ff7fc10cbe9db4221c8b09596f30 to your computer and use it in GitHub Desktop.
A small Express servlet to work out shipping costs!
var express = require('express');
// Helper libraries are supplied if needed
// const _ = require('lodash')
// const r = require('ramda')
const router = express.Router()
// Same state uses the base rate on the weight;
// Different state shipments have a fixed 20% cost increase on top of the previous value (rate on weight);
// Packages can have distinct weight units, either pounds or kilograms.
// Weight needs to be validated on pounds, thus requiring conversion from kg;
// Weight rate is tiered:
// Shipments up to 2 pounds are free;
// When pounds are > 2, each pound will be 1 dollar;
// When pounds are > 20, each pound will be 2 dollars;
// Examples:
// 1 pound shipment will be free;
// 3 pound shipment will be 3 dollars;
// 8 pound shipment will be 8 dollars;
// 25 pound shipment will be 50 dollars;
router.post('/request-shipment', function (req, res, next) {
// TODO implement based on README
const shipmentRequest = req.body;
console.log(`shipmentRequest object is: ${JSON.stringify(shipmentRequest)}`);
const sameStatePackage = shipmentRequest.shippingAddress.state === shipmentRequest.receivingAddress.state;
// Iterate packages
let overallCost = 0;
let overallWeight = 0;
shipmentRequest.packages.forEach(parcel => {
if (parcel.unit === 'KG') {
// Then convert weight to LB
// 1 KG = 2.2 LB
parcel.weight = Math.round(((parcel.weight * 2.2) * 100 / 100));
}
overallWeight += parcel.weight;
});
// Check unit of measurement for weight
if (overallWeight > 2 && overallWeight < 20) {
if (!sameStatePackage) {
overallCost += outerStatePackageCost(overallWeight);
} else {
overallCost += overallWeight;
}
} else if (overallWeight > 20) { // be explicit and declarative for readibility
const currentParcelCost = overallWeight * 2;
if (!sameStatePackage) {
overallCost = outerStatePackageCost(currentParcelCost);
} else {
overallCost += currentParcelCost;
}
}
return res.status(200).send({ shipment: { cost: overallCost } });
});
const outerStatePackageCost = originalCost => originalCost * 1.2;
module.exports = router;
const exampleShippingCostsToCalculate = {
"valid_shipment_request_free_same_state": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"packages": [
{
"name": "Small package",
"weight": 1,
"unit": "LB"
}
]
},
"shipment": {
"cost": 0
}
},
"valid_shipment_request_free_different_state": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "856 S. Cross St.",
"state": "CT",
"name": "John"
},
"packages": [
{
"name": "Small package",
"weight": 1,
"unit": "LB"
}
]
},
"shipment": {
"cost": 0
}
},
"valid_shipment_request_cheap_same_state": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"packages": [
{
"name": "Small package",
"weight": 1,
"unit": "LB"
},
{
"name": "Macbook Pro",
"weight": 4,
"unit": "LB"
}
]
},
"shipment": {
"cost": 5
}
},
"valid_shipment_request_cheap_different_state": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "856 S. Cross St.",
"state": "CT",
"name": "John"
},
"packages": [
{
"name": "Small package",
"weight": 1,
"unit": "LB"
},
{
"name": "Macbook Pro",
"weight": 4,
"unit": "LB"
}
]
},
"shipment": {
"cost": 6
}
},
"valid_shipment_request_expensive_same_state": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"packages": [
{
"name": "Big TV",
"weight": 23,
"unit": "LB"
},
{
"name": "Macbook Pro",
"weight": 4,
"unit": "LB"
}
]
},
"shipment": {
"cost": 54
}
},
"valid_shipment_request_expensive_different_state": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "856 S. Cross St.",
"state": "CT",
"name": "John"
},
"packages": [
{
"name": "Big TV",
"weight": 23,
"unit": "LB"
},
{
"name": "Macbook Pro",
"weight": 4,
"unit": "LB"
}
]
},
"shipment": {
"cost": 64.8
}
},
"valid_shipment_request_mixed_weight_units": {
"shipment_request": {
"shippingAddress": {
"addressName": "7433 Victoria Ave.",
"state": "WI",
"name": "Jones"
},
"receivingAddress": {
"addressName": "856 S. Cross St.",
"state": "CT",
"name": "John"
},
"packages": [
{
"name": "Big TV",
"weight": 10.432636917,
"unit": "KG"
},
{
"name": "Macbook Pro",
"weight": 4,
"unit": "LB"
}
]
},
"shipment": {
"cost": 64.8
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment