All files / src/endpoints config.ts

20% Statements 2/10
0% Branches 0/14
0% Functions 0/2
22.22% Lines 2/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                                                                                              2x   2x                                                                                                                                                                                  
import { readFileSync } from "fs"
import { join } from "path"
 
import {
  AccountRead,
  AccountsService,
  AccountTypeFilter,
  BudgetRead,
  BudgetsService,
  CategoriesService,
  CategoryRead,
} from "@billos/firefly-iii-sdk"
import { Request, Response } from "express"
import pino from "pino"
 
import { client } from "../client"
import DynamicConfig, { AConfig, VConfig } from "../modules/config/dynamic"
import { Notifiers } from "../modules/notifiers/types"
 
export interface About {
  name: string
  version: string
  description: string
  author: string
  license: string
  repository: string
}
 
export interface NotifierConfig {
  discordWebhook: string | null
  gotifyUrl: string | null
  gotifyToken: string | null
  gotifyUserToken: string | null
  gotifyApplicationId: string | null
}
 
export interface Config {
  about: About
  token?: string
  assetAccounts: AccountRead[]
  currentAccountId: string | null
  budgets?: BudgetRead[]
  categories?: CategoryRead[]
  hiddenBudgets?: string[]
  hiddenCategories?: string[]
  billsBudgetId: string | null
  leftoversBudgetId: string | null
  autoImportCron?: string
  budgetSumUpCron?: string
  notifier: Notifiers
  notifierConfig: NotifierConfig
  availableLocales: string[]
  userLocale?: string
}
 
const logger = pino()
 
const pkg = JSON.parse(readFileSync(join(process.cwd(), "package.json"), "utf-8")) as {
  name: string
  version: string
  description?: string
  author: string
  license: string
  repository?: string | { url: string }
}
 
function repositoryUrl(repo: typeof pkg.repository): string | undefined {
  if (!repo) return undefined
  const raw = typeof repo === "string" ? repo : repo.url
  return raw.replace(/^git\+/, "").replace(/\.git$/, "")
}
 
export async function configEndpoint(_req: Request, res: Response) {
  logger.debug("Config endpoint called")
  const [
    { data: budgets },
    { data: categories },
    { data: assetAccounts },
    hiddenBudgets,
    hiddenCategories,
    currentAccountId,
    billsBudgetId,
    leftoversBudgetId,
    autoImportCron,
    budgetSumUpCron,
    notifier,
    notifierDiscordWebhook,
    notifierGotifyUrl,
    notifierGotifyToken,
    notifierGotifyUserToken,
    notifierGotifyApplicationId,
    userLocale,
  ] = await Promise.all([
    BudgetsService.listBudget({ client, query: { page: 1, limit: 50 } }),
    CategoriesService.listCategory({ client, query: { page: 1, limit: 50 } }),
    AccountsService.listAccount({ client, query: { type: AccountTypeFilter.ASSET } }),
    DynamicConfig.lrange(AConfig.HiddenBudgets, 0, -1),
    DynamicConfig.lrange(AConfig.HiddenCategories, 0, -1),
    DynamicConfig.get(VConfig.CurrentAccountId),
    DynamicConfig.get(VConfig.RoleBudgetBillsId),
    DynamicConfig.get(VConfig.RoleBudgetLeftoversId),
    DynamicConfig.get(VConfig.AutoImportCron),
    DynamicConfig.get(VConfig.BudgetSumUpCron),
    DynamicConfig.get(VConfig.Notifier),
    DynamicConfig.get(VConfig.NotifierDiscordWebhook),
    DynamicConfig.get(VConfig.NotifierGotifyUrl),
    DynamicConfig.get(VConfig.NotifierGotifyToken),
    DynamicConfig.get(VConfig.NotifierGotifyUserToken),
    DynamicConfig.get(VConfig.NotifierGotifyApplicationId),
    DynamicConfig.get(VConfig.Locale),
  ])
 
  const result: Config = {
    about: {
      name: pkg.name,
      version: pkg.version,
      description: pkg.description || "",
      author: pkg.author,
      license: pkg.license,
      repository: repositoryUrl(pkg.repository) || "",
    },
    assetAccounts,
    currentAccountId,
    token: process.env.TOKEN,
    budgets,
    categories,
    hiddenBudgets,
    hiddenCategories,
    billsBudgetId,
    leftoversBudgetId,
    autoImportCron: autoImportCron ?? undefined,
    budgetSumUpCron: budgetSumUpCron ?? undefined,
    notifier: notifier as Notifiers,
    notifierConfig: {
      discordWebhook: notifierDiscordWebhook,
      gotifyUrl: notifierGotifyUrl,
      gotifyToken: notifierGotifyToken,
      gotifyUserToken: notifierGotifyUserToken,
      gotifyApplicationId: notifierGotifyApplicationId,
    },
    availableLocales: ["fr-FR", "en"],
    userLocale: userLocale ?? undefined,
  }
 
  return res.json(result).status(200)
}