Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(code_push_protocol): add subscription object to user #339

Merged
merged 9 commits into from
Apr 24, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export 'patch.dart';
export 'patch_artifact.dart';
export 'release.dart';
export 'release_artifact.dart';
export 'subscription.dart';
export 'user.dart';
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man. US Cents I assume. :) Someone is going to write an amazing typed "money" object someday. 🤣

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming usd for now.

Stripe specifies currency on the individual price objects (https://stripe.com/docs/api/prices/object#price_object-currency), so we can get fancier if/when we need to.

final int cost;

/// When the subscription will be renewed or expire.
@TimestampConverter()
final DateTime paidThroughDate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is midnight UTC on that date?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not specified, but looking at some of the live subscriptions, it seems to be purchase_time + Duration(days: 30)


/// Whether this subscription will renew on [paidThroughDate].
final bool willRenew;

/// Whether this subscription is currently active.
bool get isActive => paidThroughDate.isAfter(DateTime.now());
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down