Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional confirmation to link price changes and airplane purchase for significant changes #634

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions airline-web/public/javascripts/airline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,13 @@ function updateAirlineBaseList(airlineId, table) {
});
}

// A 'x' percent change in price is considered to be 'significant' and trigger additional confirmation.
var SIGNIFICANT_PRICE_THRESHOLD_PERCENT = 50;

function calculatePercentChange(existingValue, newValue){
return Math.abs(newValue - existingValue) / existingValue * 100;
}

var assignedDelegates = 0
var availableDelegates = 0
var negotiationOddsLookup
Expand Down Expand Up @@ -2655,6 +2662,7 @@ function linkConfirmation() {
var planInfo = getPlanLinkCapacity()
var planFrequency = planInfo.future ? planInfo.future.frequency : planInfo.current.frequency


for (i = 0 ; i < planFrequency ; i ++) {
var image = $("<img>")
image.attr("src", $(".frequencyBar").data("fillIcon"))
Expand All @@ -2671,10 +2679,41 @@ function linkConfirmation() {
$("#linkConfirmationModal div.updating.capacity").append(futureCapacitySpan)
}

const futurePrices = {
economy: parseInt($("#planLinkEconomyPrice").val()),
business: parseInt($("#planLinkBusinessPrice").val()),
first: parseInt($("#planLinkFirstPrice").val())
};

const fareClasses = ["economy", "business", "first"];
var significantChanges = [];

if (existingLink) {
fareClasses.forEach((fareClass) => {
const existingPrice = existingLink.price[fareClass];
const futurePrice = futurePrices[fareClass];

const percentChange = calculatePercentChange(existingPrice, futurePrice);
if (percentChange >= SIGNIFICANT_PRICE_THRESHOLD_PERCENT) {
significantChanges.push(`${fareClass.charAt(0).toUpperCase() + fareClass.slice(1)}: $${existingPrice} -> $${futurePrice}`);
}
});
}

$('#linkConfirmationModal .confirmButton').off('click');
if (significantChanges.length > 0) {
const confirmationPromptMessage = `≥${SIGNIFICANT_PRICE_THRESHOLD_PERCENT}% price change${significantChanges.length > 1 ? "s" : ""} detected:<ul>${significantChanges.map(change => `<li>${change}</li>`).join("")}</ul>`;
$('#linkConfirmationModal .confirmButton').on('click', function() {
promptConfirm(confirmationPromptMessage, createLink);
closeModal($('#linkConfirmationModal'));
});
} else {
$('#linkConfirmationModal .confirmButton').on('click', function() {
createLink()
});
}


$('#linkConfirmationModal div.updating.price').text('$' + $('#planLinkEconomyPrice').val() + " / $" + $('#planLinkBusinessPrice').val() + " / $" + $('#planLinkFirstPrice').val())

$('#linkConfirmationModal div.updating.price').text(toLinkClassValueString(futurePrices, '$'));
$('#linkConfirmationModal').fadeIn(200)

getLinkNegotiation()
Expand Down
20 changes: 18 additions & 2 deletions airline-web/public/javascripts/airplane.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ function validateAirplaneQuantity() {
return quantity
}

// Threshold for determining if additional confirmation is required before completing an airplane purchase.
var SIGNIFICANT_AIRPLANE_PURCHASE_THRESHOLD = 0.5;

function promptBuyAirplane(modelId, condition, price, deliveryTime, explicitHomeAirportId, multipleAble, buyAirplaneFunction) {
var model = loadedModelsById[modelId]
if (model.imageUrl) {
Expand All @@ -293,10 +296,11 @@ function promptBuyAirplane(modelId, condition, price, deliveryTime, explicitHome
$('#buyAirplaneModal .price').text("$" + commaSeparateNumber(price))
$('#buyAirplaneModal .condition').text(condition + "%")

var quantity = 1;
if (multipleAble) {
$('#buyAirplaneModal .quantity .input').val(1)
$('#buyAirplaneModal .quantity .input').on('input', function(e){
var quantity = validateAirplaneQuantity()
quantity = validateAirplaneQuantity()
updateAirplaneTotalPrice(quantity * price)
});
$('#buyAirplaneModal .quantity').show()
Expand Down Expand Up @@ -389,7 +393,19 @@ function promptBuyAirplane(modelId, condition, price, deliveryTime, explicitHome
selectedConfigurationId = $($("#buyAirplaneModal .configuration-options").children()[selectedIndex]).data("configurationId")
}

buyAirplaneFunction($('#buyAirplaneModal .quantity .input').val(), $("#buyAirplaneModal .homeOptions").find(":selected").val(), selectedConfigurationId)
var homeAirportId = $("#buyAirplaneModal .homeOptions").find(":selected").val();

const totalPrice = quantity * price;
var percentOfBalance = totalPrice / activeAirline.balance;
if (percentOfBalance >= SIGNIFICANT_AIRPLANE_PURCHASE_THRESHOLD) {
var confirmationMessage = `This purchase accounts for ${(percentOfBalance * 100).toFixed(1)}% of your airline's current balance. Are you sure you want to proceed?`;

promptConfirm(confirmationMessage, function() {
buyAirplaneFunction(quantity, homeAirportId, selectedConfigurationId);
});
} else {
buyAirplaneFunction(quantity, homeAirportId, selectedConfigurationId);
}
})
$('#buyAirplaneModal').fadeIn(200)
},
Expand Down