Skip to content

Commit

Permalink
(feat) Add helper functions to get total bill and balance (#73)
Browse files Browse the repository at this point in the history
CynthiaKamau authored Nov 30, 2023
1 parent 21d2703 commit 523b539
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 0 additions & 1 deletion __mocks__/bills.mock.ts
Original file line number Diff line number Diff line change
@@ -70,7 +70,6 @@ export const billsSummary = [
description: 'Mpesa',
retired: false,
},

attributes: [
{
uuid: '84aacfcc-9788-4f7b-a56a-407d84af1b54',
32 changes: 32 additions & 0 deletions packages/esm-billing-app/src/helpers/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Payment, LineItem } from '../types';

// amount already paid
export function calculateTotalAmountTendered(payments: Array<Payment>) {
return Array.isArray(payments)
? payments.reduce((totalAmount, item) => {
// Ensure that "amount" property is present and numeric
if (typeof item.amount === 'number' && item.voided !== true) {
return totalAmount + item.amount;
}
return totalAmount;
}, 0)
: 0;
}

// balance
export function calculateTotalBalance(lineItems: Array<LineItem>, payments: Array<Payment>) {
return Math.min(this.calculateTotalAmount(lineItems) - this.calculateTotalAmountTendered(payments));
}

// total bill
export function calculateTotalAmount(lineItems: Array<LineItem>) {
return Array.isArray(lineItems)
? lineItems.reduce((totalAmount, item) => {
// Ensure that "price" and "quantity" properties are present and numeric
if (typeof item.price === 'number' && typeof item.quantity === 'number' && item.voided !== true) {
return totalAmount + item.price * item.quantity;
}
return totalAmount;
}, 0)
: 0;
}
1 change: 1 addition & 0 deletions packages/esm-billing-app/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './functions';
4 changes: 2 additions & 2 deletions packages/esm-billing-app/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ interface Provider {
links: ProviderLink[];
}

interface LineItem {
export interface LineItem {
uuid: string;
display: string;
voided: boolean;
@@ -103,7 +103,7 @@ interface PaymentInstanceType {
retired: boolean;
}

interface Payment {
export interface Payment {
uuid: string;
instanceType: PaymentInstanceType;
attributes: Attribute[];

0 comments on commit 523b539

Please sign in to comment.