Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunaj5 committed Oct 16, 2024
1 parent 54c017b commit b79145b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 92 deletions.
1 change: 1 addition & 0 deletions src/app/fyle/my-expenses/my-expenses.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ describe('MyExpensesPage', () => {
//@ts-ignore
spyOn(component, 'updateExpensesList').and.returnValue(apiExpenses1);
});

it('should call expenseService.getExpenses for dE incomplete expenses and return updated expenses', fakeAsync(() => {
const dEincompleteExpenseIds = ['txfCdl3TEZ7K', 'txfCdl3TEZ7l', 'txfCdl3TEZ7m'];
const dEincompleteExpenseIdParams: ExpensesQueryParams = {
Expand Down
183 changes: 91 additions & 92 deletions src/app/fyle/my-expenses/my-expenses.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { Expense as PlatformExpense } from 'src/app/core/models/platform/v1/expe
import { GetExpenseQueryParam } from 'src/app/core/models/platform/v1/get-expenses-query.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';
import { CategoriesService } from 'src/app/core/services/categories.service';
import { CorporateCreditCardExpenseService } from 'src/app/core/services/corporate-credit-card-expense.service';
import { CurrencyService } from 'src/app/core/services/currency.service';
Expand Down Expand Up @@ -451,97 +450,6 @@ export class MyExpensesPage implements OnInit {
}
}

private isZeroAmountPerDiemOrMileage(expense: PlatformExpense): boolean {
return (
(expense.category.name?.toLowerCase() === 'per diem' || expense.category.name?.toLowerCase() === 'mileage') &&
(expense.amount === 0 || expense.claim_amount === 0)
);
}

/**
* Checks if the scan process for an expense has been completed.
* @param {PlatformExpense} expense - The expense to check.
* @returns {boolean} - True if the scan is complete or if data is manually entered.
*/
private isExpenseScanComplete(expense: PlatformExpense): boolean {
const isZeroAmountPerDiemOrMileage = this.isZeroAmountPerDiemOrMileage(expense);

const hasUserManuallyEnteredData =
isZeroAmountPerDiemOrMileage ||
((expense.amount || expense.claim_amount) && isNumber(expense.amount || expense.claim_amount));
const isDataExtracted = !!expense.extracted_data;

// this is to prevent the scan failed from being shown from an indefinite amount of time.
const hasScanExpired = expense.created_at && dayjs(expense.created_at).diff(Date.now(), 'day') < 0;
return !!(hasUserManuallyEnteredData || isDataExtracted || hasScanExpired);
}

/**
* Filters the list of expenses to get only those with incomplete scans.
* @param {PlatformExpense[]} expenses - The list of expenses to check.
* @returns {string[]} - Array of expense IDs that have incomplete scans.
*/
private filterDEIncompleteExpenses(expenses: PlatformExpense[]): string[] {
return expenses.filter((expense) => !this.isExpenseScanComplete(expense)).map((expense) => expense.id);
}

/**
* Updates the expenses with polling results.
* @param {PlatformExpense[]} initialExpenses - The initial list of expenses.
* @param {PlatformExpense[]} updatedExpenses - The updated expenses after polling.
* @param {string[]} dEincompleteExpenseIds - Array of expense IDs with incomplete scans.
* @returns {PlatformExpense[]} - Updated list of expenses.
*/
private updateExpensesList(
initialExpenses: PlatformExpense[],
updatedExpenses: PlatformExpense[],
dEincompleteExpenseIds: string[]
): PlatformExpense[] {
const updatedExpensesMap = new Map(updatedExpenses.map((expense) => [expense.id, expense]));

const newExpensesList = initialExpenses.map((expense) => {
if (dEincompleteExpenseIds.includes(expense.id)) {
const updatedExpense = updatedExpensesMap.get(expense.id);
if (this.isExpenseScanComplete(updatedExpense)) {
return updatedExpense;
}
}
return expense;
});

return newExpensesList;
}

/**
* Polls for expenses that have incomplete scan data.
* @param {string[]} dEincompleteExpenseIds - Array of expense IDs with incomplete scans.
* @param {PlatformExpense[]} initialExpenses - The initial list of expenses.
* @returns {Observable<PlatformExpense[]>} - Observable that emits updated expenses.
*/
private pollDEIncompleteExpenses(
dEincompleteExpenseIds: string[],
expenses: PlatformExpense[]
): Observable<PlatformExpense[]> {
let updatedExpensesList = expenses;
// Create a stop signal that emits after 30 seconds
const stopPolling$ = timer(30000);
return timer(5000, 5000).pipe(
exhaustMap(() => {
const params: ExpensesQueryParams = { queryParams: { id: `in.(${dEincompleteExpenseIds.join(',')})` } };
return this.expenseService.getExpenses({ ...params.queryParams }).pipe(
map((updatedExpenses) => {
updatedExpensesList = this.updateExpensesList(updatedExpensesList, updatedExpenses, dEincompleteExpenseIds);
dEincompleteExpenseIds = this.filterDEIncompleteExpenses(updatedExpenses);
return updatedExpensesList;
})
);
}),
takeWhile(() => dEincompleteExpenseIds.length > 0, true),
takeUntil(stopPolling$),
takeUntil(this.onPageExit$)
);
}

ionViewWillEnter(): void {
this.isNewReportsFlowEnabled = false;
this.hardwareBackButton = this.platformHandlerService.registerBackButtonAction(
Expand Down Expand Up @@ -1873,4 +1781,95 @@ export class MyExpensesPage implements OnInit {
})
);
}

private isZeroAmountPerDiemOrMileage(expense: PlatformExpense): boolean {
return (
(expense.category.name?.toLowerCase() === 'per diem' || expense.category.name?.toLowerCase() === 'mileage') &&
(expense.amount === 0 || expense.claim_amount === 0)
);
}

/**
* Checks if the scan process for an expense has been completed.
* @param {PlatformExpense} expense - The expense to check.

Check failure on line 1794 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param

Check failure on line 1794 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param
* @returns {boolean} - True if the scan is complete or if data is manually entered.

Check failure on line 1795 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @returns

Check failure on line 1795 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @returns
*/
private isExpenseScanComplete(expense: PlatformExpense): boolean {
const isZeroAmountPerDiemOrMileage = this.isZeroAmountPerDiemOrMileage(expense);

const hasUserManuallyEnteredData =
isZeroAmountPerDiemOrMileage ||
((expense.amount || expense.claim_amount) && isNumber(expense.amount || expense.claim_amount));
const isDataExtracted = !!expense.extracted_data;

// this is to prevent the scan failed from being shown from an indefinite amount of time.
const hasScanExpired = expense.created_at && dayjs(expense.created_at).diff(Date.now(), 'day') < 0;
return !!(hasUserManuallyEnteredData || isDataExtracted || hasScanExpired);
}

/**
* Filters the list of expenses to get only those with incomplete scans.
* @param {PlatformExpense[]} expenses - The list of expenses to check.

Check failure on line 1812 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param

Check failure on line 1812 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param
* @returns {string[]} - Array of expense IDs that have incomplete scans.

Check failure on line 1813 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @returns

Check failure on line 1813 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @returns
*/
private filterDEIncompleteExpenses(expenses: PlatformExpense[]): string[] {
return expenses.filter((expense) => !this.isExpenseScanComplete(expense)).map((expense) => expense.id);
}

/**
* Updates the expenses with polling results.
* @param {PlatformExpense[]} initialExpenses - The initial list of expenses.

Check failure on line 1821 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param

Check failure on line 1821 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param
* @param {PlatformExpense[]} updatedExpenses - The updated expenses after polling.

Check failure on line 1822 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param

Check failure on line 1822 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param
* @param {string[]} dEincompleteExpenseIds - Array of expense IDs with incomplete scans.

Check failure on line 1823 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param

Check failure on line 1823 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @param
* @returns {PlatformExpense[]} - Updated list of expenses.

Check failure on line 1824 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @returns

Check failure on line 1824 in src/app/fyle/my-expenses/my-expenses.page.ts

View workflow job for this annotation

GitHub Actions / Run linters

Types are not permitted on @returns
*/
private updateExpensesList(
initialExpenses: PlatformExpense[],
updatedExpenses: PlatformExpense[],
dEincompleteExpenseIds: string[]
): PlatformExpense[] {
const updatedExpensesMap = new Map(updatedExpenses.map((expense) => [expense.id, expense]));

const newExpensesList = initialExpenses.map((expense) => {
if (dEincompleteExpenseIds.includes(expense.id)) {
const updatedExpense = updatedExpensesMap.get(expense.id);
if (this.isExpenseScanComplete(updatedExpense)) {
return updatedExpense;
}
}
return expense;
});

return newExpensesList;
}

/**
* Polls for expenses that have incomplete scan data.
* @param dEincompleteExpenseIds - Array of expense IDs with incomplete scans.
* @param initialExpenses - The initial list of expenses.
* @returns - Observable that emits updated expenses.
*/
private pollDEIncompleteExpenses(
dEincompleteExpenseIds: string[],
expenses: PlatformExpense[]
): Observable<PlatformExpense[]> {
let updatedExpensesList = expenses;
// Create a stop signal that emits after 30 seconds
const stopPolling$ = timer(30000);
return timer(5000, 5000).pipe(
exhaustMap(() => {
const params: ExpensesQueryParams = { queryParams: { id: `in.(${dEincompleteExpenseIds.join(',')})` } };
return this.expenseService.getExpenses({ ...params.queryParams }).pipe(
map((updatedExpenses) => {
updatedExpensesList = this.updateExpensesList(updatedExpensesList, updatedExpenses, dEincompleteExpenseIds);
dEincompleteExpenseIds = this.filterDEIncompleteExpenses(updatedExpenses);
return updatedExpensesList;
})
);
}),
takeWhile(() => dEincompleteExpenseIds.length > 0, true),
takeUntil(stopPolling$),
takeUntil(this.onPageExit$)
);
}
}

0 comments on commit b79145b

Please sign in to comment.