All files / src/queues/jobs checkBudgetLimit.ts

6.81% Statements 3/44
0% Branches 0/22
0% Functions 0/2
6.81% Lines 3/44

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                      1x     1x   1x                                                                                                                                                                      
import { BudgetLimitStoreWritable, BudgetsService } from "@billos/firefly-iii-sdk"
import pino from "pino"
 
import { client } from "../../client"
import { env } from "../../config"
import { notifier } from "../../modules/notifiers"
import { getDateNow } from "../../utils/date"
import { renderTemplate } from "../../utils/renderTemplate"
import { addBudgetJobToQueue } from "../utils"
import { BudgetJob } from "./BaseJob"
 
const logger = pino()
 
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 {
      data: { data: budget },
    } = await BudgetsService.getBudget({ client, path: { id: budgetId } })
 
    if (!budget) {
      logger.error("Budget with id %s not found", budgetId)
      return
    }
 
    if (budget.id === env.billsBudgetId) {
      logger.debug("Budget is Bills budget, skipping review of budget limit")
      return
    }
    if (budget.id === env.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 start = getDateNow().startOf("month").toISODate()
    const end = getDateNow().endOf("month").toISODate()
    const {
      data: {
        data: [existingLimits],
      },
    } = await BudgetsService.listBudgetLimitByBudget({ client, path: { id: budget.id }, query: { start, end } })
 
    const currencySymbol = budget.attributes.currency_code === "EUR" ? "€" : "$"
    const spent = -parseFloat(existingLimits?.attributes.spent[0]?.sum || "0")
    const limit = parseFloat(existingLimits?.attributes.amount) || 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 = "Warning"
    const message = renderTemplate("budget-overspent.njk", {
      budgetName: budget.attributes.name,
      spent,
      limit,
      currencySymbol,
    })
    await notifier.sendMessage(title, message)
    return
  }
 
  override async init(): Promise<void> {
    logger.info("Initializing CheckBudgetLimit jobs for all budgets")
    const start = getDateNow().startOf("month").toISODate()
    const end = getDateNow().endOf("month").toISODate()
    const {
      data: { data: budgets },
    } = await BudgetsService.listBudget({ client, query: { start, end, limit: 100 } })
    for (const budget of budgets) {
      if (budget.id !== env.billsBudgetId && budget.id !== env.leftoversBudgetId) {
        await addBudgetJobToQueue(this, budget.id)
      }
    }
    logger.info("Initialized CheckBudgetLimit jobs for %d budgets", budgets.length)
  }
}