70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import type { InvestmentResourceMeta } from "@/shared/api";
|
|
|
|
import type {
|
|
BrokerageAccount,
|
|
} from "../api/useBrokerageAccounts";
|
|
|
|
type BrokerageAccountCard = {
|
|
account: BrokerageAccount;
|
|
positionCounts: null | PositionCounts;
|
|
source?: NonNullable<InvestmentResourceMeta["dependencies"]>[number];
|
|
summary:
|
|
| {
|
|
portfolio: NonNullable<BrokerageAccount["portfolioSummary"]>;
|
|
state: "available";
|
|
totals: NonNullable<BrokerageAccount["portfolioSummary"]>["totals"];
|
|
}
|
|
| { state: "empty" }
|
|
| { state: "unavailable" };
|
|
};
|
|
|
|
type PositionCounts = { bonds: number; stocks: number; total: number };
|
|
|
|
function accountTypeKey(type: BrokerageAccount["type"]) {
|
|
return `accounts.types.${type}` as const;
|
|
}
|
|
|
|
function brokerageAccountCards(
|
|
accounts: BrokerageAccount[],
|
|
dependencies: InvestmentResourceMeta["dependencies"] = [],
|
|
): BrokerageAccountCard[] {
|
|
return accounts.map((account) => {
|
|
const source = dependencies.find(
|
|
item =>
|
|
item.resourceKey === `portfolio/${encodeURIComponent(account.id)}`,
|
|
);
|
|
|
|
const summary = account.portfolioSummary;
|
|
if (!summary)
|
|
return {
|
|
account,
|
|
positionCounts:
|
|
null,
|
|
source,
|
|
summary: { state: "unavailable" },
|
|
};
|
|
|
|
const positionCounts = {
|
|
bonds: summary.bondPositionCount,
|
|
stocks: summary.stockPositionCount,
|
|
total: summary.totalPositionCount,
|
|
};
|
|
|
|
if (Number(summary.totals.value) === 0)
|
|
return { account, positionCounts, source, summary: { state: "empty" } };
|
|
|
|
return {
|
|
account,
|
|
positionCounts,
|
|
source,
|
|
summary: {
|
|
portfolio: summary,
|
|
state: "available",
|
|
totals: summary.totals,
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
export { accountTypeKey, type BrokerageAccountCard, brokerageAccountCards };
|