IsTransactionRefunded checks if a transaction has already been refunded by looking for a transaction that has the inverse source/destination and references the original transaction as its parent. Parameters: - ctx: Context for managing request and tracing. - transaction: The original transaction to
(ctx context.Context, transaction *model.Transaction)
| 1513 | // - bool: true if the transaction has been refunded, false otherwise |
| 1514 | // - error: an error if the check fails |
| 1515 | func (d Datasource) IsTransactionRefunded(ctx context.Context, transaction *model.Transaction) (bool, error) { |
| 1516 | ctx, span := otel.Tracer("transaction.database").Start(ctx, "IsTransactionRefunded") |
| 1517 | defer span.End() |
| 1518 | |
| 1519 | var exists bool |
| 1520 | err := d.Conn.QueryRowContext(ctx, ` |
| 1521 | SELECT EXISTS ( |
| 1522 | SELECT 1 |
| 1523 | FROM ledgerforge.transactions |
| 1524 | WHERE parent_transaction = $1 |
| 1525 | AND source = $2 |
| 1526 | AND destination = $3 |
| 1527 | ) |
| 1528 | `, transaction.TransactionID, transaction.Destination, transaction.Source).Scan(&exists) |
| 1529 | if err != nil { |
| 1530 | span.RecordError(err) |
| 1531 | return false, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to check refund status", err) |
| 1532 | } |
| 1533 | |
| 1534 | span.AddEvent("Refund status checked", trace.WithAttributes( |
| 1535 | attribute.String("transaction.id", transaction.TransactionID), |
| 1536 | attribute.Bool("is_refunded", exists), |
| 1537 | )) |
| 1538 | |
| 1539 | return exists, nil |
| 1540 | } |
| 1541 | |
| 1542 | // GetTransactionsByCriteria retrieves transactions based on specified criteria (amount range, currency, date range). |
| 1543 | // It supports pagination via limit and offset. |