From d7a1c667dd62dacc1ef15f2a4f408dc07045fcad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C3=B3jko?= Date: Mon, 15 Jan 2024 10:23:51 +0100 Subject: [PATCH] fix(auth): update shared subscriptions upon subscription sync (#1022) --- packages/auth/src/Bootstrap/Container.ts | 1 + .../SubscriptionSyncRequestedEventHandler.ts | 15 +++++++++++++++ .../TypeORM/TypeORMUserSubscriptionRepository.ts | 1 + 3 files changed, 17 insertions(+) diff --git a/packages/auth/src/Bootstrap/Container.ts b/packages/auth/src/Bootstrap/Container.ts index 0c23af087..f14a75a8a 100644 --- a/packages/auth/src/Bootstrap/Container.ts +++ b/packages/auth/src/Bootstrap/Container.ts @@ -1421,6 +1421,7 @@ export class ContainerConfigLoader { container.get(TYPES.Auth_SetSettingValue), container.get(TYPES.Auth_OfflineSettingService), container.get(TYPES.Auth_ContenDecoder), + container.get(TYPES.Auth_RenewSharedSubscriptions), container.get(TYPES.Auth_Logger), ), ) diff --git a/packages/auth/src/Domain/Handler/SubscriptionSyncRequestedEventHandler.ts b/packages/auth/src/Domain/Handler/SubscriptionSyncRequestedEventHandler.ts index 76e40ea7e..affd1549a 100644 --- a/packages/auth/src/Domain/Handler/SubscriptionSyncRequestedEventHandler.ts +++ b/packages/auth/src/Domain/Handler/SubscriptionSyncRequestedEventHandler.ts @@ -16,6 +16,7 @@ import { OfflineSettingName } from '../Setting/OfflineSettingName' import { UserSubscriptionType } from '../Subscription/UserSubscriptionType' import { ApplyDefaultSubscriptionSettings } from '../UseCase/ApplyDefaultSubscriptionSettings/ApplyDefaultSubscriptionSettings' import { SetSettingValue } from '../UseCase/SetSettingValue/SetSettingValue' +import { RenewSharedSubscriptions } from '../UseCase/RenewSharedSubscriptions/RenewSharedSubscriptions' export class SubscriptionSyncRequestedEventHandler implements DomainEventHandlerInterface { constructor( @@ -27,6 +28,7 @@ export class SubscriptionSyncRequestedEventHandler implements DomainEventHandler private setSettingValue: SetSettingValue, private offlineSettingService: OfflineSettingServiceInterface, private contentDecoder: ContentDecoderInterface, + private renewSharedSubscriptions: RenewSharedSubscriptions, private logger: Logger, ) {} @@ -85,6 +87,19 @@ export class SubscriptionSyncRequestedEventHandler implements DomainEventHandler event.payload.timestamp, ) + const renewalResult = await this.renewSharedSubscriptions.execute({ + inviterEmail: user.email, + newSubscriptionId: event.payload.subscriptionId, + newSubscriptionName: event.payload.subscriptionName, + newSubscriptionExpiresAt: event.payload.subscriptionExpiresAt, + timestamp: event.payload.timestamp, + }) + if (renewalResult.isFailed()) { + this.logger.error(`Could not renew shared subscriptions for user: ${renewalResult.getError()}`, { + userId: user.uuid, + }) + } + await this.roleService.addUserRoleBasedOnSubscription(user, event.payload.subscriptionName) const applyingSettingsResult = await this.applyDefaultSubscriptionSettings.execute({ diff --git a/packages/auth/src/Infra/TypeORM/TypeORMUserSubscriptionRepository.ts b/packages/auth/src/Infra/TypeORM/TypeORMUserSubscriptionRepository.ts index 3a3ec9126..85a6c7eb1 100644 --- a/packages/auth/src/Infra/TypeORM/TypeORMUserSubscriptionRepository.ts +++ b/packages/auth/src/Infra/TypeORM/TypeORMUserSubscriptionRepository.ts @@ -84,6 +84,7 @@ export class TypeORMUserSubscriptionRepository implements UserSubscriptionReposi userUuid, subscriptionId, }) + .orderBy('ends_at', 'DESC') .getOne() }