mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 04:12:19 +01:00
The phone now walks the panel on its own -- login, active order, delivery, menu -- parses the dishes and banks them, so the feature works with the PC switched off. Read-only: nothing here logs a meal. `package:http` has no cookie jar, so the `SESSION` cookie is read off the login response and replayed by hand. `Response.headers` folds duplicate `set-cookie` values into one comma-joined string, so the parser splits on both `,` and `;` and looks for the named cookie rather than assuming the header holds only it -- tested against both orderings and against a panel that sets no session cookie at all. Android-only, as decided: the panel sends no CORS headers, so a browser cannot call it. `refreshDelivery` checks `kIsWeb` first and returns the unsupported reason without touching the network. No `dart:io` anywhere -- `package:http` compiles for web, it is the panel that refuses, not the toolchain, so this needs no `_io`/`_web` seam. The idempotency guard is ported with its teeth: `addManualEntry` restamps `editedAt` unconditionally, so re-banking an unchanged dish republishes the whole curated bank to every peer. `bankDishes` compares the nutritional fields first and returns a write *count*, which is what the tests assert on -- "the entry exists" passes while this misbehaves. The comparison is numeric, not string, so `270 == 270.0` is a match exactly as in Python's `_matches`. Every failure becomes a reason string, never a throw, so a catering outage cannot break the caller: a rejected login, a delivery whose dishes all fail the unit check, and missing credentials each have a test. One test bug worth noting: the assertion that nothing is logged was missing an `await` and so was comparing a Future against `isEmpty` -- the single most important guarantee in this file was passing vacuously until the split forced it to run. Test files split three ways for the 250-line cap; block counts checked against the pre-split file (16 either side), not just a green suite. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
81 lines
3.2 KiB
Dart
81 lines
3.2 KiB
Dart
/// Walks the panel's endpoints to a day's delivered dishes.
|
|
///
|
|
/// The Dart mirror of `diet_guard/_kuchnia_orders.py`. Three calls, and the
|
|
/// shape of the walk is not what the endpoint names suggest:
|
|
///
|
|
/// 1. `company/customer/order/active-ids` -> `[orderId]`.
|
|
/// 2. `company/customer/order/{orderId}` -> the order, which **embeds every
|
|
/// delivery** with its date. No enumeration call is needed, but the
|
|
/// embedded `deliveryMeals` carry ids only -- no names, no macros.
|
|
/// 3. `company/general/menus/delivery/{deliveryId}/new` -> the day's actual
|
|
/// menu, with names and per-portion nutrition.
|
|
///
|
|
/// Step 3 is the important correction: the obvious-looking
|
|
/// `.../deliveries/{id}/details` **404s** (and 400s when keyed by date),
|
|
/// confirmed against three separate delivery days. The menu endpoint is keyed
|
|
/// by the opaque `deliveryId`, never by the date.
|
|
library;
|
|
|
|
import 'package:diet_guard_app/models/kuchnia_dish.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_client.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_errors.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_parse.dart';
|
|
|
|
/// Returns the first active order id, or null when there is no active order.
|
|
Object? activeOrderId(Object? payload) {
|
|
if (payload is! List || payload.isEmpty) return null;
|
|
return payload.first;
|
|
}
|
|
|
|
/// Returns the delivery id whose date is [wanted] (an ISO `YYYY-MM-DD`).
|
|
///
|
|
/// Null when that day has no delivery, or only a cancelled one -- a `deleted`
|
|
/// delivery still appears in the list.
|
|
Object? deliveryIdFor(Object? payload, String wanted) {
|
|
if (payload is! Map) return null;
|
|
final deliveries = payload['deliveries'];
|
|
if (deliveries is! List) return null;
|
|
for (final delivery in deliveries) {
|
|
if (delivery is! Map) continue;
|
|
if (delivery['deleted'] == true) continue;
|
|
if ('${delivery['date']}' == wanted) return delivery['deliveryId'];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Formats [day] as the ISO `YYYY-MM-DD` the panel keys deliveries by.
|
|
String isoDay(DateTime day) =>
|
|
'${day.year.toString().padLeft(4, '0')}-'
|
|
'${day.month.toString().padLeft(2, '0')}-'
|
|
'${day.day.toString().padLeft(2, '0')}';
|
|
|
|
/// Returns the dishes delivered on [day], empty when nothing is delivered.
|
|
///
|
|
/// Throws [KuchniaError] when the panel cannot be read, or when a delivery
|
|
/// exists but nothing survived parsing -- either the menu is not published yet
|
|
/// or its units failed the consistency check. Both are "no data", but they are
|
|
/// worth distinguishing from "no delivery" in the caller's message.
|
|
Future<List<KuchniaDish>> fetchDishes(
|
|
KuchniaSession session,
|
|
DateTime day,
|
|
) async {
|
|
final orderId = activeOrderId(await session.getJson(
|
|
'company/customer/order/active-ids',
|
|
));
|
|
if (orderId == null) return const [];
|
|
|
|
final order = await session.getJson('company/customer/order/$orderId');
|
|
final wanted = isoDay(day);
|
|
final deliveryId = deliveryIdFor(order, wanted);
|
|
if (deliveryId == null) return const [];
|
|
|
|
final menu = await session.getJson(
|
|
'company/general/menus/delivery/$deliveryId/new',
|
|
);
|
|
final dishes = parseMenu(menu);
|
|
if (dishes.isEmpty) {
|
|
throw KuchniaError('catering menu for $wanted has no usable dishes');
|
|
}
|
|
return dishes;
|
|
}
|