diff --git a/apps/dashboard/src/actions/index.ts b/apps/dashboard/src/actions/index.ts index d1693c64b0..3534c5200e 100644 --- a/apps/dashboard/src/actions/index.ts +++ b/apps/dashboard/src/actions/index.ts @@ -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"; @@ -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}`, @@ -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}`, diff --git a/apps/dashboard/src/components/category.tsx b/apps/dashboard/src/components/category.tsx index aeddaacf68..bf2027c6a8 100644 --- a/apps/dashboard/src/components/category.tsx +++ b/apps/dashboard/src/components/category.tsx @@ -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: @@ -69,7 +69,7 @@ export function CategoryIcon({ name, size = 18 }) { case "taxes": return ; case "internet_and_telephone": - return ; + return ; case "facilities_expenses": return ; default: diff --git a/apps/dashboard/src/components/select-category.tsx b/apps/dashboard/src/components/select-category.tsx index 6f30688fd9..65d9717c99 100644 --- a/apps/dashboard/src/components/select-category.tsx +++ b/apps/dashboard/src/components/select-category.tsx @@ -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, }); diff --git a/apps/dashboard/src/locales/en.ts b/apps/dashboard/src/locales/en.ts index eee3453fc2..3b56168d9c 100644 --- a/apps/dashboard/src/locales/en.ts +++ b/apps/dashboard/src/locales/en.ts @@ -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", diff --git a/packages/supabase/src/mutations/index.ts b/packages/supabase/src/mutations/index.ts index 1129f71201..83bc5e493e 100644 --- a/packages/supabase/src/mutations/index.ts +++ b/packages/supabase/src/mutations/index.ts @@ -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) { @@ -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(); } @@ -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"); } @@ -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") diff --git a/packages/supabase/src/queries/index.ts b/packages/supabase/src/queries/index.ts index 5e3558c1b3..e21f888828 100644 --- a/packages/supabase/src/queries/index.ts +++ b/packages/supabase/src/queries/index.ts @@ -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") { @@ -324,7 +324,7 @@ export async function getTransaction(supabase: Client, id: string) { } type GetSimilarTransactionsParams = { - id: string; + name: string; teamId: string; }; @@ -332,19 +332,15 @@ 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(); } diff --git a/packages/ui/src/components/icons.tsx b/packages/ui/src/components/icons.tsx index 955597558e..a386b63af7 100644 --- a/packages/ui/src/components/icons.tsx +++ b/packages/ui/src/components/icons.tsx @@ -13,7 +13,6 @@ import { MdFlightTakeoff, MdHomeWork, MdInventory2, - MdLtePlusMobiledata, MdOutlineAccountBalanceWallet, MdOutlineCategory, MdOutlineDescription, @@ -28,6 +27,7 @@ import { MdPerson, MdRefresh, MdSave, + MdSensors, MdTrendingDown, MdTrendingUp, } from "react-icons/md"; @@ -314,6 +314,6 @@ export const Icons = { Category: MdOutlineCategory, Difference: MdOutlineDifference, Apartment: MdApartment, - LtePlusMobiledata: MdLtePlusMobiledata, + Sensors: MdSensors, DynamicForm: MdDynamicForm, };