restoreSale method
Gives back what revokeSale took, inside context. Returns what was
granted.
Implementation
Future<Set<Entitlement>> restoreSale(DVContext context, String saleId) async {
final DVSale? sale = await ledger.find(saleId);
if (sale == null) {
throw ArgumentError.value(saleId, 'saleId', 'is not a sale');
}
final DateTime? revokedAt = sale.revokedAt;
if (revokedAt == null) return const <Entitlement>{};
await ledger.setRevokedAt(saleId, null);
context.compensate(() => ledger.setRevokedAt(saleId, revokedAt));
final DVEntitlementGrants? granter = grants;
if (granter == null || sale.entitlements.isEmpty) {
return const <Entitlement>{};
}
final Set<Entitlement> restored = <Entitlement>{
for (final String id in sale.entitlements) Entitlement(id),
};
await granter.grant(sale.customerKey, restored);
context.compensate(() async {
final Set<Entitlement> unheld =
await _notHeldElsewhere(sale.customerKey, saleId, sale.entitlements);
if (unheld.isNotEmpty) await granter.revoke(sale.customerKey, unheld);
});
return restored;
}