All files / src/queues/jobs removeTransactionMessages.ts

9.67% Statements 3/31
0% Branches 0/10
0% Functions 0/1
9.67% Lines 3/31

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                1x     1x   1x                                                                                                          
import { TransactionsService } from "@billos/firefly-iii-sdk"
import pino from "pino"
 
import { client } from "../../client"
import { getNotifier } from "../../modules/notifiers"
import { unbindTransactionToNotification } from "../../utils/notification"
import { TransactionJob } from "./BaseJob"
 
const logger = pino()
 
export class RemoveTransactionMessagesJob extends TransactionJob {
  readonly id = "remove-transaction-messages"
 
  override readonly startDelay = 15
 
  async run(id: string): Promise<void> {
    // Checking that the transaction exists in Firefly, otherwise the error will be handled by the retry mechanism of the job and the transaction will be checked again later, when it might have been deleted from Firefly
    try {
      await TransactionsService.getTransaction({ client, path: { id } })
    } catch (err) {
      logger.error({ err }, "Could not find transaction %s in Firefly, skipping message removal", id)
      return
    }
    logger.info("Transaction %s found, removing messages for updated transaction %s", id, id)
    const {
      data: {
        attributes: {
          transactions: [transaction],
        },
      },
    } = await TransactionsService.getTransaction({ client, path: { id } })
 
    const notifier = await getNotifier()
    if (!notifier) {
      logger.warn("No notifier configured, skipping message removal for transaction %s", id)
      return
    }
    if (transaction.category_id) {
      const categoryMessageId = await notifier.getMessageId("CategoryMessageId", id)
      if (categoryMessageId) {
        logger.info("Removing category message %s for transaction %s", categoryMessageId, id)
        try {
          await unbindTransactionToNotification(id, "CategoryMessageId", categoryMessageId)
        } catch (err) {
          logger.error({ err }, "Could not unset message ID for type CategoryMessageId and transaction %s:", id)
          return
        }
        await notifier.deleteMessage(categoryMessageId)
      }
    }
 
    if (transaction.budget_id) {
      const budgetMessageId = await notifier.getMessageId("BudgetMessageId", id)
      if (budgetMessageId) {
        logger.info("Removing budget message %s for transaction %s", budgetMessageId, id)
        try {
          await unbindTransactionToNotification(id, "BudgetMessageId", budgetMessageId)
        } catch (err) {
          logger.error({ err }, "Could not unset message ID for type BudgetMessageId and transaction %s:", id)
          return
        }
        await notifier.deleteMessage(budgetMessageId)
      }
    }
  }
}