All files / src/endpoints setBudgetRole.ts

21.05% Statements 4/19
25% Branches 2/8
33.33% Functions 1/3
21.05% Lines 4/19

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          2x   2x 2x 2x                                                            
import { Request, Response } from "express"
import pino from "pino"
 
import DynamicConfig, { VConfig } from "../modules/config/dynamic"
 
const logger = pino()
 
enum BudgetRole {
  Bills = "bills",
  Leftovers = "leftovers",
}
 
function isBudgetRole(value: string): value is BudgetRole {
  return Object.values(BudgetRole).includes(value as BudgetRole)
}
 
export async function setBudgetRole(req: Request<{ role: string; budgetId: string }>, res: Response) {
  const { role, budgetId } = req.params
  logger.info("=================================== Setting budget role ===================================")
 
  if (!isBudgetRole(role)) {
    logger.error("Invalid budget role %s", role)
    return res.status(400).send({ message: "Invalid budget role" })
  }
 
  const key = role === BudgetRole.Bills ? VConfig.RoleBudgetBillsId : VConfig.RoleBudgetLeftoversId
  const currentId = await DynamicConfig.get(key)
 
  // Clicking the budget already assigned to the role unassigns it (toggle behaviour)
  if (currentId === budgetId) {
    logger.info("Budget %s is already the %s budget, clearing it", budgetId, role)
    await DynamicConfig.delete(key)
    return res.status(202).send({ role, budgetId: "", selected: false })
  }
 
  logger.info("Setting budget %s as the %s budget", budgetId, role)
  await DynamicConfig.set(key, budgetId)
  return res.status(201).send({ role, budgetId, selected: true })
}