Skip to content

Commit

Permalink
Merge pull request #459 from PermanentOrg/promo-code-bug
Browse files Browse the repository at this point in the history
Do not update account storage if it's already updated
  • Loading branch information
meisekimiu authored Sep 24, 2024
2 parents 5d29ba7 + 2352bc4 commit d7dea61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/app/core/components/redeem-gift/redeem-gift.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ describe('StorageDialogComponent', () => {

expect(instance.resultMessage.successful).toBeFalse();
});

it('should not bump up account storage if it has already been done on the server side', async () => {
const { instance } = await shallow.render();
mockAccountService.addMoreSpaceAfterRefresh = true;
await instance.onPromoFormSubmit({ code: 'potato' });

expect(mockAccountService.account.spaceLeft).toBe(
5000 * 1024 * 1024 + 1024,
);
});
});
9 changes: 8 additions & 1 deletion src/app/core/components/redeem-gift/redeem-gift.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ export class RedeemGiftComponent implements OnInit {
}

private async updateAccountStorageBytes(response: BillingResponse) {
const spaceBeforeRefresh = this.account.getAccount()?.spaceTotal;
await this.account.refreshAccount();

const spaceAfterRefresh = this.account.getAccount()?.spaceTotal;
const promo = response.getPromoVO();
const bytes = promo.sizeInMB * (1024 * 1024);
this.account.addStorageBytes(bytes);

if (spaceBeforeRefresh == spaceAfterRefresh) {
this.account.addStorageBytes(bytes);
}

this.showPromoCodeSuccess(bytes);
}

Expand Down
14 changes: 14 additions & 0 deletions src/app/core/components/redeem-gift/shared-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,28 @@ export interface MockApiService {
export class MockAccountService {
public addedStorage: number | undefined;
public failRefresh: boolean = false;
public addMoreSpaceAfterRefresh: boolean = false;
public account: AccountVO = new AccountVO({
spaceLeft: 1024,
spaceTotal: 1024,
});
public refreshAccount(): Promise<void> {
if (this.failRefresh) {
return Promise.reject();
}
if (this.addMoreSpaceAfterRefresh) {
this.account.spaceLeft += 5000 * 1024 * 1024;
this.account.spaceTotal += 5000 * 1024 * 1024;
}
return Promise.resolve();
}
public getAccount(): AccountVO {
return this.account;
}
public setAccount(_account: AccountVO): void {}
public addStorageBytes(sizeInBytes: number): void {
this.account.spaceLeft += sizeInBytes;
this.account.spaceTotal += sizeInBytes;
this.addedStorage = sizeInBytes;
}
}

0 comments on commit d7dea61

Please sign in to comment.