forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.dart
More file actions
412 lines (358 loc) · 14.1 KB
/
Copy pathsubscription.dart
File metadata and controls
412 lines (358 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import 'package:omi/backend/schema/gen/subscription_usage_wire.g.dart' as wire;
enum SubscriptionStatus { active, inactive }
/// A subscription plan identity received from the backend.
///
/// This is intentionally a class rather than an enum. The backend can deploy
/// a new plan before this app is updated, and decoding that value must not
/// silently turn a paying subscriber into [PlanType.basic]. Known plan values
/// retain the enum-shaped API used by the app (`PlanType.values`, `.name`, and
/// the static plan constants), while unknown values retain their raw wire ID.
class PlanType {
static const PlanType basic = PlanType._('basic', 'basic', 'basic');
static const PlanType unlimited = PlanType._('unlimited', 'unlimited', 'unlimited');
static const PlanType architect = PlanType._('architect', 'architect', 'architect');
static const PlanType operator = PlanType._('operator', 'operator', 'operator');
static const PlanType plus = PlanType._('plus', 'plus', 'plus');
static const PlanType unlimitedV2 = PlanType._('unlimitedV2', 'unlimited_v2', 'unlimited_v2');
/// The canonical catalog identities known by this client.
///
/// Legacy aliases and future identities are deliberately not included.
static const List<PlanType> values = <PlanType>[basic, unlimited, architect, operator, plus, unlimitedV2];
/// Dart-style identifier used by existing analytics and UI call sites.
final String name;
/// The exact plan ID to use at the backend wire boundary.
///
/// For an unknown plan this is the raw value received from the backend.
final String wireName;
/// Canonical catalog identity, or null for an unrecognized future value.
final String? _canonicalWireName;
const PlanType._(this.name, this.wireName, this._canonicalWireName);
/// Creates a lossless representation for a plan ID unknown to this client.
factory PlanType.unknown(String rawWireName) {
return PlanType._(rawWireName, rawWireName, null);
}
/// Decodes a backend plan ID without collapsing unknown values to Basic.
///
/// `pro` is the catalog's legacy alias for Architect and is canonicalized to
/// Architect's identity at the domain boundary.
factory PlanType.fromWire(String? value) {
switch (value) {
case 'basic':
return PlanType.basic;
case 'unlimited':
return PlanType.unlimited;
case 'architect':
return PlanType.architect;
case 'operator':
return PlanType.operator;
case 'plus':
return PlanType.plus;
case 'unlimited_v2':
return PlanType.unlimitedV2;
case 'pro':
return PlanType.architect;
case null:
// GeneratedSubscription supplies the legacy default for an omitted
// plan field. Keep this defensive path aligned with that contract.
return PlanType.basic;
default:
return PlanType.unknown(value);
}
}
bool get isUnknown => _canonicalWireName == null;
/// Unknown plans never infer paid access from an unfamiliar ID.
bool get isPaid => !isUnknown && _canonicalWireName != PlanType.basic.wireName;
/// Plans with no monthly transcription cap. Plus is paid but metered
/// (1500 min/month), so it is deliberately excluded. Unknown plans are
/// conservative and do not infer unlimited access.
bool get hasUnlimitedTranscription =>
_canonicalWireName == PlanType.unlimited.wireName ||
_canonicalWireName == PlanType.operator.wireName ||
_canonicalWireName == PlanType.architect.wireName ||
_canonicalWireName == PlanType.unlimitedV2.wireName;
/// Mirrors backend DESKTOP_ENTITLED_PLAN_TYPES.
bool get grantsDesktop =>
_canonicalWireName == PlanType.operator.wireName || _canonicalWireName == PlanType.architect.wireName;
@override
bool operator ==(Object other) {
if (other is! PlanType) return false;
if (isUnknown || other.isUnknown) {
return isUnknown && other.isUnknown && wireName == other.wireName;
}
return _canonicalWireName == other._canonicalWireName;
}
@override
int get hashCode => isUnknown ? wireName.hashCode : _canonicalWireName.hashCode;
@override
String toString() => isUnknown ? 'PlanType.unknown($wireName)' : 'PlanType.$name';
}
PlanType _planTypeFromWire(String? value) => PlanType.fromWire(value);
SubscriptionStatus _subscriptionStatusFromWire(String? value) {
return SubscriptionStatus.values.asNameMap()[value] ?? SubscriptionStatus.inactive;
}
class PlanLimits {
final int? transcriptionSeconds;
final int? wordsTranscribed;
final int? insightsGained;
final int? chatQuestionsPerMonth;
final double? chatCostUsdPerMonth;
PlanLimits({
this.transcriptionSeconds,
this.wordsTranscribed,
this.insightsGained,
this.chatQuestionsPerMonth,
this.chatCostUsdPerMonth,
});
factory PlanLimits.fromJson(Map<String, dynamic> json) {
return PlanLimits.fromGenerated(wire.GeneratedPlanLimits.fromJson(json));
}
factory PlanLimits.fromGenerated(wire.GeneratedPlanLimits generated) {
return PlanLimits(
transcriptionSeconds: generated.transcriptionSeconds,
wordsTranscribed: generated.wordsTranscribed,
insightsGained: generated.insightsGained,
chatQuestionsPerMonth: generated.chatQuestionsPerMonth,
chatCostUsdPerMonth: generated.chatCostUsdPerMonth,
);
}
wire.GeneratedPlanLimits toGenerated() {
return wire.GeneratedPlanLimits(
transcriptionSeconds: transcriptionSeconds,
wordsTranscribed: wordsTranscribed,
insightsGained: insightsGained,
chatQuestionsPerMonth: chatQuestionsPerMonth,
chatCostUsdPerMonth: chatCostUsdPerMonth,
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
class Subscription {
final PlanType plan;
final SubscriptionStatus status;
final int? currentPeriodEnd;
final String? stripeSubscriptionId;
final String? currentPriceId;
final List<String> features;
final bool cancelAtPeriodEnd;
final bool deprecated;
final String? deprecationMessage;
final PlanLimits limits;
Subscription({
required this.plan,
required this.status,
this.currentPeriodEnd,
this.stripeSubscriptionId,
this.currentPriceId,
this.features = const [],
this.cancelAtPeriodEnd = false,
this.deprecated = false,
this.deprecationMessage,
PlanLimits? limits,
}) : limits = limits ?? PlanLimits();
factory Subscription.fromJson(Map<String, dynamic> json) {
return Subscription.fromGenerated(wire.GeneratedSubscription.fromJson(json));
}
factory Subscription.fromGenerated(wire.GeneratedSubscription generated) {
return Subscription(
plan: _planTypeFromWire(generated.plan),
status: _subscriptionStatusFromWire(generated.status),
currentPeriodEnd: generated.currentPeriodEnd,
stripeSubscriptionId: generated.stripeSubscriptionId,
currentPriceId: generated.currentPriceId,
features: generated.features,
cancelAtPeriodEnd: generated.cancelAtPeriodEnd,
deprecated: generated.deprecated,
deprecationMessage: generated.deprecationMessage,
limits: PlanLimits.fromGenerated(generated.limits),
);
}
wire.GeneratedSubscription toGenerated() {
return wire.GeneratedSubscription(
plan: plan.wireName,
status: status.name,
currentPeriodEnd: currentPeriodEnd,
stripeSubscriptionId: stripeSubscriptionId,
currentPriceId: currentPriceId,
features: features,
cancelAtPeriodEnd: cancelAtPeriodEnd,
deprecated: deprecated,
deprecationMessage: deprecationMessage,
limits: limits.toGenerated(),
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
class PricingOption {
final String id;
final String title;
final String? description;
final String priceString;
PricingOption({required this.id, required this.title, this.description, required this.priceString});
factory PricingOption.fromJson(Map<String, dynamic> json) {
return PricingOption.fromGenerated(wire.GeneratedPricingOption.fromJson(json));
}
factory PricingOption.fromGenerated(wire.GeneratedPricingOption generated) {
return PricingOption(
id: generated.id,
title: generated.title,
description: generated.description,
priceString: generated.priceString,
);
}
wire.GeneratedPricingOption toGenerated() {
return wire.GeneratedPricingOption(id: id, title: title, description: description, priceString: priceString);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
class SubscriptionPlan {
final String id;
final String title;
final List<String> features;
final List<PricingOption> prices;
SubscriptionPlan({required this.id, required this.title, this.features = const [], this.prices = const []});
factory SubscriptionPlan.fromJson(Map<String, dynamic> json) {
return SubscriptionPlan.fromGenerated(wire.GeneratedSubscriptionPlan.fromJson(json));
}
factory SubscriptionPlan.fromGenerated(wire.GeneratedSubscriptionPlan generated) {
return SubscriptionPlan(
id: generated.id,
title: generated.title,
features: generated.features,
prices: generated.prices.map(PricingOption.fromGenerated).toList(),
);
}
wire.GeneratedSubscriptionPlan toGenerated() {
return wire.GeneratedSubscriptionPlan(
id: id,
title: title,
features: features,
prices: prices.map((price) => price.toGenerated()).toList(),
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
class PhoneCallQuota {
final bool hasAccess;
final bool isPaid;
final int? monthlyLimit;
final int monthlyUsed;
final int? remaining;
final int? maxDurationSeconds;
final List<String> allowedCountries;
final int? resetAt;
PhoneCallQuota({
required this.hasAccess,
required this.isPaid,
this.monthlyLimit,
this.monthlyUsed = 0,
this.remaining,
this.maxDurationSeconds,
this.allowedCountries = const [],
this.resetAt,
});
factory PhoneCallQuota.fromJson(Map<String, dynamic> json) {
return PhoneCallQuota.fromGenerated(wire.GeneratedPhoneCallQuota.fromJson(json));
}
factory PhoneCallQuota.fromGenerated(wire.GeneratedPhoneCallQuota generated) {
return PhoneCallQuota(
hasAccess: generated.hasAccess,
isPaid: generated.isPaid,
monthlyLimit: generated.monthlyLimit,
monthlyUsed: generated.monthlyUsed,
remaining: generated.remaining,
maxDurationSeconds: generated.maxDurationSeconds,
allowedCountries: generated.allowedCountries,
resetAt: generated.resetAt,
);
}
wire.GeneratedPhoneCallQuota toGenerated() {
return wire.GeneratedPhoneCallQuota(
hasAccess: hasAccess,
isPaid: isPaid,
monthlyLimit: monthlyLimit,
monthlyUsed: monthlyUsed,
remaining: remaining,
maxDurationSeconds: maxDurationSeconds,
allowedCountries: allowedCountries,
resetAt: resetAt,
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}
class UserSubscriptionResponse {
final Subscription subscription;
final int transcriptionSecondsUsed;
final int transcriptionSecondsLimit;
final int wordsTranscribedUsed;
final int wordsTranscribedLimit;
final int insightsGainedUsed;
final int insightsGainedLimit;
final List<SubscriptionPlan> availablePlans;
final bool showSubscriptionUi;
// Chat quota fields — populated from subscription endpoint
final double chatQuotaUsed;
final String? chatQuotaUnit;
final double chatQuotaPercent;
final bool chatQuotaAllowed;
final int? chatQuotaResetAt;
final PhoneCallQuota? phoneCallQuota;
UserSubscriptionResponse({
required this.subscription,
required this.transcriptionSecondsUsed,
required this.transcriptionSecondsLimit,
required this.wordsTranscribedUsed,
required this.wordsTranscribedLimit,
required this.insightsGainedUsed,
required this.insightsGainedLimit,
this.availablePlans = const [],
this.showSubscriptionUi = true,
this.chatQuotaUsed = 0.0,
this.chatQuotaUnit,
this.chatQuotaPercent = 0.0,
this.chatQuotaAllowed = true,
this.chatQuotaResetAt,
this.phoneCallQuota,
});
factory UserSubscriptionResponse.fromJson(Map<String, dynamic> json) {
return UserSubscriptionResponse.fromGenerated(wire.GeneratedUserSubscriptionResponse.fromJson(json));
}
factory UserSubscriptionResponse.fromGenerated(wire.GeneratedUserSubscriptionResponse generated) {
return UserSubscriptionResponse(
subscription: Subscription.fromGenerated(generated.subscription),
transcriptionSecondsUsed: generated.transcriptionSecondsUsed,
transcriptionSecondsLimit: generated.transcriptionSecondsLimit,
wordsTranscribedUsed: generated.wordsTranscribedUsed,
wordsTranscribedLimit: generated.wordsTranscribedLimit,
insightsGainedUsed: generated.insightsGainedUsed,
insightsGainedLimit: generated.insightsGainedLimit,
availablePlans: generated.availablePlans.map(SubscriptionPlan.fromGenerated).toList(),
showSubscriptionUi: generated.showSubscriptionUi,
chatQuotaUsed: generated.chatQuotaUsed,
chatQuotaUnit: generated.chatQuotaUnit,
chatQuotaPercent: generated.chatQuotaPercent,
chatQuotaAllowed: generated.chatQuotaAllowed,
chatQuotaResetAt: generated.chatQuotaResetAt,
phoneCallQuota: generated.phoneCallQuota == null ? null : PhoneCallQuota.fromGenerated(generated.phoneCallQuota!),
);
}
wire.GeneratedUserSubscriptionResponse toGenerated() {
return wire.GeneratedUserSubscriptionResponse(
subscription: subscription.toGenerated(),
transcriptionSecondsUsed: transcriptionSecondsUsed,
transcriptionSecondsLimit: transcriptionSecondsLimit,
wordsTranscribedUsed: wordsTranscribedUsed,
wordsTranscribedLimit: wordsTranscribedLimit,
insightsGainedUsed: insightsGainedUsed,
insightsGainedLimit: insightsGainedLimit,
availablePlans: availablePlans.map((plan) => plan.toGenerated()).toList(),
showSubscriptionUi: showSubscriptionUi,
chatQuotaUsed: chatQuotaUsed,
chatQuotaUnit: chatQuotaUnit,
chatQuotaPercent: chatQuotaPercent,
chatQuotaAllowed: chatQuotaAllowed,
chatQuotaResetAt: chatQuotaResetAt,
phoneCallQuota: phoneCallQuota?.toGenerated(),
);
}
Map<String, dynamic> toJson() => toGenerated().toJson();
}