All files / src/queues/jobs checkBudgetLimit.ts

5.88% Statements 3/51
0% Branches 0/30
0% Functions 0/3
5.88% Lines 3/51

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                      1x                       1x   1x                                                                                                                                                                              
import { BudgetLimitStoreWritable, BudgetRead, BudgetsService } from "@billos/firefly-iii-sdk"
import pino from "pino"
 
import { client } from "../../client"
import DynamicConfig, { VConfig } from "../../modules/config/dynamic"
import { getNotifier } from "../../modules/notifiers"
import { getEndOfCurrentMonth, getStartOfCurrentMonth } from "../../utils/date"
import { renderTemplate, TemplateName } from "../../utils/renderTemplate"
import { addBudgetJobToQueue } from "../utils"
import { BudgetJob } from "./BaseJob"
 
const logger = pino()
 
function getSpent(budget: BudgetRead): number {
  const { spent } = budget.attributes
  if (!spent || !spent.length) {
    return 0
  }
  const [sum] = spent
  return -parseFloat(sum.sum ?? "0")
}
 
export class CheckBudgetLimitJob extends BudgetJob {
  readonly id = "check-budget-limit"
 
  override readonly startDelay = 5
 
  async run(budgetId: string): Promise<void> {
    if (!budgetId) {
      logger.error("No budgetId provided for CheckBudgetLimit job")
      return
    }
    const start = getStartOfCurrentMonth()
    const end = getEndOfCurrentMonth()
 
    const { data: budget } = await BudgetsService.getBudget({ client, path: { id: budgetId }, query: { start, end } })
 
    if (!budget) {
      logger.error("Budget with id %s not found", budgetId)
      return
    }
 
    const [billsBudgetId, leftoversBudgetId] = await Promise.all([
      DynamicConfig.get(VConfig.RoleBudgetBillsId),
      DynamicConfig.get(VConfig.RoleBudgetLeftoversId),
    ])
 
    if (budget.id === billsBudgetId) {
      logger.debug("Budget is Bills budget, skipping review of budget limit")
      return
    }
    if (budget.id === leftoversBudgetId) {
      logger.debug("Budget is Leftovers budget, skipping review of budget limit")
 
      return
    }
 
    logger.info("Reviewing budget limit for %s with id %s", budget.attributes.name, budget.id)
 
    const {
      data: [existingLimits],
    } = await BudgetsService.listBudgetLimitByBudget({ client, path: { id: budget.id }, query: { start, end } })
 
    const currencySymbol = budget.attributes.currency_code === "EUR" ? "€" : "$"
    const spent = getSpent(budget)
    const limit = parseFloat(existingLimits?.attributes.amount ?? "0") || 0
 
    if (spent <= limit) {
      logger.info("Budget is within limit. Spent: %d, Limit: %d", spent, limit)
      return
    }
 
    logger.info("Budget is overspent! Spent: %d, Limit: %d", spent, limit)
    // Setting the limit to spent and sending a notification
    const body: BudgetLimitStoreWritable = { amount: spent.toString(), start, end, fire_webhooks: true }
 
    if (!existingLimits) {
      logger.info("No existing limits found, creating a new one")
      await BudgetsService.storeBudgetLimit({ client, path: { id: budget.id }, body })
    } else {
      await BudgetsService.updateBudgetLimit({ client, path: { id: budget.id, limitId: existingLimits.id }, body })
    }
 
    const { title, content } = await renderTemplate(TemplateName.BudgetOverspent, {
      budgetName: budget.attributes.name,
      spent,
      limit,
      currencySymbol,
    })
    const notifier = await getNotifier()
    if (notifier) {
      await notifier.sendMessage(title, content)
    }
  }
 
  override async init(): Promise<void> {
    logger.info("Initializing CheckBudgetLimit jobs for all budgets")
    const start = getStartOfCurrentMonth()
    const end = getEndOfCurrentMonth()
    const { data: budgets } = await BudgetsService.listBudget({ client, query: { start, end, limit: 100 } })
    const [billsBudgetId, leftoversBudgetId] = await Promise.all([
      DynamicConfig.get(VConfig.RoleBudgetBillsId),
      DynamicConfig.get(VConfig.RoleBudgetLeftoversId),
    ])
    for (const budget of budgets) {
      if (budget.id !== billsBudgetId && budget.id !== leftoversBudgetId) {
        await addBudgetJobToQueue(this, budget.id)
      }
    }
    logger.info("Initialized CheckBudgetLimit jobs for %d budgets", budgets.length)
  }
}