feat: add valuations API endpoints for managing account reconciliations (#745)

* feat: add valuations API endpoints for managing account reconciliations

* refactor: formatting

* fix: make account extraction clearer

* feat: validation and error handling improvements

* feat: transaction

* feat: error handling

* Add API documentation LLM context

* Make it easier for people

* feat: transaction in creation

* feat: add OpenAPI spec for Valuations API

* fix: update notes validation to check for key presence

* Prevent double render

* All other docs use `apiKeyAuth`

* More `apiKeyAuth`

* Remove testing assertions from API doc specs

* fix: correct valuation entry references

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
Pere Montpeó
2026-01-30 18:54:15 +01:00
committed by GitHub
parent 02cd84568e
commit 9f5fdd4d13
16 changed files with 1014 additions and 168 deletions

View File

@@ -76,12 +76,7 @@ RSpec.describe 'API V1 Accounts', type: :request do
response '200', 'accounts listed' do
schema '$ref' => '#/components/schemas/AccountCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('accounts')).to be_present
expect(payload.fetch('accounts').length).to eq(3)
expect(payload.fetch('pagination')).to include('page', 'per_page', 'total_count', 'total_pages')
end
run_test!
end
response '200', 'accounts paginated' do
@@ -90,12 +85,7 @@ RSpec.describe 'API V1 Accounts', type: :request do
let(:page) { 1 }
let(:per_page) { 2 }
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('accounts').length).to eq(2)
expect(payload.dig('pagination', 'per_page')).to eq(2)
expect(payload.dig('pagination', 'total_count')).to eq(3)
end
run_test!
end
end
end

View File

@@ -83,11 +83,7 @@ RSpec.describe 'API V1 Categories', type: :request do
response '200', 'categories listed' do
schema '$ref' => '#/components/schemas/CategoryCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('categories')).to be_present
expect(payload.fetch('pagination')).to include('page', 'per_page', 'total_count', 'total_pages')
end
run_test!
end
response '200', 'categories filtered by classification' do
@@ -95,12 +91,7 @@ RSpec.describe 'API V1 Categories', type: :request do
let(:classification) { 'expense' }
run_test! do |response|
payload = JSON.parse(response.body)
payload.fetch('categories').each do |category|
expect(category.fetch('classification')).to eq('expense')
end
end
run_test!
end
response '200', 'root categories only' do
@@ -108,12 +99,7 @@ RSpec.describe 'API V1 Categories', type: :request do
let(:roots_only) { true }
run_test! do |response|
payload = JSON.parse(response.body)
payload.fetch('categories').each do |category|
expect(category.fetch('parent')).to be_nil
end
end
run_test!
end
response '200', 'categories filtered by parent' do
@@ -121,12 +107,7 @@ RSpec.describe 'API V1 Categories', type: :request do
let(:parent_id) { parent_category.id }
run_test! do |response|
payload = JSON.parse(response.body)
payload.fetch('categories').each do |category|
expect(category.dig('parent', 'id')).to eq(parent_category.id)
end
end
run_test!
end
end
end
@@ -144,13 +125,7 @@ RSpec.describe 'API V1 Categories', type: :request do
response '200', 'category retrieved' do
schema '$ref' => '#/components/schemas/CategoryDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('id')).to eq(parent_category.id)
expect(payload.fetch('name')).to eq('Food & Drink')
expect(payload.fetch('classification')).to eq('expense')
expect(payload.fetch('subcategories_count')).to eq(1)
end
run_test!
end
response '200', 'subcategory retrieved with parent' do
@@ -158,13 +133,7 @@ RSpec.describe 'API V1 Categories', type: :request do
let(:id) { subcategory.id }
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('id')).to eq(subcategory.id)
expect(payload.fetch('name')).to eq('Restaurants')
expect(payload.dig('parent', 'id')).to eq(parent_category.id)
expect(payload.dig('parent', 'name')).to eq('Food & Drink')
end
run_test!
end
response '404', 'category not found' do

View File

@@ -83,11 +83,7 @@ RSpec.describe 'API V1 Chats', type: :request do
response '200', 'chats listed' do
schema '$ref' => '#/components/schemas/ChatCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('chats')).to be_present
expect(payload.fetch('pagination')).to include('page', 'per_page', 'total_count', 'total_pages')
end
run_test!
end
response '403', 'AI features disabled' do
@@ -132,11 +128,7 @@ RSpec.describe 'API V1 Chats', type: :request do
response '201', 'chat created' do
schema '$ref' => '#/components/schemas/ChatDetail'
run_test! do |response|
payload = JSON.parse(response.body)
chat_record = Chat.find(payload.fetch('id'))
expect(chat_record.messages.first.content).to eq('Can you help me plan a summer trip?')
end
run_test!
end
response '422', 'validation error' do
@@ -162,10 +154,7 @@ RSpec.describe 'API V1 Chats', type: :request do
response '200', 'chat retrieved' do
schema '$ref' => '#/components/schemas/ChatDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('messages').size).to be >= 1
end
run_test!
end
response '404', 'chat not found' do
@@ -197,10 +186,7 @@ RSpec.describe 'API V1 Chats', type: :request do
response '200', 'chat updated' do
schema '$ref' => '#/components/schemas/ChatDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('title')).to eq('Updated budget plan')
end
run_test!
end
response '404', 'chat not found' do
@@ -269,10 +255,7 @@ RSpec.describe 'API V1 Chats', type: :request do
response '201', 'message created' do
schema '$ref' => '#/components/schemas/MessageResponse'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('ai_response_status')).to eq('pending')
end
run_test!
end
response '404', 'chat not found' do
@@ -310,10 +293,7 @@ RSpec.describe 'API V1 Chats', type: :request do
allow_any_instance_of(AssistantMessage).to receive(:valid?).and_return(true)
end
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('message')).to eq('Retry initiated')
end
run_test!
end
response '404', 'chat not found' do

View File

@@ -81,11 +81,7 @@ RSpec.describe 'API V1 Imports', type: :request do
response '200', 'imports listed' do
schema '$ref' => '#/components/schemas/ImportCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('data')).to be_present
expect(payload.fetch('meta')).to include('current_page', 'total_count', 'total_pages', 'per_page')
end
run_test!
end
response '200', 'imports filtered by status' do
@@ -93,12 +89,7 @@ RSpec.describe 'API V1 Imports', type: :request do
let(:status) { 'pending' }
run_test! do |response|
payload = JSON.parse(response.body)
payload.fetch('data').each do |import|
expect(import.fetch('status')).to eq('pending')
end
end
run_test!
end
response '200', 'imports filtered by type' do
@@ -106,12 +97,7 @@ RSpec.describe 'API V1 Imports', type: :request do
let(:type) { 'TransactionImport' }
run_test! do |response|
payload = JSON.parse(response.body)
payload.fetch('data').each do |import|
expect(import.fetch('type')).to eq('TransactionImport')
end
end
run_test!
end
end
@@ -203,12 +189,7 @@ RSpec.describe 'API V1 Imports', type: :request do
}
end
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.dig('data', 'id')).to be_present
expect(payload.dig('data', 'type')).to eq('TransactionImport')
expect(payload.dig('data', 'status')).to eq('pending')
end
run_test!
end
response '422', 'validation error - file too large' do
@@ -240,13 +221,7 @@ RSpec.describe 'API V1 Imports', type: :request do
response '200', 'import retrieved' do
schema '$ref' => '#/components/schemas/ImportResponse'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.dig('data', 'id')).to eq(pending_import.id)
expect(payload.dig('data', 'type')).to eq('TransactionImport')
expect(payload.dig('data', 'configuration')).to be_present
expect(payload.dig('data', 'stats')).to be_present
end
run_test!
end
response '404', 'import not found' do

View File

@@ -54,12 +54,7 @@ RSpec.describe 'API V1 Tags', type: :request do
response '200', 'tags listed' do
schema '$ref' => '#/components/schemas/TagCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload).to be_an(Array)
expect(payload.length).to eq(3)
expect(payload.first).to include('id', 'name', 'color', 'created_at', 'updated_at')
end
run_test!
end
end
@@ -95,11 +90,7 @@ RSpec.describe 'API V1 Tags', type: :request do
}
end
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Business')
expect(payload.fetch('color')).to eq('#8b5cf6')
end
run_test!
end
response '201', 'tag created with auto-assigned color' do
@@ -113,11 +104,7 @@ RSpec.describe 'API V1 Tags', type: :request do
}
end
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Travel')
expect(payload.fetch('color')).to be_present
end
run_test!
end
response '422', 'validation error - missing name' do
@@ -149,12 +136,7 @@ RSpec.describe 'API V1 Tags', type: :request do
response '200', 'tag retrieved' do
schema '$ref' => '#/components/schemas/TagDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('id')).to eq(essential_tag.id)
expect(payload.fetch('name')).to eq('Essential')
expect(payload.fetch('color')).to eq('#22c55e')
end
run_test!
end
response '404', 'tag not found' do
@@ -199,11 +181,7 @@ RSpec.describe 'API V1 Tags', type: :request do
response '200', 'tag updated' do
schema '$ref' => '#/components/schemas/TagDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Must Have')
expect(payload.fetch('color')).to eq('#10b981')
end
run_test!
end
response '404', 'tag not found' do

View File

@@ -132,11 +132,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
response '200', 'transactions listed' do
schema '$ref' => '#/components/schemas/TransactionCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('transactions')).to be_present
expect(payload.fetch('pagination')).to include('page', 'per_page', 'total_count', 'total_pages')
end
run_test!
end
response '200', 'transactions filtered by account' do
@@ -144,10 +140,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
let(:account_id) { account.id }
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('transactions')).to be_present
end
run_test!
end
response '200', 'transactions filtered by date range' do
@@ -156,10 +149,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
let(:start_date) { (Date.current - 7.days).to_s }
let(:end_date) { Date.current.to_s }
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('transactions')).to be_present
end
run_test!
end
end
@@ -209,11 +199,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
response '201', 'transaction created' do
schema '$ref' => '#/components/schemas/Transaction'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Test purchase')
expect(payload.fetch('account').fetch('id')).to eq(account.id)
end
run_test!
end
response '422', 'validation error - missing account_id' do
@@ -261,14 +247,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
response '200', 'transaction retrieved' do
schema '$ref' => '#/components/schemas/Transaction'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('id')).to eq(transaction.id)
expect(payload.fetch('name')).to eq('Grocery shopping')
expect(payload.fetch('category').fetch('name')).to eq('Groceries')
expect(payload.fetch('merchant').fetch('name')).to eq('Whole Foods')
expect(payload.fetch('tags').first.fetch('name')).to eq('Essential')
end
run_test!
end
response '404', 'transaction not found' do
@@ -321,11 +300,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
response '200', 'transaction updated' do
schema '$ref' => '#/components/schemas/Transaction'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Updated grocery shopping')
expect(payload.fetch('notes')).to eq('Weekly groceries')
end
run_test!
end
response '404', 'transaction not found' do
@@ -347,10 +322,7 @@ RSpec.describe 'API V1 Transactions', type: :request do
response '200', 'transaction deleted' do
schema '$ref' => '#/components/schemas/DeleteResponse'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('message')).to eq('Transaction deleted successfully')
end
run_test!
end
response '404', 'transaction not found' do

View File

@@ -0,0 +1,287 @@
# frozen_string_literal: true
require 'swagger_helper'
RSpec.describe 'API V1 Valuations', type: :request do
let(:family) do
Family.create!(
name: 'API Family',
currency: 'USD',
locale: 'en',
date_format: '%m-%d-%Y'
)
end
let(:user) do
family.users.create!(
email: 'api-user@example.com',
password: 'password123',
password_confirmation: 'password123'
)
end
let(:oauth_application) do
Doorkeeper::Application.create!(
name: 'API Docs',
redirect_uri: 'https://example.com/callback',
scopes: 'read read_write'
)
end
let(:access_token) do
Doorkeeper::AccessToken.create!(
application: oauth_application,
resource_owner_id: user.id,
scopes: 'read_write',
expires_in: 2.hours,
token: SecureRandom.hex(32)
)
end
let(:Authorization) { "Bearer #{access_token.token}" }
let(:account) do
Account.create!(
family: family,
name: 'Investment Account',
balance: 10000,
currency: 'USD',
accountable: Investment.create!
)
end
let!(:valuation_entry) do
account.entries.create!(
name: 'Investment Reconciliation',
date: Date.current,
amount: 10000,
currency: 'USD',
entryable: Valuation.new(
kind: 'reconciliation'
)
)
end
let!(:valuation) { valuation_entry.entryable }
let!(:valuation_id) { valuation_entry.id }
path '/api/v1/valuations' do
post 'Create valuation' do
tags 'Valuations'
security [ { apiKeyAuth: [] } ]
consumes 'application/json'
produces 'application/json'
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with write scope'
parameter name: :body, in: :body, required: true, schema: {
type: :object,
properties: {
valuation: {
type: :object,
properties: {
account_id: { type: :string, format: :uuid, description: 'Account ID (required)' },
amount: { type: :number, description: 'Valuation amount (required)' },
date: { type: :string, format: :date, description: 'Valuation date (required)' },
notes: { type: :string, description: 'Additional notes' }
},
required: %w[account_id amount date]
}
},
required: %w[valuation]
}
let(:body) do
{
valuation: {
account_id: account.id,
amount: 15000.00,
date: Date.current.to_s
}
}
end
response '201', 'valuation created' do
schema '$ref' => '#/components/schemas/Valuation'
run_test! do |response|
data = JSON.parse(response.body)
created_id = data.fetch('id')
get "/api/v1/valuations/#{created_id}", headers: { 'Authorization' => Authorization }
expect(response).to have_http_status(:ok)
fetched = JSON.parse(response.body)
expect(fetched['id']).to eq(created_id)
end
end
response '422', 'validation error - missing account_id' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
valuation: {
amount: 15000.00,
date: Date.current.to_s
}
}
end
run_test!
end
response '422', 'validation error - missing amount' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
valuation: {
account_id: account.id,
date: Date.current.to_s
}
}
end
run_test!
end
response '422', 'validation error - missing date' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
valuation: {
account_id: account.id,
amount: 15000.00
}
}
end
run_test!
end
response '404', 'account not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
valuation: {
account_id: SecureRandom.uuid,
amount: 15000.00,
date: Date.current.to_s
}
}
end
run_test!
end
end
end
path '/api/v1/valuations/{id}' do
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token'
parameter name: :id, in: :path, type: :string, required: true, description: 'Valuation ID (entry ID)'
get 'Retrieve a valuation' do
tags 'Valuations'
security [ { apiKeyAuth: [] } ]
produces 'application/json'
let(:id) { valuation_id }
response '200', 'valuation retrieved' do
schema '$ref' => '#/components/schemas/Valuation'
run_test!
end
response '404', 'valuation not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
patch 'Update a valuation' do
tags 'Valuations'
security [ { apiKeyAuth: [] } ]
consumes 'application/json'
produces 'application/json'
let(:id) { valuation_id }
parameter name: :body, in: :body, required: true, schema: {
type: :object,
properties: {
valuation: {
type: :object,
properties: {
amount: { type: :number, description: 'New valuation amount (must provide with date)' },
date: { type: :string, format: :date, description: 'New valuation date (must provide with amount)' },
notes: { type: :string, description: 'Additional notes' }
}
}
}
}
response '200', 'valuation updated with notes' do
schema '$ref' => '#/components/schemas/Valuation'
let(:body) do
{
valuation: {
notes: 'Quarterly valuation update'
}
}
end
run_test!
end
response '200', 'valuation updated with amount and date' do
schema '$ref' => '#/components/schemas/Valuation'
let(:body) do
{
valuation: {
amount: 12000.00,
date: (Date.current - 1.day).to_s
}
}
end
run_test!
end
response '422', 'validation error - only one of amount/date provided' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
valuation: {
amount: 12000.00
}
}
end
run_test!
end
response '404', 'valuation not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
let(:body) do
{
valuation: {
notes: 'This will fail'
}
}
end
run_test!
end
end
end
end