All files / src/queues/jobs removeTransactionMessages.ts

13.04% Statements 3/23
0% Branches 0/8
0% Functions 0/1
13.04% Lines 3/23

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                1x     1x   1x                                                                                      
import { TransactionsService } from "@billos/firefly-iii-sdk"
import pino from "pino"
 
import { client } from "../../client"
import { notifier } 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> {
    logger.info("Checking and removing messages for updated transaction %s", id)
    const {
      data: {
        data: {
          attributes: {
            transactions: [transaction],
          },
        },
      },
    } = await TransactionsService.getTransaction({ client, path: { id } })
 
    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)
      }
    }
  }
}