From 0e5d6e79c557c78828c6b8e385237323f3327da4 Mon Sep 17 00:00:00 2001
From: aj-rosado <109146700+aj-rosado@users.noreply.github.com>
Date: Wed, 8 Feb 2023 12:03:02 +0000
Subject: [PATCH] [PS-1809] Updating the account premium state when syncing the
 vault (#2290)

* [PS-1809] Updating the account premium state when syncing the vault

* [PS-1809] Added validation to check if HasPremiumPersonally needs to be updated

* PS-1809 Renamed SetPremiumAsync to SetPersonalPremiumAsync
---
 src/Core/Abstractions/IStateService.cs |  1 +
 src/Core/Services/StateService.cs      | 14 ++++++++++++++
 src/Core/Services/SyncService.cs       |  1 +
 3 files changed, 16 insertions(+)

diff --git a/src/Core/Abstractions/IStateService.cs b/src/Core/Abstractions/IStateService.cs
index 621a7cee3..ecfba14d8 100644
--- a/src/Core/Abstractions/IStateService.cs
+++ b/src/Core/Abstractions/IStateService.cs
@@ -31,6 +31,7 @@ namespace Bit.Core.Abstractions
         Task<bool> GetBiometricLockedAsync(string userId = null);
         Task SetBiometricLockedAsync(bool value, string userId = null);
         Task<bool> CanAccessPremiumAsync(string userId = null);
+        Task SetPersonalPremiumAsync(bool value, string userId = null);
         Task<string> GetProtectedPinAsync(string userId = null);
         Task SetProtectedPinAsync(string value, string userId = null);
         Task<string> GetPinProtectedAsync(string userId = null);
diff --git a/src/Core/Services/StateService.cs b/src/Core/Services/StateService.cs
index 5c587d82f..dce4c980b 100644
--- a/src/Core/Services/StateService.cs
+++ b/src/Core/Services/StateService.cs
@@ -291,6 +291,20 @@ namespace Bit.Core.Services
             return organizations?.Any(o => o.UsersGetPremium && o.Enabled) ?? false;
         }
 
+        public async Task SetPersonalPremiumAsync(bool value, string userId = null)
+        {
+            var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
+                await GetDefaultStorageOptionsAsync());
+            var account = await GetAccountAsync(reconciledOptions);
+            if (account?.Profile == null || account.Profile.HasPremiumPersonally.GetValueOrDefault() == value)
+            {
+                return;
+            }
+
+            account.Profile.HasPremiumPersonally = value;
+            await SaveAccountAsync(account, reconciledOptions);
+        }
+
         public async Task<string> GetProtectedPinAsync(string userId = null)
         {
             var reconciledOptions = ReconcileOptions(new StorageOptions { UserId = userId },
diff --git a/src/Core/Services/SyncService.cs b/src/Core/Services/SyncService.cs
index db24b5338..595eb11c9 100644
--- a/src/Core/Services/SyncService.cs
+++ b/src/Core/Services/SyncService.cs
@@ -335,6 +335,7 @@ namespace Bit.Core.Services
             await _organizationService.ReplaceAsync(organizations);
             await _stateService.SetEmailVerifiedAsync(response.EmailVerified);
             await _stateService.SetNameAsync(response.Name);
+            await _stateService.SetPersonalPremiumAsync(response.Premium);
             await _stateService.SetAvatarColorAsync(response.AvatarColor);
             await _keyConnectorService.SetUsesKeyConnector(response.UsesKeyConnector);
         }