Skip to content

Commit

Permalink
Categories
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Nov 27, 2023
1 parent 9f515fd commit 6abcd61
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 22 deletions.
15 changes: 13 additions & 2 deletions apps/dashboard/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use server";

import { env } from "@/env.mjs";
import { getUser } from "@midday/supabase/cached-queries";
import {
createBankAccounts,
createEnrichmentTransaction,
updateSimilarTransactions,
updateTransaction,
} from "@midday/supabase/mutations";
Expand Down Expand Up @@ -68,6 +70,14 @@ export async function updateTransactionAction(id: string, payload: any) {
const supabase = await createClient();
const { data } = await updateTransaction(supabase, id, payload);

// Add category to global enrichment_transactions
if (data?.category) {
createEnrichmentTransaction(supabase, {
name: data.name,
category: data.category,
});
}

invalidateCacheAction([
`transactions_${data.team_id}`,
`spending_${data.team_id}`,
Expand All @@ -77,8 +87,9 @@ export async function updateTransactionAction(id: string, payload: any) {

export async function updateSimilarTransactionsAction(id: string) {
const supabase = await createClient();
const { data } = await updateSimilarTransactions(supabase, id);
const teamId = data.at(0).team_id;
await updateSimilarTransactions(supabase, id);
const user = await getUser();
const teamId = user.data.team_id;

invalidateCacheAction([
`transactions_${teamId}`,
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/src/components/category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const mapCategoryColor = (name: string) => {
case "uncategorized":
return "#606060";
case "taxes":
return "#A0A8DF";
return "#0086A1";
case "internet_and_telephone":
return "#00524C";
return "#FF8976";
case "facilities_expenses":
return "#A8AABC";
default:
Expand Down Expand Up @@ -69,7 +69,7 @@ export function CategoryIcon({ name, size = 18 }) {
case "taxes":
return <Icons.Apartment style={{ color }} size={size} />;
case "internet_and_telephone":
return <Icons.LtePlusMobiledata style={{ color }} size={size} />;
return <Icons.Sensors style={{ color }} size={size} />;
case "facilities_expenses":
return <Icons.DynamicForm style={{ color }} size={size} />;
default:
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/components/select-category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function SelectCategory({ id, name, selectedId, isLoading }) {
await updateTransactionAction(id, { category: value });
const { data: userData } = await getCurrentUserTeamQuery(supabase);
const transactions = await getSimilarTransactions(supabase, {
id,
name,
teamId: userData?.team_id,
});

Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
travel: "Travel",
software: "Software",
office_supplies: "Office Supplies",
internet_and_telephone: "Internet and Telephone",
internet_and_telephone: "Internet & Telephone",
rent: "Rent",
equipment: "Equipment",
income: "Income",
Expand Down
30 changes: 27 additions & 3 deletions packages/supabase/src/mutations/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addDays } from "date-fns";
import { getCurrentUserTeamQuery, getSession } from "../queries";
import { Client } from "../types";
import { Client, Database } from "../types";
import { remove } from "../utils/storage";

export async function createBankAccounts(supabase: Client, accounts) {
Expand Down Expand Up @@ -91,7 +91,7 @@ export async function updateTransaction(
.from("transactions")
.update(data)
.eq("id", id)
.select("id, team_id")
.select("id, team_id, category, name")
.single();
}

Expand Down Expand Up @@ -142,7 +142,8 @@ export async function updateSimilarTransactions(supabase: Client, id: string) {
.update({ category: transaction.data.category })
.eq("name", transaction.data.name)
.eq("team_id", userData?.team_id)
.eq("category", "uncategorized")
.is("category", null)
.is("enrichment_id", null)
.select("id, team_id");
}

Expand Down Expand Up @@ -173,6 +174,29 @@ export async function createAttachments(
return data;
}

type CreateEnrichmentTransactionParams = {
name: string;
category: Database["public"]["Enums"]["transactionCategories"];
};

export async function createEnrichmentTransaction(
supabase: Client,
params: CreateEnrichmentTransactionParams
) {
const { data: userData } = await getCurrentUserTeamQuery(supabase);

const { data } = await supabase
.from("transaction_enrichments")
.insert({
name: params.name,
category: params.category,
created_by: userData?.id,
})
.select();

return data;
}

export async function deleteAttachment(supabase: Client, id: string) {
const { data } = await supabase
.from("attachments")
Expand Down
16 changes: 6 additions & 10 deletions packages/supabase/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export async function getTransactionsQuery(
}

if (category === "exclude") {
query.eq("category", "uncategorized");
query.is("category", null).is("enrichment_id", null);
}

if (category === "include") {
Expand Down Expand Up @@ -324,27 +324,23 @@ export async function getTransaction(supabase: Client, id: string) {
}

type GetSimilarTransactionsParams = {
id: string;
name: string;
teamId: string;
};

export async function getSimilarTransactions(
supabase: Client,
params: GetSimilarTransactionsParams
) {
const { id, teamId } = params;
const transaction = await supabase
.from("transactions")
.select("name, category")
.eq("id", id)
.single();
const { name, teamId } = params;

return supabase
.from("transactions")
.select("id, amount", { count: "exact" })
.eq("name", transaction.data.name)
.eq("name", name)
.eq("team_id", teamId)
.eq("category", "uncategorized")
.is("category", null)
.is("enrichment_id", null)
.throwOnError();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/components/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
MdFlightTakeoff,
MdHomeWork,
MdInventory2,
MdLtePlusMobiledata,
MdOutlineAccountBalanceWallet,
MdOutlineCategory,
MdOutlineDescription,
Expand All @@ -28,6 +27,7 @@ import {
MdPerson,
MdRefresh,
MdSave,
MdSensors,
MdTrendingDown,
MdTrendingUp,
} from "react-icons/md";
Expand Down Expand Up @@ -314,6 +314,6 @@ export const Icons = {
Category: MdOutlineCategory,
Difference: MdOutlineDifference,
Apartment: MdApartment,
LtePlusMobiledata: MdLtePlusMobiledata,
Sensors: MdSensors,
DynamicForm: MdDynamicForm,
};

0 comments on commit 6abcd61

Please sign in to comment.