Created
October 15, 2018 08:03
-
-
Save saharshagrawal/56c0fe90ea14ac96229aac71d1a5a1f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A shipment has been received by an importer | |
* @param {org.acme.shipping.perishable.ShipmentReceived} shipmentReceived - the ShipmentReceived transaction | |
* @transaction | |
*/ | |
async function payOut(shipmentReceived) { // eslint-disable-line no-unused-vars | |
const contract = shipmentReceived.shipment.contract; | |
const shipment = shipmentReceived.shipment; | |
let payOut = contract.unitPrice * shipment.unitCount; | |
console.log('Received at: ' + shipmentReceived.timestamp); | |
console.log('Contract arrivalDateTime: ' + contract.arrivalDateTime); | |
// set the status of the shipment | |
shipment.status = 'ARRIVED'; | |
// if the shipment did not arrive on time the payout is zero | |
if (shipmentReceived.timestamp > contract.arrivalDateTime) { | |
payOut = 0; | |
console.log('Late shipment'); | |
} else { | |
// find the lowest temperature reading | |
if (shipment.temperatureReadings) { | |
// sort the temperatureReadings by centigrade | |
shipment.temperatureReadings.sort(function (a, b) { | |
return (a.centigrade - b.centigrade); | |
}); | |
const lowestReading = shipment.temperatureReadings[0]; | |
const highestReading = shipment.temperatureReadings[shipment.temperatureReadings.length - 1]; | |
let penalty = 0; | |
console.log('Lowest temp reading: ' + lowestReading.centigrade); | |
console.log('Highest temp reading: ' + highestReading.centigrade); | |
// does the lowest temperature violate the contract? | |
if (lowestReading.centigrade < contract.minTemperature) { | |
penalty += (contract.minTemperature - lowestReading.centigrade) * contract.minPenaltyFactor; | |
console.log('Min temp penalty: ' + penalty); | |
} | |
// does the highest temperature violate the contract? | |
if (highestReading.centigrade > contract.maxTemperature) { | |
penalty += (highestReading.centigrade - contract.maxTemperature) * contract.maxPenaltyFactor; | |
console.log('Max temp penalty: ' + penalty); | |
} | |
// apply any penalities | |
payOut -= (penalty * shipment.unitCount); | |
if (payOut < 0) { | |
payOut = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment