async updateSubscriptions(
userSession: UserSession,
packageName: string,
subscriptions: SubscriptionRequest[]
): Promise<UserI | null> {
const key = this.getKey(userSession.userId, packageName);
// Version tracking for concurrent updates
const currentVersion = (this.subscriptionUpdateVersion.get(key) || 0) + 1;
this.subscriptionUpdateVersion.set(key, currentVersion);
// Create new subscription set
const newSubscriptions = new Set<ExtendedStreamType>();
// Process each subscription request
for (const subRequest of subscriptions) {
const streamType = subRequest.type;
// Validate permission for stream type
const hasPermission = await SimplePermissionChecker.checkPermission(
packageName,
streamType
);
if (!hasPermission) {
throw new Error(`Missing permission for stream type: ${streamType}`);
}
// Add base and language-specific subscriptions
newSubscriptions.add(streamType);
if (subRequest.config?.languages) {
for (const lang of subRequest.config.languages) {
const langStream = createTranscriptionStream(streamType, lang);
newSubscriptions.add(langStream);
}
}
}
// Store subscriptions
this.subscriptions.set(key, newSubscriptions);
// Track history
this.addToHistory(key, Array.from(newSubscriptions), "update");
// Persist location rate to database if needed
// ... database operations with retry logic
}