-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
One-for-All-174 I enabled to fetch the account data on the server side.
- Loading branch information
Showing
13 changed files
with
808 additions
and
842 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
apps/web/src/hooks/household/account/useGetAccountBalanceList.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 21 additions & 5 deletions
26
apps/web/src/pageComponents/household/account/components/AccountListServer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
apps/web/src/pageComponents/household/account/server/fetchBalanceList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
2 changes: 1 addition & 1 deletion
2
.../household/accountList/components/type.ts → packages/domain/household/account.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
packages/graphql/household/convert/parseToAccounts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}; |
Oops, something went wrong.