Skip to content

Commit

Permalink
feat: allow for multiple descriptions in autocategorization
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Feb 27, 2024
1 parent 97d9f8d commit a7b7d14
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Allowance {
}

export interface AutoCategorization {
description?: string;
descriptions?: string[];
amount?: {
gt?: number;
gte?: number;
Expand Down
16 changes: 11 additions & 5 deletions src/utils/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ describe("Function: autoCategorize", () => {
it("auto-categorizes when the description is matched", () => {
mockTransaction.description = "__TEST_DESCRIPTION__";
mockConfig.autoCategorization = [{ ...mockAutoCategorization }];
mockConfig.autoCategorization[0].description = "TEST_DESCRIPTION";
mockConfig.autoCategorization[0].descriptions = [
"TEST_DESCRIPTION",
"NOT_MATCHING",
];

expect(autoCategorize(mockTransaction, mockConfig)).toEqual(
mockConfig.autoCategorization[0].categorization
Expand All @@ -46,7 +49,10 @@ describe("Function: autoCategorize", () => {
mockTransaction.description = "__TEST_DESCRIPTION__";

mockConfig.autoCategorization = [{ ...mockAutoCategorization }];
mockConfig.autoCategorization[0].description = "NOT_MATCHING";
mockConfig.autoCategorization[0].descriptions = [
"NOT_MATCHING",
"ALSO_NOT_MATCHING",
];

expect(autoCategorize(mockTransaction, mockConfig)).toBeNull();
});
Expand Down Expand Up @@ -136,7 +142,7 @@ describe("Function: autoCategorize", () => {
mockTransaction.amount = 150;

mockConfig.autoCategorization = [{ ...mockAutoCategorization }];
mockConfig.autoCategorization[0].description = "TEST_DESCRIPTION";
mockConfig.autoCategorization[0].descriptions = ["TEST_DESCRIPTION"];
mockConfig.autoCategorization[0].amount = {
gt: 100,
lt: 200,
Expand All @@ -152,7 +158,7 @@ describe("Function: autoCategorize", () => {
mockTransaction.amount = 150;

mockConfig.autoCategorization = [{ ...mockAutoCategorization }];
mockConfig.autoCategorization[0].description = "NOT_MATCHED";
mockConfig.autoCategorization[0].descriptions = ["NOT_MATCHED"];
mockConfig.autoCategorization[0].amount = {
gt: 100,
lt: 200,
Expand All @@ -166,7 +172,7 @@ describe("Function: autoCategorize", () => {
mockTransaction.amount = 250;

mockConfig.autoCategorization = [{ ...mockAutoCategorization }];
mockConfig.autoCategorization[0].description = "TEST_DESCRIPTION";
mockConfig.autoCategorization[0].descriptions = ["TEST_DESCRIPTION"];
mockConfig.autoCategorization[0].amount = {
gt: 100,
lt: 200,
Expand Down
10 changes: 7 additions & 3 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ export const autoCategorize = (
}

for (const matchCriteria of config.autoCategorization) {
const { description, amount, categorization } = matchCriteria;
const { descriptions, amount, categorization } = matchCriteria;

let matchedDescription = true;
if (description) {
matchedDescription = transaction.description.includes(description);
if (descriptions && descriptions.length) {
matchedDescription = false;
for (const description of descriptions) {
matchedDescription =
matchedDescription || transaction.description.includes(description);
}
}

let matchedAmount = true;
Expand Down

0 comments on commit a7b7d14

Please sign in to comment.