feat(plantare): reproduction-commitment data layer (schema v9)

The Plantare — a promise to reproduce seed and return some (data-model §2.7,
the seed-domain Pledge with return_kind=similar). NOT a sale. Local-first
v1: your honest ledger of commitments (from/to a person by name); the
bilateral signed cross-party form is a later social-layer phase.

- New Plantares table (SyncColumns): varietyId, direction (iReturn/owedToMe),
  counterparty, owedDescription, madeOn, dueBy, status (open/returned/
  forgiven), settledOn, note. schemaVersion 8 -> 9 with a guarded createTable
  migration; schema exported + migration-test helper regenerated.
- VarietyRepository: createPlantare / watchPlantares / watchPlantaresForVariety
  / setPlantareStatus / deletePlantare (soft-delete, CRDT-stamped).
- Included in backups + sync: InventorySnapshot + JSON codec (round-trips
  isDeleted) + exportInventory/exportForSync/importInventory, so commitments
  survive restore and replicate LWW like every other row.

Tests: v1..v8 -> v9 migration + fresh v9; repo create/list/settle/reopen/
delete; and a backup round-trip preserving a commitment. 13 green.

UI (detail section + Plantares screen) follows.
This commit is contained in:
vjrj 2026-07-11 02:01:25 +02:00
parent 0e41293de5
commit 81094f25a8
12 changed files with 5418 additions and 60 deletions

View file

@ -208,6 +208,28 @@ class InventoryJsonCodec {
'sortOrder': a.sortOrder,
},
],
'plantares': [
for (final p in snapshot.plantares)
{
..._syncMeta(
id: p.id,
createdAt: p.createdAt,
updatedAt: p.updatedAt,
lastAuthor: p.lastAuthor,
schemaRowVersion: p.schemaRowVersion,
isDeleted: p.isDeleted,
),
'varietyId': p.varietyId,
'direction': p.direction.name,
'counterparty': p.counterparty,
'owedDescription': p.owedDescription,
'madeOn': p.madeOn,
'dueBy': p.dueBy,
'status': p.status.name,
'settledOn': p.settledOn,
'note': p.note,
},
],
});
}
@ -418,6 +440,28 @@ class InventoryJsonCodec {
sortOrder: _int(m, 'sortOrder', fallback: 0),
);
}),
plantares: _rows(root, 'plantares', (m) {
return Plantare(
id: _string(m, 'id'),
createdAt: _int(m, 'createdAt'),
updatedAt: _string(m, 'updatedAt'),
lastAuthor: _string(m, 'lastAuthor'),
isDeleted: _bool(m, 'isDeleted'),
schemaRowVersion: _int(m, 'schemaRowVersion', fallback: 1),
varietyId: m['varietyId'] as String?,
direction:
_enumOr(PlantareDirection.values, m['direction'],
PlantareDirection.iReturn),
counterparty: m['counterparty'] as String?,
owedDescription: m['owedDescription'] as String?,
madeOn: _int(m, 'madeOn'),
dueBy: m['dueBy'] as int?,
status:
_enumOr(PlantareStatus.values, m['status'], PlantareStatus.open),
settledOn: m['settledOn'] as int?,
note: m['note'] as String?,
);
}),
);
}

View file

@ -30,6 +30,7 @@ class InventorySnapshot {
this.movements = const [],
this.parties = const [],
this.attachments = const [],
this.plantares = const [],
this.speciesNamesById = const {},
});
@ -42,6 +43,7 @@ class InventorySnapshot {
final List<Movement> movements;
final List<Party> parties;
final List<Attachment> attachments;
final List<Plantare> plantares;
/// speciesId scientificName, for the species referenced by [varieties].
final Map<String, String> speciesNamesById;

View file

@ -1707,6 +1707,80 @@ class VarietyRepository {
return id;
}
// --- Plantares: reproduction commitments (data-model §2.7) ----------------
/// Records a Plantare a promise to reproduce seed and return some (or a
/// promise made TO you). Returns the new commitment id.
Future<String> createPlantare({
required PlantareDirection direction,
String? varietyId,
String? counterparty,
String? owedDescription,
int? dueBy,
String? note,
}) async {
final (created, updated) = await _stamp();
final id = idGen.newId();
await _db.into(_db.plantares).insert(
PlantaresCompanion.insert(
id: id,
createdAt: created,
updatedAt: updated,
lastAuthor: nodeId,
direction: direction,
varietyId: Value(varietyId),
counterparty: Value(counterparty),
owedDescription: Value(owedDescription),
madeOn: created,
dueBy: Value(dueBy),
note: Value(note),
),
);
return id;
}
/// All live commitments, newest first (for the Plantares screen).
Stream<List<Plantare>> watchPlantares() => (_db.select(_db.plantares)
..where((p) => p.isDeleted.equals(false))
..orderBy([(p) => OrderingTerm.desc(p.madeOn)]))
.watch();
/// The commitments recorded against one variety.
Stream<List<Plantare>> watchPlantaresForVariety(String varietyId) =>
(_db.select(_db.plantares)
..where(
(p) => p.varietyId.equals(varietyId) & p.isDeleted.equals(false),
)
..orderBy([(p) => OrderingTerm.desc(p.madeOn)]))
.watch();
/// Moves a commitment to [status]; stamps the settle time for returned/
/// forgiven, clears it when reopened.
Future<void> setPlantareStatus(String id, PlantareStatus status) async {
final (now, updated) = await _stamp();
await (_db.update(_db.plantares)..where((p) => p.id.equals(id))).write(
PlantaresCompanion(
status: Value(status),
settledOn:
Value(status == PlantareStatus.open ? null : now),
updatedAt: Value(updated),
lastAuthor: Value(nodeId),
),
);
}
/// Soft-deletes a commitment (tombstone, so it syncs as a removal).
Future<void> deletePlantare(String id) async {
final (_, updated) = await _stamp();
await (_db.update(_db.plantares)..where((p) => p.id.equals(id))).write(
PlantaresCompanion(
isDeleted: const Value(true),
updatedAt: Value(updated),
lastAuthor: Value(nodeId),
),
);
}
/// Snapshots the live inventory (tombstones excluded) for the interchange
/// export data-model §7. Includes photo bytes; the JSON codec embeds them
/// as base64 and the CSV codec ignores them.
@ -1742,6 +1816,9 @@ class VarietyRepository {
attachments: await (_db.select(
_db.attachments,
)..where((a) => a.isDeleted.equals(false))).get(),
plantares: await (_db.select(
_db.plantares,
)..where((p) => p.isDeleted.equals(false))).get(),
);
}
@ -1764,6 +1841,7 @@ class VarietyRepository {
conditionChecks: await _db.select(_db.conditionChecks).get(),
movements: await _db.select(_db.movements).get(),
parties: await _db.select(_db.parties).get(),
plantares: await _db.select(_db.plantares).get(),
// attachments intentionally omitted photos don't ride the sync wire.
);
}
@ -1856,6 +1934,14 @@ class VarietyRepository {
updatedAtOf: (r) => r.updatedAt,
reconciler: reconciler,
);
summary += await _importMutableRows(
table: _db.plantares,
idColumn: _db.plantares.id,
rows: snapshot.plantares,
idOf: (r) => r.id,
updatedAtOf: (r) => r.updatedAt,
reconciler: reconciler,
);
summary += await _importMovements(snapshot.movements, reconciler);
});
@ -1868,6 +1954,7 @@ class VarietyRepository {
for (final c in snapshot.conditionChecks) c.updatedAt,
for (final p in snapshot.parties) p.updatedAt,
for (final a in snapshot.attachments) a.updatedAt,
for (final pl in snapshot.plantares) pl.updatedAt,
]);
if (maxIncoming != null) {
_clock = _clock.receiveEvent(maxIncoming, _now());