Skip to content

Commit

Permalink
Add total balance
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 1, 2024
1 parent c5611d8 commit 383cd10
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getTeamBankAccounts } from "@midday/supabase/cached-queries";
import { getBankAccountsBalances } from "@midday/supabase/cached-queries";
import { AccountBalance } from "./account-balance";

export function AccountBalanceSkeleton() {
return null;
}

export async function AccountBalanceWidget() {
const accountsData = await getTeamBankAccounts({ enabled: true });
const accountsData = await getBankAccountsBalances();

return (
<div className="h-full">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,39 @@

import { AddAccountButton } from "@/components/add-account-button";
import { FormatAmount } from "@/components/format-amount";
import { useI18n } from "@/locales/client";
import { formatAccountName } from "@/utils/format";
import { cn } from "@midday/ui/cn";
import Image from "next/image";
import { useState } from "react";

export function AccountBalance({ data }) {
type Props = {
data: {
id: string;
name: string;
balance: number;
currency: string;
logo_url?: string;
}[];
};

export function AccountBalance({ data }: Props) {
const [activeIndex, setActiveIndex] = useState(0);
const sortedAccounts = data?.sort((a, b) => b.balance - a.balance);
const t = useI18n();

const formattedData = data.map((account) => {
if (account.name === "total_balance") {
return {
...account,
id: account.name,
name: t("account_balance.total_balance"),
};
}

return account;
});

const activeAccount = sortedAccounts.at(activeIndex);
const activeAccount = formattedData.at(activeIndex);

if (!activeAccount) {
return (
Expand All @@ -37,10 +60,10 @@ export function AccountBalance({ data }) {
</h2>

<div className="flex space-x-2 items-center">
{activeAccount.bank?.logo_url && (
{activeAccount?.logo_url && (
<Image
src={activeAccount.bank.logo_url}
alt={activeAccount.bank.name}
src={activeAccount.logo_url}
alt=""
width={24}
height={24}
quality={100}
Expand All @@ -57,9 +80,9 @@ export function AccountBalance({ data }) {
</div>
</div>

{sortedAccounts.length > 1 && (
{formattedData.length > 1 && (
<div className="flex space-x-2">
{sortedAccounts.map((account, idx) => (
{formattedData.map((account, idx) => (
<button
type="button"
onMouseEnter={() => setActiveIndex(idx)}
Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,7 @@ export default {
"invoice_count#zero": "No invoices",
"invoice_count#one": "1 invoice",
"invoice_count#other": "{count} invoices",
account_balance: {
total_balance: "Total balance",
},
} as const;
3 changes: 3 additions & 0 deletions apps/dashboard/src/locales/sv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ export default {
in_progress: "Pågående",
completed: "Färdig",
},
account_balance: {
total_balance: "Total saldo",
},
} as const;
22 changes: 22 additions & 0 deletions packages/supabase/src/queries/cached-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type GetTrackerProjectsQueryParams,
type GetTrackerRecordsByRangeParams,
type GetTransactionsParams,
getBankAccountsBalancesQuery,
getBankAccountsCurrenciesQuery,
getBankConnectionsByTeamIdQuery,
getBurnRateQuery,
Expand Down Expand Up @@ -605,3 +606,24 @@ export const getTags = async () => {
},
)();
};

export const getBankAccountsBalances = async () => {
const supabase = createClient();
const user = await getUser();
const teamId = user?.data?.team_id;

if (!teamId) {
return null;
}

return unstable_cache(
async () => {
return getBankAccountsBalancesQuery(supabase, teamId);
},
["bank_accounts_balances", teamId],
{
tags: [`bank_accounts_balances_${teamId}`],
revalidate: 3600,
},
)();
};
9 changes: 9 additions & 0 deletions packages/supabase/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,12 @@ export async function getTagsQuery(supabase: Client, teamId: string) {
.eq("team_id", teamId)
.order("created_at", { ascending: false });
}

export async function getBankAccountsBalancesQuery(
supabase: Client,
teamId: string,
) {
return supabase.rpc("get_team_bank_accounts_balances", {
team_id: teamId,
});
}
25 changes: 25 additions & 0 deletions packages/supabase/src/types/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type Database = {
currency: string | null
enabled: boolean
error_details: string | null
error_retries: number | null
id: string
manual: boolean | null
name: string | null
Expand All @@ -83,6 +84,7 @@ export type Database = {
currency?: string | null
enabled?: boolean
error_details?: string | null
error_retries?: number | null
id?: string
manual?: boolean | null
name?: string | null
Expand All @@ -100,6 +102,7 @@ export type Database = {
currency?: string | null
enabled?: boolean
error_details?: string | null
error_retries?: number | null
id?: string
manual?: boolean | null
name?: string | null
Expand Down Expand Up @@ -1287,6 +1290,7 @@ export type Database = {
method: Database["public"]["Enums"]["transactionMethods"]
name: string
note: string | null
notified: boolean | null
recurring: boolean | null
status: Database["public"]["Enums"]["transactionStatus"] | null
team_id: string
Expand Down Expand Up @@ -1318,6 +1322,7 @@ export type Database = {
method: Database["public"]["Enums"]["transactionMethods"]
name: string
note?: string | null
notified?: boolean | null
recurring?: boolean | null
status?: Database["public"]["Enums"]["transactionStatus"] | null
team_id: string
Expand Down Expand Up @@ -1346,6 +1351,7 @@ export type Database = {
method?: Database["public"]["Enums"]["transactionMethods"]
name?: string
note?: string | null
notified?: boolean | null
recurring?: boolean | null
status?: Database["public"]["Enums"]["transactionStatus"] | null
team_id?: string
Expand Down Expand Up @@ -1586,6 +1592,12 @@ export type Database = {
}
Returns: number
}
calculate_total_sum2: {
Args: {
target_currency: string
}
Returns: number
}
calculate_transaction_differences_v2: {
Args: {
p_team_id: string
Expand Down Expand Up @@ -1748,6 +1760,7 @@ export type Database = {
method: Database["public"]["Enums"]["transactionMethods"]
name: string
note: string | null
notified: boolean | null
recurring: boolean | null
status: Database["public"]["Enums"]["transactionStatus"] | null
team_id: string
Expand Down Expand Up @@ -2069,6 +2082,18 @@ export type Database = {
percentage: number
}[]
}
get_team_bank_accounts_balances: {
Args: {
team_id: string
}
Returns: {
id: string
currency: string
balance: number
name: string
logo_url: string
}[]
}
get_total_balance: {
Args: {
team_id: string
Expand Down

0 comments on commit 383cd10

Please sign in to comment.