Skip to content

Use outgoingPaymentGrantSpentAmounts table during outgoing payment creation #3373

Open
@mkurapov

Description

@mkurapov

Currently, when an outgoing payment is being created under a particular grant, we need to make sure that the outgoing payment is within the particular limits of the grant (and the grant's interval).

Instead of doing what we are currently doing: grabbing all of the outgoing payments under that grant and calculating the limits in memory, we can leverage the outgoingPaymentGrantSpentAmounts table created in RAF-1032.

When an outgoing payment is being created we need to:

  1. Fetch the existing outgoingPaymentGrant, and at the same time, grab the most recent outgoingPaymentGrantSpentAmounts row:
    1. When fetching the outgoingPaymentGrants we need to use the FOR NO KEY UPDATE lock to prevent any concurrency issues
    2. To fetch the most recent outgoingPaymentGrantSpentAmounts row:
      1. SELECT * FROM “outgoingPaymentGrantSpentAmounts” WHERE grantId = {grantId} ORDER BY createdAt DESC LIMIT 1.

After we fetch both, in order to make this a backwards compatible change (since we will have existing grants that will not end up having any outgoingPaymentGrantSpentAmounts records yet), we will need to do the following (pseudocode):

  if (outgoingPaymentGrantRecord && !outgoingPaymentGrantSpentAmountsRecord) {
    // in this case, an existing grant is being requested. We haven't stored any records
    // for it in outgoingPaymentGrantSpentAmountsRecord, so we need to use the old calculation 
    // in `validateGrantAndAddSpentAmountsToPayment` to generate the first values we will insert
    // in the outgoingPaymentGrantSpentAmounts table:

    // 1. upsert outgoingPaymentGrant
    // 2. Determine new cumulative grant amounts using existing validateGrantAndAddSpentAmountsToPayment logic
    // 3. Fail if payment exceeds grant or interval limits
    // 4. insert new outgoingPaymentGrantSpentAmounts record

  } else {
    // 1. upsert outgoingPaymentGrant
    // 2. Determine new cumulative grant amounts (use existing outgoingPaymentGrantSpentAmountsRecord as starting point if present)
    // 3. Fail if payment exceeds grant or interval limits
    // 4. insert new outgoingPaymentGrantSpentAmounts record
  }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions