UpdateTransactionStatus updates the status of a transaction in the database. It traces the operation using OpenTelemetry and returns an error if the update fails or if the transaction is not found. Parameters: - ctx: Context for managing the request and tracing. - id: The ID of the transaction to up
(ctx context.Context, id string, status string)
| 615 | // Returns: |
| 616 | // - An error if the update fails or if the transaction is not found. |
| 617 | func (d Datasource) UpdateTransactionStatus(ctx context.Context, id string, status string) error { |
| 618 | // Start a new tracing span for the update operation |
| 619 | ctx, span := otel.Tracer("transaction.database").Start(ctx, "UpdateTransactionStatus") |
| 620 | defer span.End() |
| 621 | |
| 622 | // Execute the update query |
| 623 | result, err := d.Conn.ExecContext(ctx, ` |
| 624 | UPDATE ledgerforge.transactions |
| 625 | SET status = $2 |
| 626 | WHERE transaction_id = $1 |
| 627 | `, id, status) |
| 628 | if err != nil { |
| 629 | span.RecordError(err) |
| 630 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to update transaction status", err) |
| 631 | } |
| 632 | |
| 633 | // Check how many rows were affected by the update |
| 634 | rowsAffected, err := result.RowsAffected() |
| 635 | if err != nil { |
| 636 | span.RecordError(err) |
| 637 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to get rows affected", err) |
| 638 | } |
| 639 | |
| 640 | // If no rows were affected, return a not found error |
| 641 | if rowsAffected == 0 { |
| 642 | span.AddEvent("Transaction not found for status update", trace.WithAttributes( |
| 643 | attribute.String("transaction.id", id), |
| 644 | attribute.String("transaction.status", status), |
| 645 | )) |
| 646 | return apierror.NewAPIError(apierror.ErrNotFound, fmt.Sprintf("Transaction with ID '%s' not found", id), nil) |
| 647 | } |
| 648 | |
| 649 | // Log the successful status update in the tracing span |
| 650 | span.AddEvent("Transaction status updated", trace.WithAttributes( |
| 651 | attribute.String("transaction.id", id), |
| 652 | attribute.String("transaction.status", status), |
| 653 | )) |
| 654 | return nil |
| 655 | } |
| 656 | |
| 657 | // GetAllTransactions retrieves all transactions from the database, ordered by creation date in descending order. |
| 658 | // It traces the operation using OpenTelemetry and returns an error if the retrieval or processing fails. |