-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathwebhooks.ts
More file actions
65 lines (60 loc) · 2.28 KB
/
webhooks.ts
File metadata and controls
65 lines (60 loc) · 2.28 KB
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
import type { ActionFunctionArgs } from '@remix-run/node'
import { json } from '@remix-run/node'
import {
handleLowLiquidity,
handleWalletAddressNotFound,
handleWalletAddressWebMonetization,
handleOutgoingPaymentCreated,
handleOutgoingPaymentCompletedFailed,
handleIncomingPaymentCompletedExpired,
handleIncomingPartialPaymentReceived
} from '~/lib/webhooks.server'
import { WebhookEventType, Webhook } from 'mock-account-service-lib'
import { getTenantCredentials } from '~/lib/utils'
import { messageStorage } from '~/lib/message.server'
export function parseError(e: unknown): string {
return e instanceof Error && e.stack ? e.stack : String(e)
}
export async function action({ request }: ActionFunctionArgs) {
const wh: Webhook = await request.json()
console.log('received webhook: ', JSON.stringify(wh))
const session = await messageStorage.getSession()
const tenantOptions = await getTenantCredentials(session)
try {
switch (wh.type) {
case WebhookEventType.OutgoingPaymentCreated:
await handleOutgoingPaymentCreated(wh, tenantOptions)
break
case WebhookEventType.OutgoingPaymentCompleted:
case WebhookEventType.OutgoingPaymentFailed:
await handleOutgoingPaymentCompletedFailed(wh)
break
case WebhookEventType.IncomingPaymentCreated:
break
case WebhookEventType.IncomingPaymentPartialPaymentReceived:
await handleIncomingPartialPaymentReceived(wh, tenantOptions)
break
case WebhookEventType.IncomingPaymentCompleted:
case WebhookEventType.IncomingPaymentExpired:
await handleIncomingPaymentCompletedExpired(wh, tenantOptions)
break
case WebhookEventType.WalletAddressWebMonetization:
await handleWalletAddressWebMonetization(wh, tenantOptions)
break
case WebhookEventType.WalletAddressNotFound:
await handleWalletAddressNotFound(wh)
break
case WebhookEventType.AssetLiquidityLow:
case WebhookEventType.PeerLiquidityLow:
await handleLowLiquidity(wh)
break
default:
console.log(`unknown event type: ${wh.type}`)
return json(undefined, { status: 400 })
}
} catch (e) {
const errorInfo = parseError(e)
console.log(errorInfo)
}
return json(undefined, { status: 200 })
}