Skip to content

Commit

Permalink
One-for-All-174 I enabled to fetch the account data on the server side.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo-Kgym committed Jul 11, 2024
1 parent f198178 commit 7a101e3
Show file tree
Hide file tree
Showing 13 changed files with 808 additions and 842 deletions.
22 changes: 17 additions & 5 deletions apps/web/src/app/household/account/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/*
* Copyright (c) 2023 Ryo-Kgym.
*/

import { AccountListServer } from "@pageComponents/household/account/components/AccountListServer";

const Page = () => <AccountListServer />;
const Page = ({
searchParams,
}: {
searchParams: {
fromDate: string | undefined;
toDate: string | undefined;
accountId: string | undefined;
};
}) => (
<AccountListServer
fromDate={
searchParams.fromDate ? new Date(searchParams.fromDate) : undefined
}
toDate={searchParams.toDate ? new Date(searchParams.toDate) : undefined}
accountId={searchParams.accountId}
/>
);

export default Page;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { useState } from "react";
import { usePathname, useRouter } from "next/navigation";
import { AccountTableRow } from "@features/household/accountList/components/type";

import { Account } from "../../../../../../../packages/domain/household/account";
import { AccountPresenter } from "./AccountPresenter";

const FIRST_DATE = new Date("2019-01-01");
Expand All @@ -12,7 +12,7 @@ export const AccountContainer = ({
accountRecords,
total = 0,
}: {
accountRecords: AccountTableRow[];
accountRecords: Account[];
total: number | undefined;
}) => {
const [fromDate, setFromDate] = useState<Date | null>(FIRST_DATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Total } from "@components/molecules/Total";
import { RangeDatePicker } from "@components/ui/date";
import { DataTable } from "@components/ui/v4/table";

import { AccountTableRow } from "./type";
import { Account } from "../../../../../../../packages/domain/household/account";

export const AccountPresenter = ({
fromDate,
Expand All @@ -22,8 +22,8 @@ export const AccountPresenter = ({
toDate: Date | null;
changeToDate: (_: Date | null) => void;
total: number | undefined;
records: AccountTableRow[];
onRowClick: (record: AccountTableRow) => void;
records: Account[];
onRowClick: (record: Account) => void;
}) => (
<div>
<RangeDatePicker
Expand Down
33 changes: 0 additions & 33 deletions apps/web/src/hooks/household/account/useGetAccountBalanceList.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
import { ResponsiveSwitcher } from "@app/household/_layout/ResponsiveSwitcher";
import { DailyTableByAccount } from "@components/organisms/daily_table/account";
import { AccountContainer } from "@features/household/accountList/components/AccountContainer";
import { AccountTableRow } from "@features/household/accountList/components/type";
import { useGetAccountBalanceList } from "@hooks/household/account/useGetAccountBalanceList";

import { Account } from "../../../../../../../packages/domain/household/account";

export const AccountListClient = ({
accountRecords,
total,
fromDate,
toDate,
accountId,
}: {
accountRecords: Account[];
total: number | undefined;
fromDate: Date;
toDate: Date;
accountId: string | undefined;
}) => {
const { data, total } = useGetAccountBalanceList(fromDate, toDate);

const records: AccountTableRow[] =
data?.account.map((a) => ({
id: a.id,
accountName: a.accountName,
balance: a.allDetailViewsAggregate.aggregate?.sum?.signedAmount,
})) ?? [];

return (
<ResponsiveSwitcher
first={<AccountContainer accountRecords={records} total={total} />}
first={<AccountContainer accountRecords={accountRecords} total={total} />}
second={
<DailyTableByAccount
fromDate={fromDate}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { AccountListClient } from "@pageComponents/household/account/components/AccountListClient";
import { fetchBalanceList } from "../server/fetchBalanceList";
import { AccountListClient } from "./AccountListClient";

export const AccountListServer = async ({
fromDate = new Date("2019-01-01"),
toDate = new Date(),
accountId,
}: {
fromDate: Date | undefined;
toDate: Date | undefined;
accountId: string | undefined;
}) => {
const { records, total } = await fetchBalanceList({
fromDate,
toDate,
});

export const AccountListServer = () => {
return (
<AccountListClient
fromDate={new Date("2019-01-01")}
toDate={new Date()}
accountId={undefined}
accountRecords={records}
total={total}
fromDate={fromDate}
toDate={toDate}
accountId={accountId}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { findUser } from "@persistence/browser/server/find-user";
import { fetchQuery } from "@persistence/database/server/fetchQuery";
import {
convertToAccounts,
totalBalance,
} from "@v3/graphql/household/convert/parseToAccounts";
import { GetAccountBalanceListDocument } from "@v3/graphql/household/type";

export const fetchBalanceList = async ({
fromDate,
toDate,
}: {
fromDate: Date;
toDate: Date;
}) => {
const { group } = await findUser();

const { data } = await fetchQuery(GetAccountBalanceListDocument, {
fromDate: fromDate,
toDate: toDate,
groupId: group.id,
});

const records = convertToAccounts(data);
const total = totalBalance(data);

return {
records,
total,
};
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type AccountTableRow = {
export type Account = {
id: string;
} & Record<"accountName" | "balance", string | number>;
90 changes: 90 additions & 0 deletions packages/graphql/household/convert/parseToAccounts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { convertToAccounts, totalBalance } from "./parseToAccounts";

describe("convertToAccounts", () => {
it("should convert data to accounts", () => {
const data: Parameters<typeof convertToAccounts>[0] = {
account: [
{
__typename: "HouseholdAccount",
id: "1",
accountName: "account1",
allDetailViewsAggregate: {
aggregate: {
sum: {
__typename: "HouseholdAllDetailViewSumFields",
signedAmount: 100,
},
},
},
},
{
__typename: "HouseholdAccount",
id: "2",
accountName: "account2",
allDetailViewsAggregate: {
aggregate: {
sum: {
__typename: "HouseholdAllDetailViewSumFields",
signedAmount: 200,
},
},
},
},
],
};

const actual = convertToAccounts(data);

expect(actual).toEqual([
{
id: "1",
accountName: "account1",
balance: 100,
},
{
id: "2",
accountName: "account2",
balance: 200,
},
]);
});
});

describe("totalBalance", () => {
it("should calculate total balance", () => {
const data: Parameters<typeof convertToAccounts>[0] = {
account: [
{
__typename: "HouseholdAccount",
id: "1",
accountName: "account1",
allDetailViewsAggregate: {
aggregate: {
sum: {
__typename: "HouseholdAllDetailViewSumFields",
signedAmount: 100,
},
},
},
},
{
__typename: "HouseholdAccount",
id: "2",
accountName: "account2",
allDetailViewsAggregate: {
aggregate: {
sum: {
__typename: "HouseholdAllDetailViewSumFields",
signedAmount: 200,
},
},
},
},
],
};

const actual = totalBalance(data);

expect(actual).toBe(300);
});
});
21 changes: 21 additions & 0 deletions packages/graphql/household/convert/parseToAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Account } from "@oneforall/domain/household/account";

import { GetAccountBalanceListQuery } from "../type";

export const convertToAccounts = (
data: GetAccountBalanceListQuery,
): Account[] => {
return data.account.map((a) => ({
id: a.id,
accountName: a.accountName,
balance: a.allDetailViewsAggregate.aggregate?.sum?.signedAmount,
}));
};

export const totalBalance = (data: GetAccountBalanceListQuery): number => {
return data.account.reduce(
(acc, cur) =>
Number(cur.allDetailViewsAggregate.aggregate?.sum?.signedAmount) + acc,
0,
);
};
Loading

0 comments on commit 7a101e3

Please sign in to comment.