From e6d9d2ef3f2f49070338c78d8d685e65fb7c0804 Mon Sep 17 00:00:00 2001 From: crisnicandrei <62384997+crisnicandrei@users.noreply.github.com> Date: Wed, 11 Oct 2023 23:39:15 +0300 Subject: [PATCH] PER-9349 Moved the logic inside the account service. --- .../storage-dialog.component.spec.ts | 13 ++++--------- .../storage-dialog/storage-dialog.component.ts | 14 +++----------- .../components/new-pledge/new-pledge.component.ts | 11 +++-------- src/app/shared/services/account/account.service.ts | 10 ++++++++++ 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/app/core/components/storage-dialog/storage-dialog.component.spec.ts b/src/app/core/components/storage-dialog/storage-dialog.component.spec.ts index c617ec4f7..a01c6fcc1 100644 --- a/src/app/core/components/storage-dialog/storage-dialog.component.spec.ts +++ b/src/app/core/components/storage-dialog/storage-dialog.component.spec.ts @@ -43,6 +43,7 @@ const mockAccountService = { getAccount: (): AccountVO => { return new AccountVO({ spaceLeft: 10000, spaceTotal: 10000 }); }, + addStorageBytes: (sizeInBytes): void => {}, }; describe('StorageDialogComponent', () => { @@ -85,14 +86,7 @@ describe('StorageDialogComponent', () => { mockApiService.billing, 'redeemPromoCode' ).and.resolveTo(new BillingResponse(mockPromoData)); - const spyRefreshAccount = spyOn( - mockAccountService, - 'refreshAccount' - ).and.resolveTo(undefined); - const updateStorageAfterRedeemingSpy = spyOn( - instance, - 'updateStorageAfterRedeeming' - ).and.callThrough(); + const addStorageSpy = spyOn(mockAccountService, 'addStorageBytes').and.callThrough(); const promoData: PromoVOData = { code: 'promo' }; @@ -101,9 +95,10 @@ describe('StorageDialogComponent', () => { expect(redeemPromoCodeSpy).toHaveBeenCalled(); const response = await redeemPromoCodeSpy.calls.mostRecent().returnValue; + const promo = response.getPromoVO(); - expect(updateStorageAfterRedeemingSpy).toHaveBeenCalledWith( + expect(addStorageSpy).toHaveBeenCalledWith( promo.sizeInMB * 1024 * 1024 ); }); diff --git a/src/app/core/components/storage-dialog/storage-dialog.component.ts b/src/app/core/components/storage-dialog/storage-dialog.component.ts index 603c6be4d..1a946e203 100644 --- a/src/app/core/components/storage-dialog/storage-dialog.component.ts +++ b/src/app/core/components/storage-dialog/storage-dialog.component.ts @@ -68,8 +68,7 @@ export class StorageDialogComponent implements OnInit, IsTabbedDialog { await this.account.refreshAccount(); const promo = response.getPromoVO(); const bytes = promo.sizeInMB * (1024 * 1024); - const updatedAccount = this.updateStorageAfterRedeeming(bytes); - this.account.setAccount(updatedAccount); + this.account.addStorageBytes(bytes); const pipe = new FileSizePipe(); this.message.showMessage( `Gift code redeemed for ${pipe.transform(bytes)} of storage`, @@ -86,14 +85,7 @@ export class StorageDialogComponent implements OnInit, IsTabbedDialog { this.waiting = false; } } - - public updateStorageAfterRedeeming(bytes: number): AccountVO { - const account = this.account.getAccount(); - const updatedAccount = new AccountVO({ - ...account, - spaceTotal: account.spaceTotal + bytes, - spaceLeft: account.spaceLeft + bytes, - }); - return updatedAccount; + public getAccountForTesting() { + return this.account; } } diff --git a/src/app/pledge/components/new-pledge/new-pledge.component.ts b/src/app/pledge/components/new-pledge/new-pledge.component.ts index 558d73d04..8923ba9f7 100644 --- a/src/app/pledge/components/new-pledge/new-pledge.component.ts +++ b/src/app/pledge/components/new-pledge/new-pledge.component.ts @@ -263,13 +263,7 @@ export class NewPledgeComponent implements OnInit, AfterViewInit, OnDestroy { ); this.waiting = false; if (billingResponse.isSuccessful) { - const newAccount = new AccountVO({ - ...account, - spaceLeft: account.spaceLeft + sizeInBytes, - spaceTotal: account.spaceTotal + sizeInBytes, - }); - this.accountService.setAccount(newAccount); - this.accountService.accountStorageUpdate.next(newAccount); + this.accountService.addStorageBytes(sizeInBytes); this.message.showMessage( `You just claimed ${this.getStorageAmount( pledge.dollarAmount @@ -299,7 +293,8 @@ export class NewPledgeComponent implements OnInit, AfterViewInit, OnDestroy { } private getSizeInBytes(amount: number): number { - return Math.floor(amount / 10) * 1073741824; + const bytesInGiB = 1073741824; + return Math.floor(amount / 10) * bytesInGiB; } ngOnDestroy() {} diff --git a/src/app/shared/services/account/account.service.ts b/src/app/shared/services/account/account.service.ts index 728742eec..5c3d0ab72 100644 --- a/src/app/shared/services/account/account.service.ts +++ b/src/app/shared/services/account/account.service.ts @@ -626,4 +626,14 @@ export class AccountService { this.account.spaceLeft -= amount; this.setAccount(this.account); } + + public addStorageBytes(sizeInBytes): void { + const newAccount = new AccountVO({ + ...this.getAccount(), + spaceLeft: this.account.spaceLeft + sizeInBytes, + spaceTotal: this.account.spaceTotal + sizeInBytes, + }); + this.setAccount(newAccount); + this.accountStorageUpdate.next(newAccount); + } }