Skip to content

Commit

Permalink
fix: Move My Expenses To Platform: Fixing Delete Button (#2667)
Browse files Browse the repository at this point in the history
* fixing delete button

* fixed bug -2

* increased branch coverage

* increasing branch coverage -2

* increased branch coverage -3

---------

Co-authored-by: Jay Budhadev <[email protected]>
  • Loading branch information
jayfyle and Jay Budhadev authored Jan 10, 2024
1 parent 1e36088 commit 44417f3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class ExpensesService {

getExpenseDeletionMessage(expensesToBeDeleted: Expense[]): string {
return `You are about to permanently delete ${
expensesToBeDeleted.length === 1 ? '1 selected expense.' : expensesToBeDeleted.length + ' selected expenses.'
expensesToBeDeleted?.length === 1 ? '1 selected expense.' : expensesToBeDeleted?.length + ' selected expenses.'
}`;
}

Expand Down
33 changes: 25 additions & 8 deletions src/app/core/services/transaction.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,8 +1146,14 @@ describe('TransactionService', () => {
});
});

it('getDeletableTxns(): should return deletable transactions', () => {
expect(transactionService.getDeletableTxns(apiExpenseRes)).toEqual(apiExpenseRes);
describe('getDeletableTxns():', () => {
it('should return deletable transactions', () => {
expect(transactionService.getDeletableTxns(apiExpenseRes)).toEqual(apiExpenseRes);
});

it('should return undefined if no transaction ', () => {
expect(transactionService.getDeletableTxns(undefined)).toBeUndefined();
});
});

describe('getExpenseDeletionMessage():', () => {
Expand Down Expand Up @@ -1376,13 +1382,24 @@ describe('TransactionService', () => {
expect(transactionService.excludeCCCExpenses(expenseList3)).toEqual([expenseList3[1]]);
});

it('getReportableExpenses(): should return reportable expenses', () => {
spyOn(transactionService, 'getIsCriticalPolicyViolated').and.returnValue(false);
spyOn(transactionService, 'getIsDraft').and.returnValue(false);
describe('getReportableExpenses(): ', () => {
it('should return reportable expenses', () => {
spyOn(transactionService, 'getIsCriticalPolicyViolated').and.returnValue(false);
spyOn(transactionService, 'getIsDraft').and.returnValue(false);

expect(transactionService.getReportableExpenses(apiExpenseRes)).toEqual([apiExpenseRes[0]]);
expect(transactionService.getIsCriticalPolicyViolated).toHaveBeenCalledOnceWith(apiExpenseRes[0]);
expect(transactionService.getIsDraft).toHaveBeenCalledOnceWith(apiExpenseRes[0]);
expect(transactionService.getReportableExpenses(apiExpenseRes)).toEqual([apiExpenseRes[0]]);
expect(transactionService.getIsCriticalPolicyViolated).toHaveBeenCalledOnceWith(apiExpenseRes[0]);
expect(transactionService.getIsDraft).toHaveBeenCalledOnceWith(apiExpenseRes[0]);
});

it('should return undefined if expenses are 0', () => {
spyOn(transactionService, 'getIsCriticalPolicyViolated').and.returnValue(false);
spyOn(transactionService, 'getIsDraft').and.returnValue(false);

expect(transactionService.getReportableExpenses(null)).toEqual(undefined);
expect(transactionService.getIsCriticalPolicyViolated).not.toHaveBeenCalled();
expect(transactionService.getIsDraft).not.toHaveBeenCalled();
});
});

it('matchCCCExpense(): should match ccc expense', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ export class TransactionService {
}

getReportableExpenses(expenses: Partial<Expense>[]): Partial<Expense>[] {
return expenses.filter(
return expenses?.filter(
(expense) => !this.getIsCriticalPolicyViolated(expense) && !this.getIsDraft(expense) && expense.tx_id
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/fyle/my-expenses-v2/my-expenses-v2.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<ng-container *ngIf="homeCurrency$ | async as homeCurrency">
<ng-container *ngIf="allExpensesStats$ | async as allExpensesStats">
<ion-title class="my-expenses--multiselect-title page-title">
{{selectedElements?.length}} {{(selectedElements?.length > 1) ? 'expenses' : 'expense'}} worth
{{allExpensesStats?.count}} {{(allExpensesStats?.count > 1) ? 'expenses' : 'expense'}} worth
{{(allExpensesStats.amount || 0) | humanizeCurrency: homeCurrency }}
</ion-title>
</ng-container>
Expand Down
109 changes: 57 additions & 52 deletions src/app/fyle/my-expenses-v2/my-expenses-v2.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class MyExpensesV2Page implements OnInit {

selectedElements: PlatformExpense[];

selectedOutboxExpenses: Partial<Expense>[];
selectedOutboxExpenses: Partial<Expense>[] = [];

syncing = false;

Expand Down Expand Up @@ -1332,31 +1332,34 @@ export class MyExpensesV2Page implements OnInit {
}

deleteSelectedExpenses(offlineExpenses: Partial<Expense>[]): Observable<Transaction[]> {
if (offlineExpenses?.length) {
if (offlineExpenses?.length > 0) {
this.transactionOutboxService.deleteBulkOfflineExpenses(this.pendingTransactions, offlineExpenses);
}
this.selectedElements = this.expensesToBeDeleted.filter((expense) => expense.id);
if (this.selectedElements.length > 0) {
return this.transactionService.deleteBulk(this.selectedElements.map((selectedExpense) => selectedExpense.id));
} else {
return of(null);
} else {
this.selectedElements = this.expensesToBeDeleted.filter((expense) => expense.id);
if (this.selectedElements.length > 0) {
return this.transactionService.deleteBulk(this.selectedElements.map((selectedExpense) => selectedExpense.id));
} else {
return of(null);
}
}
}

async openDeleteExpensesPopover(): Promise<void> {
const offlineExpenses = this.outboxExpensesToBeDeleted.filter((expense) => !expense.tx_id);

const expenseDeletionMessage = this.sharedExpenseService.getExpenseDeletionMessage(this.expensesToBeDeleted);
let expenseDeletionMessage: string;
let cccExpensesMessage: string;
let totalDeleteLength = 0;

const cccExpensesMessage = this.sharedExpenseService.getCCCExpenseMessage(
this.expensesToBeDeleted,
this.cccExpenses
);

let totalDeleteLength = this.expensesToBeDeleted?.length;

if (this.outboxExpensesToBeDeleted.length > 0) {
totalDeleteLength = totalDeleteLength + this.outboxExpensesToBeDeleted.length;
if (offlineExpenses.length > 0) {
expenseDeletionMessage = this.transactionService.getExpenseDeletionMessage(offlineExpenses);
cccExpensesMessage = this.transactionService.getCCCExpenseMessage(offlineExpenses, this.cccExpenses);
totalDeleteLength = this.outboxExpensesToBeDeleted.length;
} else {
expenseDeletionMessage = this.sharedExpenseService.getExpenseDeletionMessage(this.expensesToBeDeleted);
cccExpensesMessage = this.sharedExpenseService.getCCCExpenseMessage(this.expensesToBeDeleted, this.cccExpenses);
totalDeleteLength = this.expensesToBeDeleted?.length;
}

const deletePopover = await this.popoverController.create({
Expand Down Expand Up @@ -1428,42 +1431,44 @@ export class MyExpensesV2Page implements OnInit {
this.allExpensesCount = this.pendingTransactions.length;
this.isReportableExpensesSelected =
this.transactionService.getReportableExpenses(this.selectedOutboxExpenses).length > 0;
this.outboxExpensesToBeDeleted = this.transactionService.getDeletableTxns(this.selectedOutboxExpenses);
this.outboxExpensesToBeDeleted = this.selectedOutboxExpenses;
this.setOutboxExpenseStatsOnSelect();
}

this.loadExpenses$
.pipe(
take(1),
map((params) => {
const queryParams = params.queryParams || {};

queryParams.report_id = queryParams.report_id || 'is.null';
queryParams.state = 'in.(COMPLETE,DRAFT)';
if (params.searchString) {
queryParams.q = params?.searchString + ':*';
}

return queryParams;
}),
switchMap((queryParams) => this.expenseService.getAllExpenses({ queryParams }))
)
.subscribe((allExpenses) => {
this.selectedElements = this.selectedElements.concat(allExpenses);
if (this.selectedElements.length > 0) {
if (this.outboxExpensesToBeDeleted?.length) {
this.outboxExpensesToBeDeleted = this.transactionService.getDeletableTxns(this.outboxExpensesToBeDeleted);
} else {
this.loadExpenses$
.pipe(
take(1),
map((params) => {
const queryParams = params.queryParams || {};

queryParams.report_id = queryParams.report_id || 'is.null';
queryParams.state = 'in.(COMPLETE,DRAFT)';
if (params.searchString) {
queryParams.q = params?.searchString + ':*';
}

return queryParams;
}),
switchMap((queryParams) => this.expenseService.getAllExpenses({ queryParams }))
)
.subscribe((allExpenses) => {
this.selectedElements = this.selectedElements.concat(allExpenses);
if (this.selectedElements.length > 0) {
if (this.outboxExpensesToBeDeleted?.length) {
this.outboxExpensesToBeDeleted = this.transactionService.getDeletableTxns(
this.outboxExpensesToBeDeleted
);
}

this.expensesToBeDeleted = this.sharedExpenseService.excludeCCCExpenses(this.selectedElements);

this.cccExpenses = this.selectedElements.length - this.expensesToBeDeleted.length;
}

this.expensesToBeDeleted = this.sharedExpenseService.excludeCCCExpenses(this.selectedElements);

this.cccExpenses = this.selectedElements.length - this.expensesToBeDeleted.length;
}
this.allExpensesCount = this.selectedElements.length;
this.isReportableExpensesSelected =
this.sharedExpenseService.getReportableExpenses(this.selectedElements).length > 0;
this.setExpenseStatsOnSelect();
});
this.allExpensesCount = this.selectedElements.length;
this.isReportableExpensesSelected =
this.sharedExpenseService.getReportableExpenses(this.selectedElements).length > 0;
this.setExpenseStatsOnSelect();
});
}
} else {
this.selectedElements = [];
this.selectedOutboxExpenses = [];
Expand Down Expand Up @@ -1579,7 +1584,7 @@ export class MyExpensesV2Page implements OnInit {
!this.expensesToBeDeleted ||
(this.expensesToBeDeleted?.length === 0 && this.cccExpenses > 0);
} else if (!isConnected) {
this.isDisabled = this.selectedOutboxExpenses?.length === 0 || !this.outboxExpensesToBeDeleted;
this.isDisabled = this.selectedOutboxExpenses.length === 0 || !this.outboxExpensesToBeDeleted;
}
})
);
Expand Down

0 comments on commit 44417f3

Please sign in to comment.