All files / src/endpoints createNewCategory.ts

0% Statements 0/13
0% Branches 0/2
0% Functions 0/2
0% Lines 0/12

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                                                           
import { CategoriesService } from "@billos/firefly-iii-sdk"
import { NextFunction, Request, Response } from "express"
import pino from "pino"
 
import { client } from "../client"
 
const logger = pino()
 
export async function createNewCategory(
  req: Request<{ category_id?: string }, {}, {}, { name: string }>,
  _res: Response,
  next: NextFunction,
) {
  logger.info("=================================== Creating new category ===================================")
  const { name } = req.query
 
  logger.info("Creating new category with name: %s", name)
  const { data: categories } = await CategoriesService.listCategory({ client, query: { page: 1, limit: 50 } })
  let category = categories.data.find(({ attributes }) => attributes.name === name)
  if (!category) {
    category = (await CategoriesService.storeCategory({ client, body: { name } })).data.data
    logger.info("Category with name %s created successfully", name)
  } else {
    logger.info("Category with name %s already exists, skipping creation", name)
  }
 
  req.params.category_id = category.id
  next()
}