All files / src/endpoints categoriesForTransaction.ts

0% Statements 0/9
100% Branches 0/0
0% Functions 0/2
0% Lines 0/8

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                                                                                         
import { CategoriesService, TransactionsService } from "@billos/firefly-iii-sdk"
import { Request, Response } from "express"
import pino from "pino"
 
import { client } from "../client"
import { env } from "../config"
import { getBudgetName } from "../utils/budgetName"
import { getTransactionShowLink } from "../utils/getTransactionShowLink"
 
const logger = pino()
 
export async function categoriesForTransaction(req: Request<{ transactionId: string }>, res: Response) {
  logger.info("=================================== Showing categories for transaction ===================================")
  const { transactionId } = req.params
 
  // Get all categories
  const {
    data: { data: allCategories },
  } = await CategoriesService.listCategory({ client, query: { page: 1, limit: 50 } })
 
  // Filter out hidden categories
  const billsBudgetName = await getBudgetName(env.billsBudgetId)
  const categories = allCategories.filter(({ attributes: { name } }) => name !== billsBudgetName)
 
  const {
    data: {
      data: {
        attributes: {
          transactions: [{ description, amount, currency_symbol: currency }],
        },
      },
    },
  } = await TransactionsService.getTransaction({ client, path: { id: transactionId } })
 
  res.render("set-category", {
    categories,
    transactionId,
    description,
    amount: Number(amount).toFixed(2),
    currency,
    transactionLink: getTransactionShowLink(transactionId),
    token: req.query.api_token,
  })
}