Skip to content

Commit

Permalink
Merge pull request #174 from midday-ai/feature/amounts
Browse files Browse the repository at this point in the history
Transaction amounts
  • Loading branch information
pontusab authored Jun 28, 2024
2 parents b391e45 + b933b15 commit 00f23ae
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

exports[`Transform pending transaction 1`] = `
{
"amount": 5.4,
"amount": -5.4,
"balance": null,
"category": "income",
"category": "travel",
"currency": "CAD",
"date": "2024-02-24",
"description": null,
"description": "Uber",
"internal_id": "NxkDjlyk45cQoDm5PEqJuKJaw6qrj9cy89zBA",
"method": "other",
"name": "Uber 063015 SF**POOL**",
Expand All @@ -17,9 +17,9 @@ exports[`Transform pending transaction 1`] = `

exports[`Transform income transaction 1`] = `
{
"amount": 500,
"amount": -1500,
"balance": null,
"category": "income",
"category": "travel",
"currency": "CAD",
"date": "2024-02-22",
"description": null,
Expand All @@ -32,12 +32,12 @@ exports[`Transform income transaction 1`] = `

exports[`Transform type transfer 1`] = `
{
"amount": 5.4,
"amount": -31.53,
"balance": null,
"category": "transfer",
"currency": "CAD",
"date": "2024-02-24",
"description": null,
"description": "Uber",
"internal_id": "NxkDjlyk45cQoDm5PEqJuKJaw6qrj9cy89zBA",
"method": "transfer",
"name": "Uber 063015 SF**POOL**",
Expand Down
4 changes: 2 additions & 2 deletions apps/engine/src/providers/plaid/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test("Transform income transaction", () => {
transaction: {
account_id: "AG7EkLW7DRSVaN8Z75jMT1DJN51QpWc9LKB7w",
account_owner: null,
amount: -500,
amount: 1500,
authorized_date: "2024-02-22",
authorized_datetime: null,
category: ["Travel", "Airlines and Aviation Services"],
Expand Down Expand Up @@ -156,7 +156,7 @@ test("Transform type transfer", () => {
transaction: {
account_id: "AG7EkLW7DRSVaN8Z75jMT1DJN51QpWc9LKB7w",
account_owner: null,
amount: 5.4,
amount: 31.53,
authorized_date: "2024-02-23",
authorized_datetime: null,
category: ["Travel", "Taxi"],
Expand Down
38 changes: 24 additions & 14 deletions apps/engine/src/providers/plaid/transform.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
type AccountType,
formatAmountForAsset,
getType,
} from "@/utils/account";
import { type AccountType, getType } from "@/utils/account";
import { capitalCase } from "change-case";
import type { Transaction, TransactionCode } from "plaid";
import type {
Expand Down Expand Up @@ -125,28 +121,42 @@ export const mapTransactionCategory = ({
return null;
};

const formatAmout = (amount: number) => {
// Positive values when money moves out of the account; negative values when money moves in.
// For example, debit card purchases are positive; credit card payments, direct deposits, and refunds are negative.
return +(amount * -1);
};

const transformDescription = (transaction: Transaction) => {
if (transaction?.original_description) {
return capitalCase(transaction.original_description);
}

if (
transaction?.merchant_name &&
transaction?.merchant_name !== transaction.name
) {
return transaction?.merchant_name;
}

return null;
};

type TransformTransactionPayload = {
transaction: TransformTransaction;
accountType: AccountType;
};

export const transformTransaction = ({
transaction,
accountType,
}: TransformTransactionPayload): BaseTransaction => {
const method = mapTransactionMethod(transaction?.transaction_code);

const amount = formatAmountForAsset({
amount: transaction.amount,
type: accountType,
});
const amount = formatAmout(transaction.amount);

return {
date: transaction.date,
name: transaction.name,
description: transaction?.original_description
? capitalCase(transaction.original_description)
: null,
description: transformDescription(transaction),
method,
internal_id: transaction.transaction_id,
amount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`Transform pending transaction 1`] = `
{
"amount": 83.62,
"balance": null,
"category": "income",
"currency": "USD",
"date": "2024-03-05",
"description": "Bank Of Many",
"internal_id": "txn_os41r5u90e29shubl2000",
"method": "other",
"name": "Online Check Deposit",
"status": "pending",
}
`;

exports[`Transform card payment transaction 1`] = `
{
"amount": -68.9,
"balance": "83431.46",
"category": null,
"currency": "USD",
"date": "2024-03-01",
"description": "Nordstrom",
"internal_id": "txn_os41r5u90e29shubl2005",
"method": "card_purchase",
"name": "Nordstrom",
"status": "posted",
}
`;

exports[`Transform income transaction 1`] = `
{
"amount": -20.21,
"balance": "83296.40",
"category": null,
"currency": "USD",
"date": "2024-03-03",
"description": "Exxon Mobil",
"internal_id": "txn_os41r5u90e29shubl2002",
"method": "card_purchase",
"name": "Exxon Mobil",
"status": "posted",
}
`;

exports[`Transform type transfer 1`] = `
{
"amount": -37.99,
"balance": "85897.25",
"category": "transfer",
"currency": "USD",
"date": "2024-01-27",
"description": "Yourself",
"internal_id": "txn_os41r5ua0e29shubl2001",
"method": "transfer",
"name": "Recurring Transfer To Savings",
"status": "posted",
}
`;

exports[`Transform accounts 1`] = `
{
"currency": "USD",
Expand Down
152 changes: 80 additions & 72 deletions apps/engine/src/providers/teller/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,115 @@ import { transformAccount, transformTransaction } from "./transform";
test("Transform pending transaction", () => {
expect(
transformTransaction({
type: "check",
status: "pending",
running_balance: null,
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5u90e29shubl2000",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5u90e29shubl2000",
details: {
processing_status: "complete",
counterparty: {
type: "organization",
name: "BANK OF MANY",
transaction: {
type: "check",
status: "pending",
running_balance: null,
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5u90e29shubl2000",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5u90e29shubl2000",
details: {
processing_status: "complete",
counterparty: {
type: "organization",
name: "BANK OF MANY",
},
category: "general",
},
category: "general",
description: "Online Check Deposit",
date: "2024-03-05",
amount: "83.62",
account_id: "acc_os41qe3a66ks2djhss000",
},
description: "Online Check Deposit",
date: "2024-03-05",
amount: "83.62",
account_id: "acc_os41qe3a66ks2djhss000",
})
).toMatchSnapshot();
});

test("Transform card payment transaction", () => {
expect(
transformTransaction({
type: "card_payment",
status: "posted",
running_balance: "83431.46",
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5u90e29shubl2005",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5u90e29shubl2005",
details: {
processing_status: "complete",
counterparty: {
type: "organization",
name: "NORDSTROM",
transaction: {
type: "card_payment",
status: "posted",
running_balance: "83431.46",
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5u90e29shubl2005",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5u90e29shubl2005",
details: {
processing_status: "complete",
counterparty: {
type: "organization",
name: "NORDSTROM",
},
category: "shopping",
},
category: "shopping",
description: "Nordstrom",
date: "2024-03-01",
amount: "-68.90",
account_id: "acc_os41qe3a66ks2djhss000",
},
description: "Nordstrom",
date: "2024-03-01",
amount: "-68.90",
account_id: "acc_os41qe3a66ks2djhss000",
})
).toMatchSnapshot();
});

test("Transform income transaction", () => {
expect(
transformTransaction({
type: "card_payment",
status: "posted",
running_balance: "83296.40",
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5u90e29shubl2002",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5u90e29shubl2002",
details: {
processing_status: "complete",
counterparty: {
type: "organization",
name: "EXXON MOBIL",
transaction: {
type: "card_payment",
status: "posted",
running_balance: "83296.40",
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5u90e29shubl2002",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5u90e29shubl2002",
details: {
processing_status: "complete",
counterparty: {
type: "organization",
name: "EXXON MOBIL",
},
category: "fuel",
},
category: "fuel",
description: "Exxon Mobil",
date: "2024-03-03",
amount: "-20.21",
account_id: "acc_os41qe3a66ks2djhss000",
},
description: "Exxon Mobil",
date: "2024-03-03",
amount: "-20.21",
account_id: "acc_os41qe3a66ks2djhss000",
})
).toMatchSnapshot();
});

test("Transform type transfer", () => {
expect(
transformTransaction({
type: "transfer",
status: "posted",
running_balance: "85897.25",
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5ua0e29shubl2001",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5ua0e29shubl2001",
details: {
processing_status: "complete",
counterparty: {
type: "person",
name: "YOURSELF",
transaction: {
type: "transfer",
status: "posted",
running_balance: "85897.25",
links: {
self: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000/transactions/txn_os41r5ua0e29shubl2001",
account: "https://api.teller.io/accounts/acc_os41qe3a66ks2djhss000",
},
id: "txn_os41r5ua0e29shubl2001",
details: {
processing_status: "complete",
counterparty: {
type: "person",
name: "YOURSELF",
},
category: "general",
},
category: "general",
description: "Recurring Transfer to Savings",
date: "2024-01-27",
amount: "-37.99",
account_id: "acc_os41qe3a66ks2djhss000",
},
description: "Recurring Transfer to Savings",
date: "2024-01-27",
amount: "-37.99",
account_id: "acc_os41qe3a66ks2djhss000",
})
).toMatchSnapshot();
});
Expand Down
Loading

0 comments on commit 00f23ae

Please sign in to comment.