Skip to content

Commit

Permalink
PER-9349
Browse files Browse the repository at this point in the history
Moved the logic inside the account service.
  • Loading branch information
crisnicandrei committed Oct 20, 2023
1 parent 2fe18b6 commit e6d9d2e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const mockAccountService = {
getAccount: (): AccountVO => {
return new AccountVO({ spaceLeft: 10000, spaceTotal: 10000 });
},
addStorageBytes: (sizeInBytes): void => {},
};

describe('StorageDialogComponent', () => {
Expand Down Expand Up @@ -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' };

Expand All @@ -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
);
});
Expand Down
14 changes: 3 additions & 11 deletions src/app/core/components/storage-dialog/storage-dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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;
}
}
11 changes: 3 additions & 8 deletions src/app/pledge/components/new-pledge/new-pledge.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {}
Expand Down
10 changes: 10 additions & 0 deletions src/app/shared/services/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit e6d9d2e

Please sign in to comment.