Skip to content

Commit

Permalink
feat: Fetching card details from admin/corporate_cards and removing s…
Browse files Browse the repository at this point in the history
…tats call (#3038)

* Fetching card details from admin/corporate_cards and removing stats call

* Removing nickname from mock data to test undefined nickname condition

* Simplifying getting card details
  • Loading branch information
neohacker18 committed Jun 4, 2024
1 parent 989bedd commit e04b5e8
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 31 deletions.
74 changes: 74 additions & 0 deletions src/app/core/mock-data/corporate-card-response.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { DataFeedSource } from '../enums/data-feed-source.enum';
import { PlatformCorporateCard } from '../models/platform/platform-corporate-card.model';
import deepFreeze from 'deep-freeze-strict';

export const corporateCardsResponseData: PlatformCorporateCard[] = deepFreeze([
{
assignor_user_id: 'usvKA4X8Ugcr',
bank_name: 'DAMNA',
card_number: '8698',
cardholder_name: null,
code: null,
created_at: '2023-03-03T07:16:46.376082+00:00',
data_feed_source: DataFeedSource.BANK_FEED_CDF,
id: 'bacc15bbrRGWzf',
is_dummy: false,
is_mastercard_enrolled: false,
is_visa_enrolled: false,
last_assigned_at: '2023-03-03T07:16:46.376082+00:00',
last_ready_for_verification_at: null,
last_synced_at: null,
last_verification_attempt_at: null,
last_verified_at: null,
org_id: 'orNVthTo2Zyo',
updated_at: '2023-03-03T07:16:46.376082+00:00',
user_id: 'usvKA4X8Ugcr',
verification_status: 'NOT_VERIFIED',
nickname: 'Business Card1',
},
{
assignor_user_id: 'usvKA4X8Ugcr',
bank_name: 'DAMNA',
card_number: '8698',
cardholder_name: null,
code: null,
created_at: '2023-03-03T07:16:46.376082+00:00',
data_feed_source: DataFeedSource.BANK_FEED_CDF,
id: 'bacc15bbrRGWzf',
is_dummy: false,
is_mastercard_enrolled: false,
is_visa_enrolled: false,
last_assigned_at: '2023-03-03T07:16:46.376082+00:00',
last_ready_for_verification_at: null,
last_synced_at: null,
last_verification_attempt_at: null,
last_verified_at: null,
org_id: 'orNVthTo2Zyo',
updated_at: '2023-03-03T07:16:46.376082+00:00',
user_id: 'usvKA4X8Ugcr',
verification_status: 'NOT_VERIFIED',
nickname: 'Business Card2',
},
{
assignor_user_id: 'usvKA4X8Ugcr',
bank_name: 'PEX BANK',
card_number: '869',
cardholder_name: null,
code: null,
created_at: '2023-03-03T07:16:46.376082+00:00',
data_feed_source: DataFeedSource.BANK_FEED_CDF,
id: 'bacc15bbrRGWzf',
is_dummy: false,
is_mastercard_enrolled: false,
is_visa_enrolled: false,
last_assigned_at: '2023-03-03T07:16:46.376082+00:00',
last_ready_for_verification_at: null,
last_synced_at: null,
last_verification_attempt_at: null,
last_verified_at: null,
org_id: 'orNVthTo2Zyo',
updated_at: '2023-03-03T07:16:46.376082+00:00',
user_id: 'usvKA4X8Ugcr',
verification_status: 'NOT_VERIFIED',
},
]);
17 changes: 17 additions & 0 deletions src/app/core/mock-data/unique-cards.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ export const uniqueCardsParam: UniqueCards[] = deepFreeze([
cardName: 'PEX BANK',
},
]);

export const uniqueCardsData: UniqueCards[] = deepFreeze([
{
cardNumber: '8698',
cardName: 'DAMNA',
cardNickname: 'Business Card1',
},
{
cardNumber: '8698',
cardName: 'DAMNA',
cardNickname: 'Business Card2',
},
{
cardNumber: '869',
cardName: 'PEX BANK',
},
]);
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export interface PlatformCorporateCard {
verification_status: string;
virtual_card_id?: string;
virtual_card_state?: string;
nickname?: string;
}
1 change: 1 addition & 0 deletions src/app/core/models/unique-cards.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface UniqueCards {
cardNumber: string;
cardName: string;
cardNickname?: string;
}
30 changes: 17 additions & 13 deletions src/app/fyle/my-expenses/my-expenses.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import { txnList } from 'src/app/core/mock-data/transaction.data';
import { unflattenedTxnData } from 'src/app/core/mock-data/unflattened-txn.data';
import { unformattedTxnData } from 'src/app/core/mock-data/unformatted-transaction.data';
import { expectedUniqueCardStats } from 'src/app/core/mock-data/unique-cards-stats.data';
import { uniqueCardsParam } from 'src/app/core/mock-data/unique-cards.data';
import { uniqueCardsData } from 'src/app/core/mock-data/unique-cards.data';
import { AdvancesStates } from 'src/app/core/models/advances-states.model';
import { BackButtonActionPriority } from 'src/app/core/models/back-button-action-priority.enum';
import { Expense } from 'src/app/core/models/expense.model';
Expand Down Expand Up @@ -134,6 +134,7 @@ import {
expectedReportsSinglePageSubmitted,
expectedReportsSinglePageWithApproval,
} from 'src/app/core/mock-data/platform-report.data';
import { corporateCardsResponseData } from 'src/app/core/mock-data/corporate-card-response.data';

describe('MyExpensesV2Page', () => {
let component: MyExpensesPage;
Expand Down Expand Up @@ -244,6 +245,7 @@ describe('MyExpensesV2Page', () => {
const corporateCreditCardServiceSpy = jasmine.createSpyObj('CorporateCreditCardExpenseService', [
'getExpenseDetailsInCards',
'getAssignedCards',
'getCorporateCards',
]);
const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', ['get']);
const platformHandlerServiceSpy = jasmine.createSpyObj('PlatformHandlerService', ['registerBackButtonAction']);
Expand Down Expand Up @@ -476,7 +478,7 @@ describe('MyExpensesV2Page', () => {
orgSettingsService.get.and.returnValue(of(orgSettingsRes));
categoriesService.getMileageOrPerDiemCategories.and.returnValue(of(mileagePerDiemPlatformCategoryData));
corporateCreditCardService.getAssignedCards.and.returnValue(of(expectedAssignedCCCStats));
spyOn(component, 'getCardDetail').and.returnValue(expectedUniqueCardStats);
spyOn(component, 'getCardDetail').and.returnValue(of(uniqueCardsData));
spyOn(component, 'syncOutboxExpenses');
spyOn(component, 'setAllExpensesCountAndAmount');
spyOn(component, 'clearFilters');
Expand Down Expand Up @@ -644,10 +646,10 @@ describe('MyExpensesV2Page', () => {
component.ionViewWillEnter();
tick(500);

expect(corporateCreditCardService.getAssignedCards).toHaveBeenCalledTimes(1);
expect(component.getCardDetail).toHaveBeenCalledOnceWith(expectedAssignedCCCStats.cardDetails);
expect(component.getCardDetail).toHaveBeenCalled();
expect(component.cardNumbers).toEqual([
{ label: '****8698', value: '8698' },
{ label: '****8698 (Business Card1)', value: '8698' },
{ label: '****8698 (Business Card2)', value: '8698' },
{ label: '****869', value: '869' },
]);
}));
Expand Down Expand Up @@ -1315,15 +1317,17 @@ describe('MyExpensesV2Page', () => {
});
});

it('getCardDetail(): should call corporateCreditCardService.getExpenseDetailsInCards method', () => {
corporateCreditCardService.getExpenseDetailsInCards.and.returnValue(expectedUniqueCardStats);
const getCardDetailRes = component.getCardDetail(cardAggregateStatParam);

expect(getCardDetailRes).toEqual(expectedUniqueCardStats);
expect(corporateCreditCardService.getExpenseDetailsInCards).toHaveBeenCalledOnceWith(
uniqueCardsParam,
cardAggregateStatParam
it('getCardDetail(): should call corporateCreditCardService.getCorporateCards() method', (done) => {
corporateCreditCardService.getCorporateCards.and.returnValue(
of([corporateCardsResponseData[0], corporateCardsResponseData[1]])
);
const getCardDetailRes$ = component.getCardDetail();

getCardDetailRes$.subscribe((data) => {
expect(data).toEqual([uniqueCardsData[0], uniqueCardsData[1]]);
done();
});
expect(corporateCreditCardService.getCorporateCards).toHaveBeenCalledTimes(1);
});

it('ionViewWillLeave(): should unsubscribe hardwareBackButton and update onPageExit', () => {
Expand Down
37 changes: 19 additions & 18 deletions src/app/fyle/my-expenses/my-expenses.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import {
takeUntil,
} from 'rxjs/operators';
import { BackButtonActionPriority } from 'src/app/core/models/back-button-action-priority.enum';
import { CardAggregateStats } from 'src/app/core/models/card-aggregate-stats.model';
import { Expense } from 'src/app/core/models/expense.model';
import { OrgSettings } from 'src/app/core/models/org-settings.model';
import { ExpenseFilters } from 'src/app/core/models/platform/expense-filters.model';
import { PlatformCategory } from 'src/app/core/models/platform/platform-category.model';
import { Expense as PlatformExpense } from 'src/app/core/models/platform/v1/expense.model';
import { GetExpenseQueryParam } from 'src/app/core/models/platform/v1/get-expenses-query.model';
import { UniqueCardStats } from 'src/app/core/models/unique-cards-stats.model';
import { UniqueCards } from 'src/app/core/models/unique-cards.model';
import { Transaction } from 'src/app/core/models/v1/transaction.model';
import { ApiV2Service } from 'src/app/core/services/api-v2.service';
Expand Down Expand Up @@ -407,18 +405,17 @@ export class MyExpensesPage implements OnInit {
}
}

getCardDetail(statsResponses: CardAggregateStats[]): UniqueCardStats[] {
const cardNames: { cardNumber: string; cardName: string }[] = [];
statsResponses.forEach((response) => {
const cardDetail = {
cardNumber: response.key[1].column_value,
cardName: response.key[0].column_value,
};
cardNames.push(cardDetail);
});
const uniqueCards = JSON.parse(JSON.stringify(cardNames)) as UniqueCards[];

return this.corporateCreditCardService.getExpenseDetailsInCards(uniqueCards, statsResponses);
getCardDetail(): Observable<UniqueCards[]> {
return this.corporateCreditCardService.getCorporateCards().pipe(
map((cards) => {
const simplifiedCards = cards.map((card) => ({
cardNumber: card.card_number,
cardName: card.bank_name,
cardNickname: card.nickname,
}));
return simplifiedCards;
})
);
}

ionViewWillLeave(): void {
Expand Down Expand Up @@ -490,12 +487,16 @@ export class MyExpensesPage implements OnInit {
})
.pipe(
filter(({ isConnected }) => isConnected),
switchMap(() => this.corporateCreditCardService.getAssignedCards())
switchMap(() => this.getCardDetail())
)
.subscribe((allCards) => {
const cards = this.getCardDetail(allCards.cardDetails);
.subscribe((cards) => {
cards.forEach((card) => {
this.cardNumbers.push({ label: this.maskNumber.transform(card.cardNumber), value: card.cardNumber });
const cardNickname = card.cardNickname ? ` (${card.cardNickname})` : '';
const cardDetail = {
label: this.maskNumber.transform(card.cardNumber) + cardNickname,
value: card.cardNumber,
};
this.cardNumbers.push(cardDetail);
});
});

Expand Down

0 comments on commit e04b5e8

Please sign in to comment.