snapshots method

Future<List<DVEntitlementSnapshot>> snapshots(
  1. Object customer
)

What a device may hold for customer: each entitlement in force, with the moment it stops without a fresh sync.

Implementation

Future<List<DVEntitlementSnapshot>> snapshots(Object customer) async {
  final DateTime now = _now;
  final Map<String, DateTime> ends = <String, DateTime>{};
  for (final DVPurchaseGrant grant
      in await ledger.forCustomer(dvBillingCustomerKey(customer))) {
    if (!grant.activeAt(now)) continue;
    final DateTime end = grant.notAfter ?? now.add(perpetualSnapshotLifetime);
    for (final String id in _entitlementIdsOf(grant)) {
      final DateTime? held = ends[id];
      if (held == null || end.isAfter(held)) ends[id] = end;
    }
  }
  // What the gateway holds, for a web purchase that unlocks the same
  // entitlement on the phone. It reports no period end, so it lasts as
  // long as a purchase that never expires: until the next sync.
  for (final Entitlement entitlement in _entitlements.values) {
    if (ends.containsKey(entitlement.id)) continue;
    if (await _gatewayHolds(customer, entitlement)) {
      ends[entitlement.id] = now.add(perpetualSnapshotLifetime);
    }
  }
  final List<String> ids = ends.keys.toList()..sort();
  return <DVEntitlementSnapshot>[
    for (final String id in ids)
      DVEntitlementSnapshot(entitlement: id, notAfter: ends[id]!),
  ];
}