chore(code_push_protocol): remove Subscription model from protocol (#1353)

This commit is contained in:
Felix Angelov
2023-10-03 14:42:30 -05:00
committed by GitHub
parent 8f4fcc48ad
commit 59ba119657
12 changed files with 1 additions and 269 deletions
@@ -1,8 +1,6 @@
/// The Shorebird CodePush Protocol
library shorebird_code_push_protocol;
export 'package:money2/money2.dart' show Currency, Money;
export 'src/converters/converters.dart';
export 'src/messages/messages.dart';
export 'src/models/models.dart';
@@ -1,2 +0,0 @@
export 'money_converter.dart';
export 'timestamp_converter.dart';
@@ -1,21 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:money2/money2.dart';
/// The US dollar.
Currency get usd => Currency.create('USD', 2);
/// {@template money_converter}
/// Converts between [Money] and [int].
/// {@endtemplate}
// TODO(bryanoltman): change this to use String as the transport type the next
// time we make a breaking change to the API.
class MoneyConverter implements JsonConverter<Money, int> {
/// {@macro money_converter}
const MoneyConverter();
@override
Money fromJson(int cents) => Money.fromIntWithCurrency(cents, usd);
@override
int toJson(Money money) => money.minorUnits.toInt();
}
@@ -1,16 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
/// {@template timestamp_converter}
/// Converts between Unix timestamps and [DateTime].
/// {@endtemplate}
class TimestampConverter implements JsonConverter<DateTime, int> {
/// {@macro timestamp_converter}
const TimestampConverter();
@override
DateTime fromJson(int timestamp) =>
DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
@override
int toJson(DateTime dateTime) => dateTime.millisecondsSinceEpoch ~/ 1000;
}
@@ -9,6 +9,4 @@ export 'release.dart';
export 'release_artifact.dart';
export 'release_platform.dart';
export 'release_status.dart';
export 'shorebird_plan.dart';
export 'subscription.dart';
export 'user.dart';
@@ -1,43 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'shorebird_plan.g.dart';
/// {@template shorebird_plan}
/// A Shorebird plan.
/// {@endtemplate}
@JsonSerializable()
class ShorebirdPlan {
/// {@macro shorebird_plan}
const ShorebirdPlan({
required this.name,
required this.monthlyCost,
required this.currency,
required this.patchInstallLimit,
this.maxTeamSize,
});
/// Creates a [ShorebirdPlan] from a JSON object.
factory ShorebirdPlan.fromJson(Json json) => _$ShorebirdPlanFromJson(json);
/// Converts a [ShorebirdPlan] to a JSON object.
Json toJson() => _$ShorebirdPlanToJson(this);
/// The name of the plan.
final String name;
/// Monthly billing rate.
@MoneyConverter()
final Money monthlyCost;
/// The currency of the plan (e.g. USD, CAD, etc.)
final String currency;
/// The number of patch installs allowed per billing period. This will be null
/// for accounts with unlimited patch installs.
final int? patchInstallLimit;
/// The maximum number of collaborators allowed per account. This will be null
/// for accounts with unlimited collaborators.
final int? maxTeamSize;
}
@@ -1,41 +0,0 @@
// 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 'shorebird_plan.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
ShorebirdPlan _$ShorebirdPlanFromJson(Map<String, dynamic> json) =>
$checkedCreate(
'ShorebirdPlan',
json,
($checkedConvert) {
final val = ShorebirdPlan(
name: $checkedConvert('name', (v) => v as String),
monthlyCost: $checkedConvert(
'monthly_cost', (v) => const MoneyConverter().fromJson(v as int)),
currency: $checkedConvert('currency', (v) => v as String),
patchInstallLimit:
$checkedConvert('patch_install_limit', (v) => v as int?),
maxTeamSize: $checkedConvert('max_team_size', (v) => v as int?),
);
return val;
},
fieldKeyMap: const {
'monthlyCost': 'monthly_cost',
'patchInstallLimit': 'patch_install_limit',
'maxTeamSize': 'max_team_size'
},
);
Map<String, dynamic> _$ShorebirdPlanToJson(ShorebirdPlan instance) =>
<String, dynamic>{
'name': instance.name,
'monthly_cost': const MoneyConverter().toJson(instance.monthlyCost),
'currency': instance.currency,
'patch_install_limit': instance.patchInstallLimit,
'max_team_size': instance.maxTeamSize,
};
@@ -1,36 +0,0 @@
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.plan,
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);
/// The plan associated with this subscription.
final ShorebirdPlan plan;
/// 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());
}
@@ -1,37 +0,0 @@
// 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(
plan: $checkedConvert(
'plan', (v) => ShorebirdPlan.fromJson(v as Map<String, dynamic>)),
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>{
'plan': instance.plan.toJson(),
'paid_through_date':
const TimestampConverter().toJson(instance.paidThroughDate),
'will_renew': instance.willRenew,
};
@@ -9,13 +9,11 @@ environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
json_annotation: ^4.8.1
money2: ^3.4.1
json_annotation: ^4.8.1
dev_dependencies:
build_runner: ^2.0.0
json_serializable: ^6.6.1
mocktail: ^1.0.0
path: ^1.8.3
test: ^1.19.2
very_good_analysis: ^5.1.0
@@ -1,20 +0,0 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group(ShorebirdPlan, () {
test('can be (de)serialized', () {
final plan = ShorebirdPlan(
name: 'Hobby',
monthlyCost: Money.fromIntWithCurrency(0, usd),
currency: 'USD',
patchInstallLimit: 1000,
maxTeamSize: 1,
);
expect(
ShorebirdPlan.fromJson(plan.toJson()).toJson(),
equals(plan.toJson()),
);
});
});
}
@@ -1,46 +0,0 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group(Subscription, () {
final plan = ShorebirdPlan(
name: 'Hobby',
monthlyCost: Money.fromIntWithCurrency(0, usd),
currency: 'USD',
patchInstallLimit: 1000,
maxTeamSize: 1,
);
test('can be (de)serialized', () {
final subscription = Subscription(
plan: plan,
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(
plan: plan,
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(
plan: plan,
paidThroughDate: DateTime.now().subtract(const Duration(days: 1)),
willRenew: true,
);
expect(subscription.isActive, isFalse);
});
});
});
}