Skip to content

Commit

Permalink
feat: complete PayoutCancelled handler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Oct 24, 2023
1 parent 6e8202a commit e41e4d5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
27 changes: 17 additions & 10 deletions core/api/src/servers/event-handlers/bria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CouldNotFindWalletFromOnChainAddressError,
LessThanDustThresholdError,
NoTransactionToUpdateError,
NotImplementedError,
} from "@/domain/errors"

import { NoTransactionToSettleError } from "@/services/ledger/domain/errors"
Expand Down Expand Up @@ -58,7 +57,13 @@ export const briaEventHandler = async (event: BriaEvent): Promise<true | DomainE
return true

case BriaPayloadType.PayoutCancelled:
return payoutCancelledEventHandler({ event: event.payload })
if (event.augmentation.payoutInfo === undefined) {
return new EventAugmentationMissingError()
}
return payoutCancelledEventHandler({
event: event.payload,
payoutInfo: event.augmentation.payoutInfo,
})

case BriaPayloadType.PayoutBroadcast:
if (event.augmentation.payoutInfo === undefined) {
Expand Down Expand Up @@ -148,18 +153,20 @@ export const payoutSubmittedEventHandler = async ({

export const payoutCancelledEventHandler = async ({
event,
payoutInfo,
}: {
event: PayoutCancelled
payoutInfo: PayoutAugmentation
}): Promise<true | ApplicationError> => {
const txns = await LedgerFacade.getTransactionsByPayoutId(event.id)
if (txns instanceof Error) return txns

if (txns.length !== 0)
return new NotImplementedError(
`Payout cancels not implemented as yet for PayoutId: ${event.id}`,
)
const res = await LedgerFacade.recordOnChainSendRevert({
journalId: payoutInfo.externalId as LedgerJournalId,
payoutId: event.id,
})
if (res instanceof NoTransactionToUpdateError) {
return true
}

return true
return res
}

export const payoutBroadcastEventHandler = async ({
Expand Down
24 changes: 24 additions & 0 deletions core/api/src/services/ledger/facade/onchain-send.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { JournalNotFoundError } from "medici"

import { MainBook, Transaction } from "../books"

import { getBankOwnerWalletId, getNonEndUserWalletIds } from "../caching"
Expand Down Expand Up @@ -100,6 +102,28 @@ export const setOnChainTxPayoutId = async ({
}
}

export const recordOnChainSendRevert = async ({
journalId,
payoutId,
}: SetOnChainTxPayoutIdArgs): Promise<true | LedgerServiceError> => {
const reason = "Payment canceled"
try {
if (!isValidObjectId(journalId)) {
return new NoTransactionToUpdateError(JSON.stringify({ journalId }))
}

await MainBook.void(journalId, reason)

return true
} catch (err) {
if (err instanceof JournalNotFoundError) {
return new NoTransactionToUpdateError(JSON.stringify({ journalId, payoutId }))
}

return new UnknownLedgerError(err)
}
}

export const setOnChainTxIdByPayoutId = async ({
payoutId,
txId,
Expand Down

0 comments on commit e41e4d5

Please sign in to comment.