tane/apps/app_seeds/lib/services/offer_mapper.dart
vjrj 7fd87e6e62 feat(block2): offers logic layer — OffersCubit + OfferMapper (no UI yet)
The testable core of the offers UI slice, transport-agnostic:
- OfferMapper: pure app-sharing-intent (OfferStatus) -> network Offer/OfferType
  mapping; keeps price only for sales, refuses to publish private lots.
- OffersCubit: discover(geohash) streams results; publish() reports the verdict;
  depends on the OfferTransport interface (fake in tests) and degrades
  gracefully offline (null transport = social layer unavailable, never throws).

8 tests green (fake in-memory transport, no relay). Screen/routing/i18n come
next — they need two product decisions (coarse-area capture, relay list).
2026-07-10 02:49:06 +02:00

53 lines
1.9 KiB
Dart

import 'package:commons_core/commons_core.dart';
import '../db/enums.dart';
/// Turns a locally-shared lot into a publishable [Offer]. Pure — no I/O — so it
/// is easy to test and keeps the privacy seam explicit: only the fields passed
/// here ever reach the network (never the whole lot/inventory, never an exact
/// address; the area is a coarse geohash chosen by the user).
class OfferMapper {
const OfferMapper._();
/// Maps the app's local sharing intent ([OfferStatus]) to the network offer
/// type. `private` lots are never published.
static OfferType offerTypeFor(OfferStatus sharing) => switch (sharing) {
OfferStatus.shared => OfferType.gift,
OfferStatus.exchange => OfferType.exchange,
OfferStatus.sell => OfferType.sale,
OfferStatus.private =>
throw ArgumentError('private lots are not published to the network'),
};
/// Builds the [Offer] for a shared lot. [lotId] gives the offer a stable,
/// addressable id (re-publishing updates in place). [areaGeohash] is the
/// user's coarse area — the transport coarsens it further on the wire.
static Offer fromSharedLot({
required String lotId,
required String authorPubkeyHex,
required String summary,
required OfferStatus sharing,
required String areaGeohash,
String? category,
num? priceAmount,
String? priceCurrency,
String? exchangeTerms,
String? imageUrl,
DateTime? expiresAt,
}) {
final type = offerTypeFor(sharing);
return Offer(
id: lotId,
authorPubkeyHex: authorPubkeyHex,
summary: summary,
type: type,
approxGeohash: areaGeohash,
category: category,
priceAmount: type == OfferType.sale ? priceAmount : null,
priceCurrency: type == OfferType.sale ? priceCurrency : null,
exchangeTerms: type == OfferType.exchange ? exchangeTerms : null,
imageUrl: imageUrl,
expiresAt: expiresAt,
);
}
}