[FIX] website_sale_aplicoop: invalidate stale carts on closed group orders

This commit is contained in:
snt 2026-03-30 17:39:15 +02:00
parent 89c008441e
commit c345699bf4
4 changed files with 329 additions and 23 deletions

View file

@ -56,33 +56,35 @@
console.log("[groupOrderShop] Translations loaded from server");
self.labels = i18nManager.getAll();
self._loadCart();
self._checkConfirmationMessage();
self._initializeTooltips();
self._checkGroupOrderStatus(function () {
self._loadCart();
self._checkConfirmationMessage();
self._initializeTooltips();
// Update display if there is cart-items-container
if (cartContainer) {
self._updateCartDisplay();
}
// Update display if there is cart-items-container
if (cartContainer) {
self._updateCartDisplay();
}
// Check if we're loading from history
var storageKey = "load_from_history_" + self.orderId;
if (sessionStorage.getItem(storageKey)) {
// Load items from historical order
self._loadFromHistory();
} else {
// Auto-load draft order on page load (silent mode)
self._autoLoadDraftOnInit();
}
// Check if we're loading from history
var storageKey = "load_from_history_" + self.orderId;
if (sessionStorage.getItem(storageKey)) {
// Load items from historical order
self._loadFromHistory();
} else {
// Auto-load draft order on page load (silent mode)
self._autoLoadDraftOnInit();
}
// Emit event when fully initialized
document.dispatchEvent(
new CustomEvent("groupOrderShopReady", {
detail: { labels: self.labels },
})
);
// Emit event when fully initialized
document.dispatchEvent(
new CustomEvent("groupOrderShopReady", {
detail: { labels: self.labels },
})
);
console.log("[groupOrderShop] ✓ Initialization complete");
console.log("[groupOrderShop] ✓ Initialization complete");
});
})
.catch(function (error) {
console.error("[groupOrderShop] Failed to initialize translations:", error);
@ -385,6 +387,74 @@
console.log("Verification - immediately read back:", localStorage.getItem(cartKey));
},
_clearCurrentOrderCartSilently: function () {
var cartKey = "eskaera_" + this.orderId + "_cart";
localStorage.removeItem(cartKey);
this.cart = {};
console.log("[groupOrderShop] Cart cleared silently for order:", this.orderId);
},
_isClosedOrderResponse: function (xhr) {
if (!xhr) {
return false;
}
if (xhr.status !== 400 && xhr.status !== 403) {
return false;
}
try {
var errorData = JSON.parse(xhr.responseText || "{}");
return (
errorData.code === "group_order_not_open" ||
errorData.action === "clear_cart" ||
errorData.order_state === "closed" ||
errorData.order_state === "cancelled"
);
} catch (e) {
return false;
}
},
_checkGroupOrderStatus: function (callback) {
var self = this;
var done = function () {
if (typeof callback === "function") {
callback();
}
};
var xhr = new XMLHttpRequest();
xhr.open("POST", "/eskaera/check-status", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (xhr.status !== 200) {
done();
return;
}
try {
var data = JSON.parse(xhr.responseText || "{}");
if (data && data.is_open === false) {
self._clearCurrentOrderCartSilently();
}
} catch (e) {
console.warn("[groupOrderShop] check-status parse error", e);
}
done();
};
xhr.onerror = function () {
done();
};
xhr.send(
JSON.stringify({
order_id: this.orderId,
})
);
},
_showNotification: function (message, type, duration) {
// type: 'success', 'error', 'warning', 'info'
// duration: milliseconds (default 8000 = 8 seconds)
@ -1406,6 +1476,11 @@
);
}
} else {
if (self._isClosedOrderResponse(xhr)) {
self._clearCurrentOrderCartSilently();
self._updateCartDisplay();
return;
}
try {
var errorData = JSON.parse(xhr.responseText);
self._showNotification(
@ -1525,6 +1600,11 @@
);
}
} else {
if (self._isClosedOrderResponse(xhr)) {
self._clearCurrentOrderCartSilently();
self._updateCartDisplay();
return;
}
try {
var errorData = JSON.parse(xhr.responseText);
self._showNotification(
@ -1603,6 +1683,11 @@
self._showNotification("Error processing response", "danger");
}
} else {
if (self._isClosedOrderResponse(xhr)) {
self._clearCurrentOrderCartSilently();
self._updateCartDisplay();
return;
}
try {
var errorData = JSON.parse(xhr.responseText);
console.error("HTTP error:", xhr.status, errorData);
@ -1712,6 +1797,11 @@
);
}
} else {
if (self._isClosedOrderResponse(xhr)) {
self._clearCurrentOrderCartSilently();
self._updateCartDisplay();
return;
}
try {
var errorData = JSON.parse(xhr.responseText);
console.error("HTTP error:", xhr.status, errorData);