summaryrefslogtreecommitdiff
path: root/gui/src/types/balanceMap.ts
blob: 760843f80ea5e50ad19cf185e0c42975e2ad6bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { Category } from "./category";
import Currency from "./currency/currency";
import { newGbpFromDto } from "./currency/gbp";

type BalanceMap<T extends { id: any }> = {
  [id in T["id"]]: Currency;
};

function newCategoryBalanceMapFromDto(dto: unknown): BalanceMap<Category> {
  return Object.fromEntries(
    (dto as { category_id: Category["id"]; balance: number }[]).map(
      ({ category_id, balance }) => [category_id, newGbpFromDto(balance)],
    ),
  );
}

export { type BalanceMap, newCategoryBalanceMapFromDto };