feat(code_push_protocol): add subscription object to user (#339)
This commit is contained in:
@@ -6,4 +6,5 @@ export 'patch.dart';
|
||||
export 'patch_artifact.dart';
|
||||
export 'release.dart';
|
||||
export 'release_artifact.dart';
|
||||
export 'subscription.dart';
|
||||
export 'user.dart';
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
|
||||
|
||||
part 'subscription.g.dart';
|
||||
|
||||
/// {@template subscription}
|
||||
/// A user's Shorebird subscription.
|
||||
/// {@endtemplate}
|
||||
@JsonSerializable()
|
||||
class Subscription {
|
||||
/// {@macro subscription}
|
||||
Subscription({
|
||||
required this.cost,
|
||||
required this.paidThroughDate,
|
||||
required this.willRenew,
|
||||
});
|
||||
|
||||
/// Creates a [Subscription] from a JSON object.
|
||||
factory Subscription.fromJson(Json json) => _$SubscriptionFromJson(json);
|
||||
|
||||
/// Converts a [Subscription] to a JSON object.
|
||||
Json toJson() => _$SubscriptionToJson(this);
|
||||
|
||||
/// Billing rate, in cents.
|
||||
final int cost;
|
||||
|
||||
/// When the subscription will be renewed or expire.
|
||||
@TimestampConverter()
|
||||
final DateTime paidThroughDate;
|
||||
|
||||
/// Whether this subscription will renew on [paidThroughDate].
|
||||
final bool willRenew;
|
||||
|
||||
/// Whether this subscription is currently active.
|
||||
bool get isActive => paidThroughDate.isAfter(DateTime.now());
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
// ignore_for_file: implicit_dynamic_parameter, require_trailing_commas, cast_nullable_to_non_nullable, lines_longer_than_80_chars
|
||||
|
||||
part of 'subscription.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Subscription _$SubscriptionFromJson(Map<String, dynamic> json) =>
|
||||
$checkedCreate(
|
||||
'Subscription',
|
||||
json,
|
||||
($checkedConvert) {
|
||||
final val = Subscription(
|
||||
cost: $checkedConvert('cost', (v) => v as int),
|
||||
paidThroughDate: $checkedConvert('paid_through_date',
|
||||
(v) => const TimestampConverter().fromJson(v as int)),
|
||||
willRenew: $checkedConvert('will_renew', (v) => v as bool),
|
||||
);
|
||||
return val;
|
||||
},
|
||||
fieldKeyMap: const {
|
||||
'paidThroughDate': 'paid_through_date',
|
||||
'willRenew': 'will_renew'
|
||||
},
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SubscriptionToJson(Subscription instance) =>
|
||||
<String, dynamic>{
|
||||
'cost': instance.cost,
|
||||
'paid_through_date':
|
||||
const TimestampConverter().toJson(instance.paidThroughDate),
|
||||
'will_renew': instance.willRenew,
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group(Subscription, () {
|
||||
test('can be (de)serialized', () {
|
||||
final subscription = Subscription(
|
||||
cost: 100,
|
||||
paidThroughDate: DateTime.now(),
|
||||
willRenew: true,
|
||||
);
|
||||
expect(
|
||||
Subscription.fromJson(subscription.toJson()).toJson(),
|
||||
equals(subscription.toJson()),
|
||||
);
|
||||
});
|
||||
|
||||
group('isActive', () {
|
||||
test('returns true if paidThroughDate is in the future', () {
|
||||
final subscription = Subscription(
|
||||
cost: 100,
|
||||
paidThroughDate: DateTime.now().add(const Duration(days: 1)),
|
||||
willRenew: true,
|
||||
);
|
||||
expect(subscription.isActive, isTrue);
|
||||
});
|
||||
|
||||
test('returns false if paidThroughDate is in the past', () {
|
||||
final subscription = Subscription(
|
||||
cost: 100,
|
||||
paidThroughDate: DateTime.now().subtract(const Duration(days: 1)),
|
||||
willRenew: true,
|
||||
);
|
||||
expect(subscription.isActive, isFalse);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -4,7 +4,10 @@ import 'package:test/test.dart';
|
||||
void main() {
|
||||
group('User', () {
|
||||
test('can be (de)serialized', () {
|
||||
const user = User(id: 1, email: 'test@shorebird.dev');
|
||||
const user = User(
|
||||
id: 1,
|
||||
email: 'test@shorebird.dev',
|
||||
);
|
||||
expect(
|
||||
User.fromJson(user.toJson()).toJson(),
|
||||
equals(user.toJson()),
|
||||
|
||||
Reference in New Issue
Block a user