Frontend verify / commits (pull_request) Successful in 29s
Frontend verify / security (pull_request) Successful in 8s
Frontend verify / verify (pull_request) Successful in 5m25s
Frontend verify / commits (push) Skipped
Frontend verify / security (push) Successful in 9s
Frontend verify / verify (push) Failing after 5m20s
81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
import type { InvestmentResourceMeta } from "@/shared/api";
|
|
|
|
import type {
|
|
BrokerageAccount,
|
|
} from "../api/useBrokerageAccounts";
|
|
|
|
const VISIBLE_CURRENCY_LIMIT = 2;
|
|
|
|
type BrokerageAccountCard = {
|
|
account: BrokerageAccount;
|
|
positionCounts: null | PositionCounts;
|
|
source?: NonNullable<InvestmentResourceMeta["dependencies"]>[number];
|
|
summary:
|
|
| {
|
|
portfolio: NonNullable<BrokerageAccount["portfolioSummary"]>;
|
|
remainingCurrencyCount: number;
|
|
state: "available";
|
|
totals: Money[];
|
|
}
|
|
| { state: "empty" }
|
|
| { state: "unavailable" };
|
|
};
|
|
|
|
type Money = NonNullable<
|
|
BrokerageAccount["portfolioSummary"]
|
|
>["totalsByCurrency"][number];
|
|
|
|
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 (summary.totalsByCurrency.length === 0)
|
|
return { account, positionCounts, source, summary: { state: "empty" } };
|
|
|
|
return {
|
|
account,
|
|
positionCounts,
|
|
source,
|
|
summary: {
|
|
portfolio: summary,
|
|
remainingCurrencyCount: Math.max(
|
|
summary.totalsByCurrency.length - VISIBLE_CURRENCY_LIMIT,
|
|
0,
|
|
),
|
|
state: "available",
|
|
totals: summary.totalsByCurrency.slice(0, VISIBLE_CURRENCY_LIMIT),
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
export { accountTypeKey, type BrokerageAccountCard, brokerageAccountCards };
|