Skip to content

Commit

Permalink
feat: add hash ID generation when no id found
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Nov 19, 2023
1 parent 046f4ed commit 9651b3b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/translators/boa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TransactionImported } from "../utils/transaction.js";
import { Translator } from "./index.js";
import { convertStringCurrencyToNumber } from "../utils/money.js";
import { getFormattedDate } from "../utils/date.js";
import { generateHash } from "../utils/index.js";

const accountName = "BoA";
export const boaTranslator: Translator = {
Expand All @@ -11,9 +12,16 @@ export const boaTranslator: Translator = {
return null;
}

let transactionId = record[1].trim();
if (!transactionId) {
transactionId = generateHash(
`${record[0]}::${record[2]}::${record[3]}::${record[4]}`
);
}

// fin.
return {
id: record[1],
id: transactionId,
datePosted: getFormattedDate(new Date(record[0])),
account: accountName,
amount: convertStringCurrencyToNumber(record[4]),
Expand Down
10 changes: 9 additions & 1 deletion src/translators/nordstroms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getFormattedDate } from "../utils/date.js";
import { convertStringCurrencyToNumber } from "../utils/money.js";
import { TransactionImported } from "../utils/transaction.js";
import { Translator } from "./index.js";
import { generateHash } from "../utils/index.js";

const accountName = "Nordstroms";
export const nordstromsTranslator: Translator = {
Expand All @@ -14,9 +15,16 @@ export const nordstromsTranslator: Translator = {
return null;
}

let transactionId = record[2].trim();
if (!transactionId) {
transactionId = generateHash(
`${record[0]}::${record[1]}::${record[3]}::${record[4]}`
);
}

// fin.
return {
id: record[2],
id: transactionId,
datePosted: getFormattedDate(new Date(record[0])),
account: accountName,
amount: -convertStringCurrencyToNumber(record[4]),
Expand Down
10 changes: 2 additions & 8 deletions src/translators/scu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { createHash } from "node:crypto";

import { TransactionImported } from "../utils/transaction.js";
import { Translator } from "./index.js";
import { convertStringCurrencyToNumber } from "../utils/money.js";
import { getFormattedDate } from "../utils/date.js";
import { generateHash } from "../utils/index.js";

const accountName = "SCU";
export const scuTranslator: Translator = {
Expand All @@ -13,14 +12,9 @@ export const scuTranslator: Translator = {
return null;
}

// ID construction
const hash = createHash("sha256");
// date :: description :: amount :: balance
hash.update(`${record[0]}::${record[1]}::${record[4]}::${record[5]}`);

// fin.
return {
id: hash.digest("hex"),
id: generateHash(`${record[0]}::${record[1]}::${record[4]}::${record[5]}`),
datePosted: getFormattedDate(new Date(record[0])),
account: accountName,
amount: convertStringCurrencyToNumber(record[4]),
Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createHash } from "node:crypto";

export const hardNo = (message: string, error?: unknown): void => {
let errorMessage = "";
if (error instanceof Error) {
Expand All @@ -14,3 +16,9 @@ export const print = (message: string): void => {
export const padLeftZero = (string: number): string => {
return `${string}`.length === 1 ? `0${string}` : `${string}`;
};

export const generateHash = (hashString: string): string => {
const hash = createHash("sha256");
hash.update(hashString);
return hash.digest("hex");
};

0 comments on commit 9651b3b

Please sign in to comment.