Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Expose entry_id in the transactions API (#4528)

Richard6b2710baa534
diff --git a/app/views/api/v1/transactions/_transaction.json.jbuilder b/app/views/api/v1/transactions/_transaction.json.jbuilder
index 5584c5da58..9c39d2775a 100644
--- a/app/views/api/v1/transactions/_transaction.json.jbuilder
+++ b/app/views/api/v1/transactions/_transaction.json.jbuilder
@@ -1,6 +1,9 @@
# frozen_string_literal: true
json.id transaction.id
+# Web routes for a single transaction (/transactions/:id) take the entry id,
+# not the transaction id, so expose it for clients that link back into the app.
+json.entry_id transaction.entry.id
json.date transaction.entry.date
json.amount transaction.entry.amount_money.format
diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml
index 79f8f6e79a..12dd92ae50 100644
--- a/docs/api/openapi.yaml
+++ b/docs/api/openapi.yaml
@@ -1733,6 +1733,7 @@ components:
type: object
required:
- id
+ - entry_id
- date
- amount
- amount_decimal
@@ -1750,6 +1751,11 @@ components:
id:
type: string
format: uuid
+ entry_id:
+ type: string
+ format: uuid
+ description: Id of the underlying entry. Web routes for a single transaction,
+ such as /transactions/{entry_id}, use this id rather than id.
date:
type: string
format: date
diff --git a/docs/api/transactions.md b/docs/api/transactions.md
index 752023a882..bc30caf353 100644
--- a/docs/api/transactions.md
+++ b/docs/api/transactions.md
@@ -64,6 +64,7 @@ A transaction response includes:
```json
{
"id": "uuid",
+ "entry_id": "uuid",
"date": "2024-01-15",
"amount": "$75.50",
"amount_cents": 7550,
@@ -116,6 +117,10 @@ A transaction response includes:
| `excluded` | `true` when the transaction is left out of budget and report totals. |
| `split_parent` | `true` when the transaction has been split. Splitting a transaction marks it excluded, and its split children carry the amount. |
+### Linking to a transaction
+
+`id` identifies the transaction in the API. Web routes for a single transaction take the id of its entry instead, so use `entry_id` to link into the app, for example `/transactions/{entry_id}`.
+
## Creating transactions
When creating a transaction, the `nature` field determines how the amount is stored:
diff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rb
index 5635ec807d..9bb95f4306 100644
--- a/spec/swagger_helper.rb
+++ b/spec/swagger_helper.rb
@@ -936,9 +936,14 @@ RSpec.configure do |config|
},
Transaction: {
type: :object,
- required: %w[id date amount amount_decimal currency name classification kind excluded split_parent account tags created_at updated_at],
+ required: %w[id entry_id date amount amount_decimal currency name classification kind excluded split_parent account tags created_at updated_at],
properties: {
id: { type: :string, format: :uuid },
+ entry_id: {
+ type: :string,
+ format: :uuid,
+ description: 'Id of the underlying entry. Web routes for a single transaction, such as /transactions/{entry_id}, use this id rather than id.'
+ },
date: { type: :string, format: :date },
amount: { type: :string, description: 'Formatted amount, rounded to the currency display precision' },
amount_decimal: {
diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb
index 3573ab1935..1f12f96a7f 100644
--- a/test/controllers/api/v1/transactions_controller_test.rb
+++ b/test/controllers/api/v1/transactions_controller_test.rb
@@ -256,6 +256,7 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
response_data = JSON.parse(response.body)
assert_equal @transaction.id, response_data["id"]
+ assert_equal @transaction.entry.id, response_data["entry_id"]
assert response_data.key?("name")
assert response_data.key?("amount")
assert_amount_cents_fields(response_data)