Skip to content

Commit

Permalink
Move transform
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Mar 4, 2024
1 parent 3e9c8a6 commit 8afc4ab
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 111 deletions.
109 changes: 0 additions & 109 deletions packages/gocardless/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use server";

import { client } from "@midday/kv";
import { capitalCase } from "change-case";

const baseUrl = "https://bankaccountdata.gocardless.com";

Expand Down Expand Up @@ -278,111 +277,3 @@ export async function deleteRequisition(id: string) {

return result.json();
}

export const mapTransactionMethod = (method: string) => {
switch (method) {
case "Payment":
case "Bankgiro payment":
case "Incoming foreign payment":
return "payment";
case "Card purchase":
case "Card foreign purchase":
return "card_purchase";
case "Card ATM":
return "card_atm";
case "Transfer":
return "transfer";
default:
return "other";
}
};

const transformName = (transaction) => {
if (transaction?.additionalInformation) {
return capitalCase(transaction.additionalInformation);
}

if (transaction?.remittanceInformationStructured) {
return capitalCase(transaction.remittanceInformationStructured);
}

if (transaction?.remittanceInformationUnstructured) {
return capitalCase(transaction.remittanceInformationUnstructured);
}

if (transaction?.creditorName) {
return capitalCase(transaction.creditorName);
}

if (transaction?.debtorName) {
return capitalCase(transaction?.debtorName);
}

if (transaction?.remittanceInformationUnstructuredArray?.at(0)) {
return capitalCase(
transaction.remittanceInformationUnstructuredArray?.at(0)
);
}

console.log("No transaction name", transaction);
};

const transformDescription = (transaction, name) => {
if (transaction?.remittanceInformationUnstructuredArray?.length) {
const text = transaction?.remittanceInformationUnstructuredArray.join(" ");
const description = capitalCase(text);

// NOTE: Sometimes the description is the same as name
// Let's skip that and just save if they are not the same
if (description !== name) {
return description;
}
}
};

export const transformTransactions = (transactions, { teamId, accountId }) => {
// We want to insert transactions in reversed order so the incremental id in supabase is correct
return transactions?.reverse().map((transaction) => {
const method = mapTransactionMethod(
transaction.proprietaryBankTransactionCode
);

let currencyExchange: { rate: number; currency: string } | undefined;

if (Array.isArray(transaction.currencyExchange)) {
const rate = Number.parseFloat(
transaction.currencyExchange.at(0)?.exchangeRate ?? ""
);

if (rate) {
const currency = transaction.currencyExchange.at(0)?.sourceCurrency;

if (currency) {
currencyExchange = {
rate,
currency,
};
}
}
}

const name = transformName(transaction);

return {
date: transaction.valueDate,
name,
method: method || "unknown",
internal_id: `${teamId}_${transaction.internalTransactionId}`,
amount: transaction.transactionAmount.amount,
currency: transaction.transactionAmount.currency,
bank_account_id: accountId,
category: transaction.transactionAmount.amount > 0 ? "income" : null,
team_id: teamId,
currency_rate: currencyExchange?.rate,
currency_source: currencyExchange?.currency,
balance: transaction?.balanceAfterTransaction?.balanceAmount?.amount,
description: transformDescription(transaction, name),
status: "posted",
};
});
};
109 changes: 109 additions & 0 deletions packages/gocardless/src/transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { capitalCase } from "change-case";

export const mapTransactionMethod = (method: string) => {
switch (method) {
case "Payment":
case "Bankgiro payment":
case "Incoming foreign payment":
return "payment";
case "Card purchase":
case "Card foreign purchase":
return "card_purchase";
case "Card ATM":
return "card_atm";
case "Transfer":
return "transfer";
default:
return "other";
}
};

const transformName = (transaction) => {
if (transaction?.additionalInformation) {
return capitalCase(transaction.additionalInformation);
}

if (transaction?.remittanceInformationStructured) {
return capitalCase(transaction.remittanceInformationStructured);
}

if (transaction?.remittanceInformationUnstructured) {
return capitalCase(transaction.remittanceInformationUnstructured);
}

if (transaction?.creditorName) {
return capitalCase(transaction.creditorName);
}

if (transaction?.debtorName) {
return capitalCase(transaction?.debtorName);
}

if (transaction?.remittanceInformationUnstructuredArray?.at(0)) {
return capitalCase(
transaction.remittanceInformationUnstructuredArray?.at(0)
);
}

console.log("No transaction name", transaction);
};

const transformDescription = (transaction, name) => {
if (transaction?.remittanceInformationUnstructuredArray?.length) {
const text = transaction?.remittanceInformationUnstructuredArray.join(" ");
const description = capitalCase(text);

// NOTE: Sometimes the description is the same as name
// Let's skip that and just save if they are not the same
if (description !== name) {
return description;
}
}
};

export const transformTransactions = (transactions, { teamId, accountId }) => {
// We want to insert transactions in reversed order so the incremental id in supabase is correct
return transactions?.reverse().map((transaction) => {
const method = mapTransactionMethod(
transaction.proprietaryBankTransactionCode
);

let currencyExchange: { rate: number; currency: string } | undefined;

if (Array.isArray(transaction.currencyExchange)) {
const rate = Number.parseFloat(
transaction.currencyExchange.at(0)?.exchangeRate ?? ""
);

if (rate) {
const currency = transaction.currencyExchange.at(0)?.sourceCurrency;

if (currency) {
currencyExchange = {
rate,
currency,
};
}
}
}

const name = transformName(transaction);

return {
date: transaction.valueDate,
name,
method: method || "unknown",
internal_id: `${teamId}_${transaction.internalTransactionId}`,
amount: transaction.transactionAmount.amount,
currency: transaction.transactionAmount.currency,
bank_account_id: accountId,
category: transaction.transactionAmount.amount > 0 ? "income" : null,
team_id: teamId,
currency_rate: currencyExchange?.rate,
currency_source: currencyExchange?.currency,
balance: transaction?.balanceAfterTransaction?.balanceAmount?.amount,
description: transformDescription(transaction, name),
status: "posted",
};
});
};
3 changes: 2 additions & 1 deletion packages/jobs/src/transactions/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { processPromisesBatch } from "@/utils/process";
import { getTransactions, transformTransactions } from "@midday/gocardless";
import { getTransactions } from "@midday/gocardless";
import { transformTransactions } from "@midday/gocardless/src/transform";
import { eventTrigger } from "@trigger.dev/sdk";
import { revalidateTag } from "next/cache";
import { z } from "zod";
Expand Down
3 changes: 2 additions & 1 deletion packages/jobs/src/transactions/sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getTransactions, transformTransactions } from "@midday/gocardless";
import { getTransactions } from "@midday/gocardless";
import { transformTransactions } from "@midday/gocardless/src/transform";
import { formatISO, subMonths } from "date-fns";
import { revalidateTag } from "next/cache";
import { client, supabase } from "../client";
Expand Down

0 comments on commit 8afc4ab

Please sign in to comment.