mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 21:02:18 +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
135 lines
5.2 KiB
Dart
135 lines
5.2 KiB
Dart
/// Fetches the day's catering dishes and banks them.
|
|
///
|
|
/// The Dart mirror of `diet_guard/_kuchnia_import.py`, and the module's single
|
|
/// fail-closed boundary: every [KuchniaError] is turned into a reason string
|
|
/// here, so no caller can be broken by a catering outage.
|
|
///
|
|
/// **Nothing is ever logged from here.** A delivered meal is not an eaten
|
|
/// meal: dishes land in the *curated* bank and the user still taps to log
|
|
/// each one. If the import logged automatically, the gate would satisfy its
|
|
/// own checkpoint from a delivery note, and the log would record what the
|
|
/// courier dropped off rather than what was eaten -- skipped, shared and
|
|
/// binned meals included.
|
|
///
|
|
/// Imports go **only** to the curated bank. The derived bank is rebuilt from
|
|
/// the log on every meal, so anything written there would vanish at the next
|
|
/// entry.
|
|
///
|
|
/// The idempotency guard is load-bearing and easy to get wrong.
|
|
/// `addManualEntry` restamps `editedAt` unconditionally and the sync merge
|
|
/// derives each record's clock from it -- so calling it for unchanged dishes
|
|
/// would republish the *entire* curated bank to every peer on every refresh.
|
|
/// [bankDishes] therefore compares the nutritional fields first and writes
|
|
/// only what actually differs.
|
|
library;
|
|
|
|
import 'package:diet_guard_app/models/food_bank_record.dart';
|
|
import 'package:diet_guard_app/models/kuchnia_dish.dart';
|
|
import 'package:diet_guard_app/services/foodbank_service.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_client.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_credential_service.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_errors.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_orders.dart';
|
|
|
|
/// The result of one refresh: the dishes, or why there are none.
|
|
class KuchniaRefresh {
|
|
/// Creates a refresh result.
|
|
const KuchniaRefresh({this.dishes = const [], this.reason});
|
|
|
|
/// The day's dishes, empty on failure or when nothing was delivered.
|
|
final List<KuchniaDish> dishes;
|
|
|
|
/// Why the panel could not be read, or null on success.
|
|
///
|
|
/// Callers surface this and carry on; it is never thrown.
|
|
final String? reason;
|
|
|
|
/// True when the fetch succeeded, whether or not anything was delivered.
|
|
bool get ok => reason == null;
|
|
}
|
|
|
|
/// Returns the curated-bank record for [dish].
|
|
FoodBankRecord dishToRecord(KuchniaDish dish) => FoodBankRecord(
|
|
desc: dish.name,
|
|
kcal: dish.kcal,
|
|
proteinG: dish.proteinG,
|
|
carbsG: dish.carbsG,
|
|
fatG: dish.fatG,
|
|
grams: dish.grams,
|
|
// The bank's count ranks foods by how often they were *eaten*, and a
|
|
// delivered dish has not been eaten yet.
|
|
count: 0,
|
|
);
|
|
|
|
/// True when [existing] already says exactly what [dish] says.
|
|
///
|
|
/// Compares the nutritional fields only. `editedAt` is excluded deliberately:
|
|
/// it is the edit stamp, not part of the record's meaning, and including it
|
|
/// would make every comparison differ -- which is the republish flood this
|
|
/// guard exists to prevent.
|
|
///
|
|
/// Numeric comparison, not string: `270 == 270.0` must be true here exactly as
|
|
/// it is in Python's `_matches`.
|
|
bool bankRecordMatches(FoodBankRecord existing, KuchniaDish dish) =>
|
|
existing.desc == dish.name &&
|
|
existing.kcal == dish.kcal &&
|
|
existing.proteinG == dish.proteinG &&
|
|
existing.carbsG == dish.carbsG &&
|
|
existing.fatG == dish.fatG &&
|
|
existing.grams == dish.grams;
|
|
|
|
/// Adds [dishes] to the curated bank, skipping ones already banked unchanged.
|
|
///
|
|
/// Returns how many records were actually written -- which is what the tests
|
|
/// assert on, because "the entry exists" passes while the guard misbehaves.
|
|
Future<int> bankDishes(List<KuchniaDish> dishes) async {
|
|
final bank = await FoodBankService.instance.readManualBank();
|
|
var written = 0;
|
|
for (final dish in dishes) {
|
|
final existing = bank[dish.bankKey];
|
|
if (existing != null && bankRecordMatches(existing, dish)) continue;
|
|
await FoodBankService.instance.addManualEntry(dishToRecord(dish));
|
|
written++;
|
|
}
|
|
return written;
|
|
}
|
|
|
|
/// Fetches [day]'s dishes and banks them, never throwing.
|
|
///
|
|
/// Returns a reason string instead of raising, so a catering outage cannot
|
|
/// break the caller. On web it returns [kuchniaWebUnsupportedReason] without
|
|
/// touching the network: the panel sends no CORS headers, so the browser would
|
|
/// refuse the request anyway. That check comes first, before anything
|
|
/// platform-shaped runs.
|
|
Future<KuchniaRefresh> refreshDelivery(
|
|
DateTime day, {
|
|
KuchniaSession Function(String username, String password)? sessionFactory,
|
|
}) async {
|
|
if (!kuchniaFetchSupported) {
|
|
return const KuchniaRefresh(reason: kuchniaWebUnsupportedReason);
|
|
}
|
|
if (!KuchniaCredentialService.isConfigured) {
|
|
return const KuchniaRefresh(
|
|
reason: 'no catering credentials — add them in Settings',
|
|
);
|
|
}
|
|
|
|
final session =
|
|
(sessionFactory ??
|
|
(username, password) =>
|
|
KuchniaSession(username: username, password: password))(
|
|
KuchniaCredentialService.username,
|
|
KuchniaCredentialService.password,
|
|
);
|
|
List<KuchniaDish> dishes;
|
|
try {
|
|
dishes = await fetchDishes(session, day);
|
|
} on KuchniaError catch (error) {
|
|
return KuchniaRefresh(reason: error.message);
|
|
} finally {
|
|
session.close();
|
|
}
|
|
await bankDishes(dishes);
|
|
return KuchniaRefresh(dishes: dishes);
|
|
}
|