All files / src/queues/jobs setBudgetForTransaction.ts

93.75% Statements 15/16
50% Branches 1/2
100% Functions 1/1
93.75% Lines 15/16

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                4x             4x     3x 3x   3x 3x 3x 3x 3x 2x 2x   1x           3x   3x         3x      
import { TransactionsService, TransactionUpdateWritable } from "@billos/firefly-iii-sdk"
import pino from "pino"
 
import { client } from "../../client"
import { getNotifier } from "../../modules/notifiers"
import { unbindTransactionToNotification } from "../../utils/notification"
import { EndpointJob } from "./BaseJob"
 
const logger = pino()
 
interface JobData {
  budget_id: string
}
 
export class SetBudgetForTransactionJob extends EndpointJob {
  readonly id = "set-budget-for-transaction"
 
  async run(id: string, data: unknown): Promise<void> {
    const { budget_id } = data as JobData
    logger.info("Setting budget %s for transaction %s", budget_id, id)
 
    const notifier = await getNotifier()
    if (notifier) {
      try {
        logger.info("Deleting notifier message")
        const messageId = await notifier.getMessageId("BudgetMessageId", id)
        await unbindTransactionToNotification(id, "BudgetMessageId", messageId)
        await notifier.deleteMessage(messageId)
      } catch {
        logger.error("No notifier message to delete for transaction %s", id)
      }
    } else E{
      logger.warn("No notifier configured, skipping message removal for transaction %s", id)
    }
 
    logger.info("Update transaction")
 
    const body: TransactionUpdateWritable = {
      apply_rules: true,
      fire_webhooks: true,
      transactions: [{ budget_id }],
    }
    await TransactionsService.updateTransaction({ client, path: { id }, body })
  }
}