Skip to content

Commit

Permalink
feat(incoming-payment): new error code + linting
Browse files Browse the repository at this point in the history
  • Loading branch information
golobitch committed Aug 10, 2024
1 parent c2ab248 commit 70e789a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
17 changes: 13 additions & 4 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,19 @@ export const Config = {

incomingPaymentWorkers: envInt('INCOMING_PAYMENT_WORKERS', 1),
incomingPaymentWorkerIdle: envInt('INCOMING_PAYMENT_WORKER_IDLE', 200), // milliseconds
pollIncomingPaymentCreatedWebhook: envBool('POLL_INCOMING_PAYMENT_CREATED_WEBHOOK', false),
incomingPaymentCreatedPollTimeout: envInt('INCOMING_PAYMENT_CREATED_POLL_TIMEOUT_MS', 10000), // milliseconds
incomingPaymentCreatedPollFrequency: envInt('INCOMING_PAYMENT_CREATED_POLL_FREQUENCY_MS', 1000), // milliseconds

pollIncomingPaymentCreatedWebhook: envBool(
'POLL_INCOMING_PAYMENT_CREATED_WEBHOOK',
false
),
incomingPaymentCreatedPollTimeout: envInt(
'INCOMING_PAYMENT_CREATED_POLL_TIMEOUT_MS',
10000
), // milliseconds
incomingPaymentCreatedPollFrequency: envInt(
'INCOMING_PAYMENT_CREATED_POLL_FREQUENCY_MS',
1000
), // milliseconds

webhookWorkers: envInt('WEBHOOK_WORKERS', 1),
webhookWorkerIdle: envInt('WEBHOOK_WORKER_IDLE', 200), // milliseconds
webhookUrl: envString('WEBHOOK_URL'),
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/open_payments/payment/incoming/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const errorToHTTPCode: {
[IncomingPaymentError.InvalidExpiry]: 400,
[IncomingPaymentError.WrongState]: 409,
[IncomingPaymentError.InactiveWalletAddress]: 400,
[IncomingPaymentError.ActionNotPerformed]: 400
[IncomingPaymentError.ActionNotPerformed]: 403
}

export const errorToCode: {
Expand All @@ -38,7 +38,7 @@ export const errorToCode: {
[IncomingPaymentError.InvalidExpiry]: GraphQLErrorCode.BadUserInput,
[IncomingPaymentError.WrongState]: GraphQLErrorCode.Conflict,
[IncomingPaymentError.InactiveWalletAddress]: GraphQLErrorCode.Inactive,
[IncomingPaymentError.ActionNotPerformed]: GraphQLErrorCode.Timeout,
[IncomingPaymentError.ActionNotPerformed]: GraphQLErrorCode.Timeout
}

export const errorToMessage: {
Expand Down
25 changes: 13 additions & 12 deletions packages/backend/src/open_payments/payment/incoming/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async function createIncomingPayment(
return IncomingPaymentError.InvalidAmount
}
}

let incomingPayment = await IncomingPayment.query(trx || deps.knex)
.insertAndFetch({
walletAddressId: walletAddressId,
Expand All @@ -148,27 +148,28 @@ async function createIncomingPayment(

try {
const response = await poll({
request: async () => getApprovedOrCanceledIncomingPayment(deps, { id: incomingPayment.id }),
request: async () =>
getApprovedOrCanceledIncomingPayment(deps, { id: incomingPayment.id }),
pollingFrequencyMs: deps.config.incomingPaymentCreatedPollFrequency,
timeoutMs: deps.config.incomingPaymentCreatedPollTimeout
})

if (response) return response
return IncomingPaymentError.ActionNotPerformed
}catch(err) {
} catch (err) {
return IncomingPaymentError.ActionNotPerformed
}

}

async function getApprovedOrCanceledIncomingPayment(deps: ServiceDependencies, options: GetOptions) {
const incomingPayment = await getIncomingPayment(deps, { id: options.id })

if (incomingPayment?.approvedAt || incomingPayment?.cancelledAt) {
return incomingPayment
}

return undefined
async function getApprovedOrCanceledIncomingPayment(
deps: ServiceDependencies,
options: GetOptions
) {
return IncomingPayment.query(deps.knex)
.get(options)
.withGraphFetched('[asset, walletAddress]')
.whereNotNull('approvedAt')
.orWhereNotNull('cancelledAt')
}

// Fetch (and lock) an incoming payment for work.
Expand Down

0 comments on commit 70e789a

Please sign in to comment.