Skip to content

Commit

Permalink
fix: integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi committed Nov 29, 2023
1 parent 8e5d0ab commit 230ee45
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 21 deletions.
45 changes: 37 additions & 8 deletions core/api/src/app/payments/send-lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,14 @@ const executePaymentViaIntraledger = async <
}))
}

const senderWalletDescriptor = paymentFlow.senderWalletDescriptor()
const journal = await LedgerFacade.recordIntraledger({
description: paymentFlow.descriptionFromInvoice,
amount: {
btc: paymentFlow.btcPaymentAmount,
usd: paymentFlow.usdPaymentAmount,
},
senderWalletDescriptor: paymentFlow.senderWalletDescriptor(),
senderWalletDescriptor,
recipientWalletDescriptor,
metadata,
additionalDebitMetadata,
Expand All @@ -537,13 +538,13 @@ const executePaymentViaIntraledger = async <
const recipientUser = await UsersRepository().findById(recipientUserId)
if (recipientUser instanceof Error) return recipientUser

const walletTransaction = await getTransactionForWalletByJournalId({
const recipientWalletTransaction = await getTransactionForWalletByJournalId({
walletId: recipientWalletDescriptor.id,
journalId: journal.journalId,
})
if (walletTransaction instanceof Error) return walletTransaction
if (recipientWalletTransaction instanceof Error) return recipientWalletTransaction

const result = await NotificationsService().sendTransaction({
const recipientResult = await NotificationsService().sendTransaction({
recipient: {
accountId: recipientAccount.id,
walletId: recipientWalletDescriptor.id,
Expand All @@ -552,13 +553,41 @@ const executePaymentViaIntraledger = async <
notificationSettings: recipientAccount.notificationSettings,
level: recipientAccount.level,
},
transaction: walletTransaction,
transaction: recipientWalletTransaction,
})

if (result instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
if (recipientResult instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
await removeDeviceTokens({
userId: recipientUser.id,
deviceTokens: result.tokens,
deviceTokens: recipientResult.tokens,
})
}

const senderUser = await UsersRepository().findById(senderAccount.kratosUserId)
if (senderUser instanceof Error) return senderUser

const senderWalletTransaction = await getTransactionForWalletByJournalId({
walletId: senderWalletDescriptor.id,
journalId: journal.journalId,
})
if (senderWalletTransaction instanceof Error) return senderWalletTransaction

const senderResult = await NotificationsService().sendTransaction({
recipient: {
accountId: senderAccount.id,
walletId: senderWalletDescriptor.id,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
level: senderAccount.level,
},
transaction: senderWalletTransaction,
})

if (senderResult instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
await removeDeviceTokens({
userId: senderUser.id,
deviceTokens: senderResult.tokens,
})
}

Expand All @@ -577,7 +606,7 @@ const executePaymentViaIntraledger = async <

return {
status: PaymentSendStatus.Success,
transaction: walletTransaction,
transaction: senderWalletTransaction,
}
})
}
Expand Down
48 changes: 40 additions & 8 deletions core/api/src/app/payments/send-on-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const payOnChainByWalletId = async ({

return executePaymentViaIntraledger({
builder,
senderAccount,
senderWallet,
senderUsername: senderAccount.username,
senderDisplayCurrency: senderAccount.displayCurrency,
Expand Down Expand Up @@ -249,13 +250,15 @@ const executePaymentViaIntraledger = async <
R extends WalletCurrency,
>({
builder,
senderAccount,
senderWallet,
senderUsername,
senderDisplayCurrency,
memo,
sendAll,
}: {
builder: OPFBWithConversion<S, R> | OPFBWithError
senderAccount: Account
senderWallet: WalletDescriptor<S>
senderUsername: Username | undefined
senderDisplayCurrency: DisplayCurrency
Expand Down Expand Up @@ -404,14 +407,15 @@ const executePaymentViaIntraledger = async <
}))
}

const senderWalletDescriptor = paymentFlow.senderWalletDescriptor()
// Record transaction
const journal = await LedgerFacade.recordIntraledger({
description: "",
amount: {
btc: paymentFlow.btcPaymentAmount,
usd: paymentFlow.usdPaymentAmount,
},
senderWalletDescriptor: paymentFlow.senderWalletDescriptor(),
senderWalletDescriptor,
recipientWalletDescriptor,
metadata,
additionalDebitMetadata,
Expand All @@ -423,14 +427,14 @@ const executePaymentViaIntraledger = async <
const recipientUser = await UsersRepository().findById(recipientUserId)
if (recipientUser instanceof Error) return recipientUser

const walletTransaction = await getTransactionForWalletByJournalId({
const recipientWalletTransaction = await getTransactionForWalletByJournalId({
walletId: recipientWallet.id,
journalId: journal.journalId,
})
if (walletTransaction instanceof Error) return walletTransaction
if (recipientWalletTransaction instanceof Error) return recipientWalletTransaction

// Send 'received'-side intraledger notification
const result = await NotificationsService().sendTransaction({
const recipientResult = await NotificationsService().sendTransaction({
recipient: {
accountId: recipientWallet.accountId,
walletId: recipientWallet.id,
Expand All @@ -439,17 +443,45 @@ const executePaymentViaIntraledger = async <
notificationSettings: recipientAccount.notificationSettings,
level: recipientAccount.level,
},
transaction: walletTransaction,
transaction: recipientWalletTransaction,
})

if (result instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
if (recipientResult instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
await removeDeviceTokens({
userId: recipientUser.id,
deviceTokens: result.tokens,
deviceTokens: recipientResult.tokens,
})
}

return { status: PaymentSendStatus.Success, transaction: walletTransaction }
const senderUser = await UsersRepository().findById(senderAccount.kratosUserId)
if (senderUser instanceof Error) return senderUser

const senderWalletTransaction = await getTransactionForWalletByJournalId({
walletId: senderWalletDescriptor.id,
journalId: journal.journalId,
})
if (senderWalletTransaction instanceof Error) return senderWalletTransaction

const senderResult = await NotificationsService().sendTransaction({
recipient: {
accountId: senderAccount.id,
walletId: senderWalletDescriptor.id,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
level: senderAccount.level,
},
transaction: senderWalletTransaction,
})

if (senderResult instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
await removeDeviceTokens({
userId: senderUser.id,
deviceTokens: senderResult.tokens,
})
}

return { status: PaymentSendStatus.Success, transaction: senderWalletTransaction }
})
}

Expand Down
16 changes: 15 additions & 1 deletion core/api/test/integration/app/wallets/send-intraledger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,21 @@ describe("intraLedgerPay", () => {
senderWalletId: newWalletDescriptor.id,
senderAccount: newAccount,
})
expect(paymentResult).toEqual(PaymentSendStatus.Success)
expect(paymentResult).toEqual({
status: PaymentSendStatus.Success,
transaction: expect.objectContaining({
walletId: newWalletDescriptor.id,
status: "success",
settlementAmount: amount * -1,
settlementCurrency: "BTC",
initiationVia: expect.objectContaining({
type: "intraledger",
}),
settlementVia: expect.objectContaining({
type: "intraledger",
}),
}),
})

// Expect sent notification
expect(sendFilteredNotification.mock.calls.length).toBe(1)
Expand Down
37 changes: 35 additions & 2 deletions core/api/test/integration/app/wallets/send-lightning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,24 @@ describe("initiated via lightning", () => {
senderAccount: newAccount,
amount,
})
expect(paymentResult).toEqual(PaymentSendStatus.Success)
if (paymentResult instanceof Error) throw paymentResult
expect(paymentResult).toEqual({
status: PaymentSendStatus.Success,
transaction: expect.objectContaining({
walletId: newWalletDescriptor.id,
status: "success",
settlementAmount: (amount + paymentResult.transaction.settlementFee) * -1,
settlementCurrency: "BTC",
initiationVia: expect.objectContaining({
type: "lightning",
paymentHash: noAmountLnInvoice.paymentHash,
pubkey: DEFAULT_PUBKEY,
}),
settlementVia: expect.objectContaining({
type: "lightning",
}),
}),
})

// Check lnPayment collection after
const lnPaymentAfter = await LnPaymentsRepository().findByPaymentHash(
Expand Down Expand Up @@ -817,7 +834,23 @@ describe("initiated via lightning", () => {
senderAccount: newAccount,
amount,
})
expect(paymentResult).toEqual(PaymentSendStatus.Success)
expect(paymentResult).toEqual({
status: PaymentSendStatus.Success,
transaction: expect.objectContaining({
walletId: newWalletDescriptor.id,
status: "success",
settlementAmount: amount * -1,
settlementCurrency: "BTC",
initiationVia: expect.objectContaining({
type: "lightning",
paymentHash: noAmountLnInvoice.paymentHash,
pubkey: noAmountLnInvoice.destination,
}),
settlementVia: expect.objectContaining({
type: "intraledger",
}),
}),
})

// Expect sent notification
expect(sendFilteredNotification.mock.calls.length).toBe(1)
Expand Down
18 changes: 16 additions & 2 deletions core/api/test/integration/app/wallets/send-onchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,26 @@ describe("onChainPay", () => {
senderAccount: newAccount,
amount,
address: recipientWalletIdAddress,

speed: PayoutSpeed.Fast,
memo,
})
if (paymentResult instanceof Error) throw paymentResult
expect(paymentResult.status).toEqual(PaymentSendStatus.Success)
expect(paymentResult).toEqual({
status: PaymentSendStatus.Success,
transaction: expect.objectContaining({
walletId: newWalletDescriptor.id,
status: "success",
settlementAmount: amount * -1,
settlementCurrency: "BTC",
initiationVia: expect.objectContaining({
type: "onchain",
address: recipientWalletIdAddress,
}),
settlementVia: expect.objectContaining({
type: "intraledger",
}),
}),
})

// Expect sent notification
expect(sendFilteredNotification.mock.calls.length).toBe(1)
Expand Down

0 comments on commit 230ee45

Please sign in to comment.