Skip to content

Commit

Permalink
update customer plan
Browse files Browse the repository at this point in the history
  • Loading branch information
nnn-gif committed Aug 27, 2024
1 parent c32c3e0 commit 72c2072
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
40 changes: 39 additions & 1 deletion pkg/model/looppayment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import "context"
import (
"context"
"encoding/json"
)

// {
// "event": "TransferProcessed",
Expand Down Expand Up @@ -227,3 +230,38 @@ func (reldb *RelDB) InsertLoopPaymentResponse(ctx context.Context, response Loop
)
return err
}

func (reldb *RelDB) GetLoopPaymentResponseByAgreementID(ctx context.Context, agreementID string) (*LoopPaymentResponse, error) {
query := `
SELECT event, transaction, network_id, network_name, contract_address, email, company,
parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number,
frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount,
payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata
FROM loop_payment_responses
WHERE agreement_id = $1
`

row := reldb.postgresClient.QueryRow(ctx, query, agreementID)

var response LoopPaymentResponse
var metadataJSON []byte

err := row.Scan(
&response.Event, &response.Transaction, &response.NetworkID, &response.NetworkName,
&response.ContractAddress, &response.Email, &response.Company, &response.Parent,
&response.Subscriber, &response.Item, &response.ItemID, &response.AgreementID,
&response.AgreementAmount, &response.FrequencyNumber, &response.FrequencyUnit,
&response.AddOnAgreements, &response.AddOnItems, &response.AddOnItemIds,
&response.AddOnTotalAmount, &response.PaymentTokenSymbol, &response.PaymentTokenAddress,
&response.EventDate, &response.RefID, &response.InvoiceID, &metadataJSON,
)
if err != nil {
return nil, err
}

if err := json.Unmarshal(metadataJSON, &response.Metadata); err != nil {
return nil, err
}

return &response, nil
}
16 changes: 16 additions & 0 deletions pkg/model/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,22 @@ func (reldb *RelDB) CreateCustomer(email string, customerPlan int, paymentStatus
return nil
}

func (reldb *RelDB) UpdateCustomerPlan(ctx context.Context, customerID int, customerPlan int, paymentSource string, lastPayment string) error {

ut, err := strconv.ParseInt(lastPayment, 10, 64)
lastPaymentts := time.Unix(ut, 0)

query := `
UPDATE customers
SET customer_plan = $1,
payment_source = $2,
last_payment = $3
WHERE customer_id = $4
`
_, err = reldb.postgresClient.Exec(ctx, query, customerPlan, paymentSource, lastPaymentts, customerID)
return err
}

func addWalletPublicKeys(tx pgx.Tx, customerID int, walletPublicKeys []string) error {
insertWalletKeyQuery := `
INSERT INTO wallet_public_keys (customer_id, public_key)
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/relDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ type RelDatastore interface {
RemoveWalletKeys(publicKey []string) error
GetCustomerIDByWalletPublicKey(publicKey string) (int, error)
GetCustomerByPublicKey(publicKey string) (*Customer, error)
UpdateCustomerPlan(ctx context.Context, customerID int, customerPlan int, paymentSource string, lastPayment string) error
GetAccessLevel(publicKey string) (string, error)

GetAllChains() (chainconfigs []dia.ChainConfig, err error)
Expand All @@ -207,6 +208,7 @@ type RelDatastore interface {

InsertLoopPaymentTransferProcessed(ctx context.Context, record LoopPaymentTransferProcessed) error
InsertLoopPaymentResponse(ctx context.Context, response LoopPaymentResponse) error
GetLoopPaymentResponseByAgreementID(ctx context.Context, agreementID string) (*LoopPaymentResponse, error)
}

const (
Expand Down

0 comments on commit 72c2072

Please sign in to comment.