Skip to content

Commit

Permalink
fix: migrate delete personal card api to platform (#3266)
Browse files Browse the repository at this point in the history
* fix: fix errors for bank-account-card.component.ts

* fix: migrate delete personal card api to platform

* fix: minor comments
  • Loading branch information
sumrender authored Nov 18, 2024
1 parent 9782dc9 commit dad92e9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 26 deletions.
21 changes: 21 additions & 0 deletions src/app/core/mock-data/personal-cards.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ export const deletePersonalCardRes: PersonalCard = deepFreeze({
updated_at: new Date('2022-05-31T07:40:58.190907'),
});

export const deletePersonalCardPlatformRes: PlatformApiResponse<PersonalCardPlatform> = deepFreeze({
data: {
account_type: 'SAVINGS',
bank_name: 'Dag Site',
card_number: 'xxxx3235',
created_at: new Date('2024-11-11T06:20:18.061281+00:00'),
currency: 'USD',
id: 'bacc0By33NqhnS',
org_id: 'orrb8EW1zZsy',
updated_at: new Date('2024-11-11T06:20:18.061281+00:00'),
user_id: 'us2KhpQLpzX4',
yodlee_account_id: 'yacQm13ONl3q1',
yodlee_fastlink_params: null,
yodlee_is_credential_update_required: false,
yodlee_is_mfa_required: false,
yodlee_last_synced_at: null,
yodlee_login_id: 'ou6kAM3CXV7d',
yodlee_provider_account_id: '10287107',
},
});

export const linkedAccountsRes: PersonalCard[] = deepFreeze([
{
account_number: 'xxxx4227',
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/models/platform/platform-api-response.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface PlatformApiResponse<T> {
count: number;
offset: number;
count?: number;
offset?: number;
data: T;
}
49 changes: 36 additions & 13 deletions src/app/core/services/personal-cards.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DateService } from './date.service';
import { allFilterPills, creditTxnFilterPill, debitTxnFilterPill } from '../mock-data/filter-pills.data';
import {
apiLinkedAccRes,
deletePersonalCardPlatformRes,
deletePersonalCardRes,
linkedAccountRes2,
platformApiLinkedAccRes,
Expand All @@ -33,7 +34,7 @@ describe('PersonalCardsService', () => {
const apiV2ServiceSpy = jasmine.createSpyObj('ApiV2Service', ['get']);
const apiServiceSpy = jasmine.createSpyObj('ApiService', ['post', 'get']);
const expenseAggregationServiceSpy = jasmine.createSpyObj('ExpenseAggregationService', ['get', 'post', 'delete']);
const spenderPlatformV1ApiServiceSpy = jasmine.createSpyObj('SpenderPlatformV1ApiService', ['get']);
const spenderPlatformV1ApiServiceSpy = jasmine.createSpyObj('SpenderPlatformV1ApiService', ['get', 'post']);
TestBed.configureTestingModule({
providers: [
PersonalCardsService,
Expand Down Expand Up @@ -134,6 +135,40 @@ describe('PersonalCardsService', () => {
});
});

describe('deleteAccount()', () => {
it('should delete personal card when using public api', (done) => {
const usePlatformApi = false;
expenseAggregationService.delete.and.returnValue(of(deletePersonalCardRes));

const accountId = 'bacc0By33NqhnS';

personalCardsService.deleteAccount(accountId, usePlatformApi).subscribe((res) => {
expect(res).toEqual(deletePersonalCardRes);
expect(expenseAggregationService.delete).toHaveBeenCalledOnceWith(`/bank_accounts/${accountId}`);
done();
});
});

it('should delete personal card when using platform api', (done) => {
const usePlatformApi = true;
spenderPlatformV1ApiService.post.and.returnValue(of(deletePersonalCardPlatformRes));

const accountId = 'bacc0By33NqhnS';
const payload = {
data: {
id: accountId,
},
};

personalCardsService.deleteAccount(accountId, usePlatformApi).subscribe((res) => {
expect(res).toEqual(deletePersonalCardPlatformRes.data);
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/personal_cards/delete', payload);
expect(apiV2Service.get).not.toHaveBeenCalled();
done();
});
});
});

it('getBankTransactions(): should get bank transactions', (done) => {
apiV2Service.get.and.returnValue(of(apiPersonalCardTxnsRes));

Expand Down Expand Up @@ -180,18 +215,6 @@ describe('PersonalCardsService', () => {
});
});

it('deleteAccount(): should delete a personal card', (done) => {
expenseAggregationService.delete.and.returnValue(of(deletePersonalCardRes));

const accountId = 'bacc0By33NqhnS';

personalCardsService.deleteAccount(accountId).subscribe((res) => {
expect(res).toEqual(deletePersonalCardRes);
expect(expenseAggregationService.delete).toHaveBeenCalledOnceWith(`/bank_accounts/${accountId}`);
done();
});
});

it('convertFilters(): should convert selected filters', () => {
expect(personalCardsService.convertFilters(selectedFilters1)).toEqual(filterData1);
});
Expand Down
16 changes: 15 additions & 1 deletion src/app/core/services/personal-cards.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,21 @@ export class PersonalCardsService {
});
}

deleteAccount(accountId: string): Observable<PersonalCard> {
deleteAccountPlatform(accountId: string): Observable<PersonalCardPlatform> {
const payload = {
data: {
id: accountId,
},
};
return this.spenderPlatformV1ApiService
.post<PlatformApiResponse<PersonalCardPlatform>>('/personal_cards/delete', payload)
.pipe(map((response) => response.data));
}

deleteAccount(accountId: string, usePlatformApi: boolean): Observable<PersonalCard | PersonalCardPlatform> {
if (usePlatformApi) {
return this.deleteAccountPlatform(accountId);
}
return this.expenseAggregationService.delete('/bank_accounts/' + accountId) as Observable<PersonalCard>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ToastMessageComponent } from '../../toast-message/toast-message.compone
import { PopupAlertComponent } from '../../popup-alert/popup-alert.component';
import { DeleteButtonComponent } from './delete-button/delete-button-component';
import { click, getElementBySelector, getTextContent } from 'src/app/core/dom-helpers';
import { LaunchDarklyService } from 'src/app/core/services/launch-darkly.service';

describe('BankAccountCardComponent', () => {
let component: BankAccountCardComponent;
Expand All @@ -23,6 +24,7 @@ describe('BankAccountCardComponent', () => {
let matSnackBar: jasmine.SpyObj<MatSnackBar>;
let snackbarProperties: jasmine.SpyObj<SnackbarPropertiesService>;
let dateService: jasmine.SpyObj<DateService>;
let launchDarklyService: jasmine.SpyObj<LaunchDarklyService>;

beforeEach(waitForAsync(() => {
const personalCardsServiceSpy = jasmine.createSpyObj('PersonalCardsService', ['deleteAccount']);
Expand All @@ -31,6 +33,7 @@ describe('BankAccountCardComponent', () => {
const matSnackBarSpy = jasmine.createSpyObj('MatSnackBar', ['openFromComponent']);
const snackbarPropertiesSpy = jasmine.createSpyObj('SnackbarPropertiesService', ['setSnackbarProperties']);
const dateServiceSpy = jasmine.createSpyObj('DateService', ['convertUTCDateToLocalDate']);
const launchDarklyServiceSpy = jasmine.createSpyObj('LaunchDarklyService', ['getVariation']);
TestBed.configureTestingModule({
declarations: [BankAccountCardComponent],
imports: [IonicModule.forRoot()],
Expand Down Expand Up @@ -59,6 +62,10 @@ describe('BankAccountCardComponent', () => {
provide: DateService,
useValue: dateServiceSpy,
},
{
provide: LaunchDarklyService,
useValue: launchDarklyServiceSpy,
},
],
}).compileComponents();
fixture = TestBed.createComponent(BankAccountCardComponent);
Expand All @@ -68,6 +75,7 @@ describe('BankAccountCardComponent', () => {
matSnackBar = TestBed.inject(MatSnackBar) as jasmine.SpyObj<MatSnackBar>;
snackbarProperties = TestBed.inject(SnackbarPropertiesService) as jasmine.SpyObj<SnackbarPropertiesService>;
dateService = TestBed.inject(DateService) as jasmine.SpyObj<DateService>;
launchDarklyService = TestBed.inject(LaunchDarklyService) as jasmine.SpyObj<LaunchDarklyService>;
component = fixture.componentInstance;

component.accountDetails = apiLinkedAccRes.data[1];
Expand All @@ -93,15 +101,16 @@ describe('BankAccountCardComponent', () => {
})
);

component.presentPopover(new Event('event'));
component.presentPopover(new PointerEvent('event'));
expect(popoverController.create).toHaveBeenCalledOnceWith({
component: DeleteButtonComponent,
cssClass: 'delete-button-class',
event: new Event('event'),
event: new PointerEvent('event'),
});
});

it('deleteAccount(): should delete account', fakeAsync(() => {
launchDarklyService.getVariation.and.returnValue(of(false));
spyOn(component.deleted, 'emit');
loaderService.showLoader.and.resolveTo();
personalCardsService.deleteAccount.and.returnValue(of(deletePersonalCardRes));
Expand All @@ -114,13 +123,15 @@ describe('BankAccountCardComponent', () => {
tick();
expect(loaderService.showLoader).toHaveBeenCalledOnceWith('Deleting your card...', 5000);
expect(loaderService.hideLoader).toHaveBeenCalledTimes(1);
expect(personalCardsService.deleteAccount).toHaveBeenCalledOnceWith(component.accountDetails.id);
expect(launchDarklyService.getVariation).toHaveBeenCalledOnceWith('personal_cards_platform', false);
expect(personalCardsService.deleteAccount).toHaveBeenCalledOnceWith(component.accountDetails.id, false);
expect(matSnackBar.openFromComponent).toHaveBeenCalledOnceWith(ToastMessageComponent, {
panelClass: ['msb-success'],
});
expect(snackbarProperties.setSnackbarProperties).toHaveBeenCalledOnceWith('success', {
message: 'Card successfully deleted.',
});
expect(component.deleted.emit).toHaveBeenCalledTimes(1);
}));

it('confirmPopup(): should display the confirm popup', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SnackbarPropertiesService } from '../../../../core/services/snackbar-pr
import { ToastMessageComponent } from 'src/app/shared/components/toast-message/toast-message.component';
import { DeleteButtonComponent } from './delete-button/delete-button-component';
import { DateService } from 'src/app/core/services/date.service';
import { LaunchDarklyService } from 'src/app/core/services/launch-darkly.service';
@Component({
selector: 'app-bank-account-card',
templateUrl: './bank-account-card.component.html',
Expand All @@ -33,7 +34,8 @@ export class BankAccountCardComponent implements OnInit {
private popoverController: PopoverController,
private matSnackBar: MatSnackBar,
private snackbarProperties: SnackbarPropertiesService,
private dateService: DateService
private dateService: DateService,
private launchDarklyService: LaunchDarklyService
) {}

ngOnInit(): void {
Expand All @@ -42,25 +44,33 @@ export class BankAccountCardComponent implements OnInit {
}
}

async presentPopover(ev: any) {
async presentPopover(ev: PointerEvent): Promise<void> {
const deleteCardPopOver = await this.popoverController.create({
component: DeleteButtonComponent,
cssClass: 'delete-button-class',
event: ev,
});
await deleteCardPopOver.present();

const { data } = await deleteCardPopOver.onDidDismiss();
const { data } = await deleteCardPopOver.onDidDismiss<string>();

if (data === 'delete') {
this.confirmPopup();
}
}

async deleteAccount() {
async deleteAccount(): Promise<void> {
from(this.loaderService.showLoader('Deleting your card...', 5000))
.pipe(
switchMap(() => this.personalCardsService.deleteAccount(this.accountDetails.id)),
switchMap(() =>
this.launchDarklyService
.getVariation('personal_cards_platform', false)
.pipe(
switchMap((usePlatformApi) =>
this.personalCardsService.deleteAccount(this.accountDetails.id, usePlatformApi)
)
)
),
finalize(async () => {
await this.loaderService.hideLoader();
const message = 'Card successfully deleted.';
Expand All @@ -73,7 +83,7 @@ export class BankAccountCardComponent implements OnInit {
.subscribe(() => this.deleted.emit());
}

async confirmPopup() {
async confirmPopup(): Promise<void> {
const deleteCardPopOver = await this.popoverController.create({
component: PopupAlertComponent,
componentProps: {
Expand All @@ -93,7 +103,7 @@ export class BankAccountCardComponent implements OnInit {

await deleteCardPopOver.present();

const { data } = await deleteCardPopOver.onWillDismiss();
const { data } = await deleteCardPopOver.onWillDismiss<{ action: string }>();

if (data && data.action === 'delete') {
this.deleteAccount();
Expand Down

0 comments on commit dad92e9

Please sign in to comment.