All files / src/modules/notifiers notifier.ts

0% Statements 0/8
0% Branches 0/4
0% Functions 0/2
0% Lines 0/8

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                                                                                                 
import { TransactionSplit, TransactionsService } from "@billos/firefly-iii-sdk"
 
import { client } from "../../client"
 
export type MessageType = "BudgetMessageId" | "CategoryMessageId" | "AutoImportMessage"
 
export interface Notifier {
  // Function about transactions
  getMessageId: (type: MessageType, transactionId: string) => Promise<string>
  // Generic function about messages
  sendMessage: (title: string, content: string) => Promise<string>
  deleteMessage: (id: string) => Promise<void>
  deleteAllMessages: () => Promise<void>
  hasMessageId: (messageId: string) => Promise<boolean>
}
 
export abstract class AbstractNotifier implements Notifier {
  private async getTransaction(id: string): Promise<TransactionSplit> {
    const {
      data: {
        data: {
          attributes: {
            transactions: [transaction],
          },
        },
      },
    } = await TransactionsService.getTransaction({ client, path: { id } })
    return transaction
  }
 
  public async getMessageId(type: MessageType, transactionId: string): Promise<string> {
    const transaction = await this.getTransaction(transactionId)
    const regex = new RegExp(`${type}: (\\d+)`)
    const match = (transaction.notes || "").match(regex)
    if (match) {
      return match[1]
    }
    return null
  }
 
  abstract sendMessage(title: string, content: string): Promise<string>
 
  abstract deleteMessage(id: string): Promise<void>
 
  abstract hasMessageId(messageId: string): Promise<boolean>
 
  abstract deleteAllMessages(): Promise<void>
}