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
344 lines (299 loc) · 11.1 KB
/
Copy pathsubscription.dart
File metadata and controls
344 lines (299 loc) · 11.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
import 'package:omi/backend/schema/gen/subscription_usage_wire.g.dart' as wire;
enum PlanType { basic, unlimited, architect, operator, plus, unlimitedV2 }
enum SubscriptionStatus { active, inactive }
const Map<PlanType, String> _planTypeWireNames = {
PlanType.basic: 'basic',
PlanType.unlimited: 'unlimited',
PlanType.architect: 'architect',
PlanType.operator: 'operator',
PlanType.plus: 'plus',
PlanType.unlimitedV2: 'unlimited_v2',
};
extension PlanTypeX on PlanType {
String get wireName => _planTypeWireNames[this]!;
bool get isPaid => this != PlanType.basic;
/// Plans with no monthly transcription cap. Plus is paid but metered
/// (1500 min/month), so it is deliberately excluded.
bool get hasUnlimitedTranscription =>
this == PlanType.unlimited ||
this == PlanType.operator ||
this == PlanType.architect ||
this == PlanType.unlimitedV2;
/// Mirrors backend DESKTOP_ENTITLED_PLAN_TYPES.
bool get grantsDesktop => this == PlanType.operator || this == PlanType.architect;
}
PlanType _planTypeFromWire(String? value) {
for (final entry in _planTypeWireNames.entries) {
if (entry.value == value) return entry.key;
}
return PlanType.basic;
}
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();
}