mirror of
https://github.com/ae-utbm/sith.git
synced 2026-10-04 09:54:16 +00:00
adapt formulas to new price system
This commit is contained in:
1 parent
adeadeaf70
commit
cf40e941d4
11 files changed
+39
-97
No files matched your search
@@ -2,6 +2,7 @@ import { AlertMessage } from "#core:utils/alert-message";
|
||||
import { BasketItem } from "#counter:counter/basket";
|
||||
import type {
|
||||
CounterConfig,
|
||||
CounterItem,
|
||||
ErrorMessage,
|
||||
ProductFormula,
|
||||
} from "#counter:counter/types";
|
||||
@@ -63,8 +64,10 @@ document.addEventListener("alpine:init", () => {
|
||||
},
|
||||
|
||||
checkFormulas() {
|
||||
// Try to find a formula.
|
||||
// A formula is found if all its elements are already in the basket
|
||||
const products = new Set(
|
||||
Object.keys(this.basket).map((i: string) => Number.parseInt(i, 10)),
|
||||
Object.values(this.basket).map((item: BasketItem) => item.product.productId),
|
||||
);
|
||||
const formula: ProductFormula = config.formulas.find((f: ProductFormula) => {
|
||||
return f.products.every((p: number) => products.has(p));
|
||||
@@ -72,22 +75,29 @@ document.addEventListener("alpine:init", () => {
|
||||
if (formula === undefined) {
|
||||
return;
|
||||
}
|
||||
// Now that the formula is found, remove the items composing it from the basket
|
||||
for (const product of formula.products) {
|
||||
const key = product.toString();
|
||||
const key = Object.entries(this.basket).find(
|
||||
([_, i]: [string, BasketItem]) => i.product.productId === product,
|
||||
)[0];
|
||||
this.basket[key].quantity -= 1;
|
||||
if (this.basket[key].quantity <= 0) {
|
||||
this.removeFromBasket(key);
|
||||
}
|
||||
}
|
||||
// Then add the result product of the formula to the basket
|
||||
const result = Object.values(config.products)
|
||||
.filter((item: CounterItem) => item.productId === formula.result)
|
||||
.reduce((acc, curr) => (acc.price.amount < curr.price.amount ? acc : curr));
|
||||
this.addToBasket(result.price.id, 1);
|
||||
this.alertMessage.display(
|
||||
interpolate(
|
||||
gettext("Formula %(formula)s applied"),
|
||||
{ formula: config.products[formula.result.toString()].name },
|
||||
{ formula: result.name },
|
||||
true,
|
||||
),
|
||||
{ success: true },
|
||||
);
|
||||
this.addToBasket(formula.result.toString(), 1);
|
||||
},
|
||||
|
||||
getBasketSize() {
|
||||
|
||||
Reference in new issue
Block a user