Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Fix Total Value cross-currency sum and overlong serial 500

Omega (QA)7f669842946c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1ecec0c..cc4b399 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,10 @@
- _Files: `frontend/js/components/tagManager.js`_
### Fixed
+- **Total Value card no longer sums across currencies (issue #192):** the `/statistics` and `/statistics/global` endpoints summed `purchase_price` across all currencies into one number while the card displays the user's default-currency symbol. Both now filter to the requesting user's default currency (via a backend symbol→code map mirroring the frontend's `getCurrencyCode()`).
+ - _Files: `backend/statistics_routes.py`
+- **Overlong serial numbers now return 400 instead of 500:** serials over 255 characters crashed with an unhandled DB error on warranty create and update; both now validate and return `serial_number_too_long`.
+ - _Files: `backend/warranties_routes.py`
- **CSV import now validates overlong product names and tolerates short rows:** importing a CSV with a product name over 255 characters previously leaked the raw Postgres error (`value too long for type character varying(255)`); the row is now rejected with the clean `ProductName must be 255 characters or less.` message matching the REST API validation. Rows with fewer cells than headers previously crashed with `Internal processing error: 'NoneType' object has no attribute 'strip'` (DictReader fills missing cells with `None`); all field extractions now guard against `None`.
- _Files: `backend/warranties_routes.py`
- **Dead mobile menu button on most pages fixed:** every page renders the hamburger `.mobile-menu-toggle` button and the `#mobileMenuPanel` markup, but only settings-new.html loaded `mobile-menu.js` — tapping the menu on index, status, register, about, and the password-reset pages did nothing. The script is now included on all pages that render the toggle. Found by a dedicated mobile QA pass (390px viewport, touch): menu opens/closes via button, overlay, and Escape on every page; nav links navigate; no horizontal overflow; no new JS errors.
diff --git a/backend/statistics_routes.py b/backend/statistics_routes.py
index 0ce6a68..fd6b90c 100644
--- a/backend/statistics_routes.py
+++ b/backend/statistics_routes.py
@@ -19,6 +19,27 @@ statistics_bp = Blueprint('statistics_bp', __name__)
# Set up logging
logger = logging.getLogger(__name__)
+# Maps the user's currency_symbol preference to the ISO code stored on
+# warranties. Mirrors getCurrencyCode() in frontend/js/lib/currency.js.
+# Issue #192: the Total Value card sums only the default currency.
+CURRENCY_SYMBOL_TO_CODE = {
+ '$': 'USD', '\u20ac': 'EUR', '\u00a3': 'GBP', '\u00a5': 'JPY',
+ '\u20b9': 'INR', '\u20a9': 'KRW', 'CHF': 'CHF', 'C$': 'CAD', 'A$': 'AUD',
+ 'kr': 'SEK', 'z\u0142': 'PLN', 'K\u010d': 'CZK', 'Ft': 'HUF',
+ '\u20bd': 'RUB', 'R$': 'BRL', '\u20aa': 'ILS', '\u20ba': 'TRY',
+ 'NZ$': 'NZD',
+}
+
+def get_user_currency_code(cur, user_id):
+ """Return the ISO currency code for the user's default currency_symbol."""
+ try:
+ cur.execute("SELECT currency_symbol FROM user_preferences WHERE user_id = %s", (user_id,))
+ row = cur.fetchone()
+ symbol = (row[0] if row and row[0] else '$')
+ except Exception:
+ symbol = '$'
+ return CURRENCY_SYMBOL_TO_CODE.get(symbol, 'USD')
+
# Helper function
def convert_decimals(obj):
"""Recursively convert Decimal objects to float in dicts/lists for JSON serialization."""
@@ -90,10 +111,12 @@ def get_statistics():
archived_count = cur.fetchone()[0]
logger.info(f"Archived warranties: {archived_count}")
- # Get total purchase value (excluding archived; issue #192)
- cur.execute(f"SELECT COALESCE(SUM(w.purchase_price), 0) {from_clause} {where_clause} AND w.archived_at IS NULL", params)
+ # Get total purchase value (excluding archived; issue #192:
+ # only the user's default currency, not a cross-currency sum)
+ user_currency = get_user_currency_code(cur, user_id)
+ cur.execute(f"SELECT COALESCE(SUM(w.purchase_price), 0) {from_clause} {where_clause} AND w.archived_at IS NULL AND w.currency = %s", params + [user_currency])
total_value = cur.fetchone()[0]
- logger.info(f"Total purchase value: {total_value}")
+ logger.info(f"Total purchase value ({user_currency}): {total_value}")
# Get active count (includes lifetime, excludes archived)
cur.execute(f"SELECT COUNT(*) {from_clause} {where_clause} {active_where if where_clause else 'WHERE'} (w.is_lifetime = TRUE OR w.expiration_date >= %s) AND w.archived_at IS NULL", params + [today])
@@ -298,9 +321,12 @@ def get_global_statistics():
cur.execute("SELECT COUNT(*) FROM warranties w WHERE w.archived_at IS NOT NULL")
archived_count = cur.fetchone()[0]
- # Get total purchase value (excluding archived; issue #192)
- cur.execute("SELECT COALESCE(SUM(w.purchase_price), 0) FROM warranties w WHERE w.archived_at IS NULL")
+ # Get total purchase value (excluding archived; issue #192:
+ # only the requesting user's default currency, not a cross-currency sum)
+ user_currency = get_user_currency_code(cur, request.user['id'])
+ cur.execute("SELECT COALESCE(SUM(w.purchase_price), 0) FROM warranties w WHERE w.archived_at IS NULL AND w.currency = %s", (user_currency,))
total_value = cur.fetchone()[0]
+ logger.info(f"Global total purchase value ({user_currency}): {total_value}")
# Get active count (includes lifetime, excludes archived)
cur.execute("SELECT COUNT(*) FROM warranties w WHERE (w.is_lifetime = TRUE OR w.expiration_date >= %s) AND w.archived_at IS NULL", (today,))
diff --git a/backend/warranties_routes.py b/backend/warranties_routes.py
index 1563642..4453928 100644
--- a/backend/warranties_routes.py
+++ b/backend/warranties_routes.py
@@ -234,6 +234,11 @@ def add_warranty():
if len(request.form.get('product_name')) > 255:
return jsonify({"error": "Product name must be 255 characters or less",
"code": "product_name_too_long"}), 400
+
+ for serial_number in request.form.getlist('serial_numbers[]'):
+ if len(serial_number) > 255:
+ return jsonify({"error": "Serial number must be 255 characters or less",
+ "code": "serial_number_too_long"}), 400
if not request.form.get('purchase_date'):
return jsonify({"error": "Purchase date is required"}), 400
@@ -688,6 +693,10 @@ def update_warranty(warranty_id):
if len(request.form.get('product_name')) > 255:
return jsonify({"error": "Product name must be 255 characters or less",
"code": "product_name_too_long"}), 400
+ for serial_number in request.form.getlist('serial_numbers[]'):
+ if len(serial_number) > 255:
+ return jsonify({"error": "Serial number must be 255 characters or less",
+ "code": "serial_number_too_long"}), 400
if not request.form.get('purchase_date'):
return jsonify({"error": "Purchase date is required"}), 400
logger.info(f"Received update request for warranty {warranty_id}")