Files
sure/spec/requests/api/v1/transactions_spec.rb
Ang Wei Feng (Ted) c77971ea0d fix: Preserve tags on bulk edits (take 3) (#889)
* fix: handle tags separately from entryable_attributes in bulk updates

Tags use a join table (taggings) rather than a direct column, which means
empty tag_ids clears all tags rather than meaning "no change". This caused
bulk category-only edits to accidentally clear existing tags.

This fix:
- Removes tag_ids from entryable_attributes in Entry.bulk_update!
- Adds update_tags parameter to explicitly control tag updates
- Uses params.key?(:tag_ids) in controller to detect explicit tag changes
- Preserves existing tags when tag_ids is not provided in the request

This is a cleaner architectural solution compared to tracking "touched"
state in the frontend, as it properly acknowledges the semantic difference
between column attributes and join table associations.

https://claude.ai/code/session_014CsmTwjteP4qJs6YZqCKnY

* fix: handle tags separately in API transaction updates

Apply the same pattern to the API endpoint: tags are now handled
separately from entryable_attributes to distinguish between "not
provided" (preserve existing tags) and "explicitly set to empty"
(clear all tags).

This allows API consumers to:
- Update other fields without affecting tags (omit tag_ids)
- Clear all tags (send tag_ids: [])
- Set specific tags (send tag_ids: [id1, id2])

https://claude.ai/code/session_014CsmTwjteP4qJs6YZqCKnY

* Proposed fix

* fix: improve tag handling in bulk updates for transactions

* fix: allow bulk edit to clear/preserve tags by omitting hidden multi-select field

* PR comments

* Dumb copy/paste error

* Linter

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <jjmata@jjmata.com>
2026-02-06 14:11:46 +01:00

342 lines
10 KiB
Ruby

# frozen_string_literal: true
require 'swagger_helper'
RSpec.describe 'API V1 Transactions', 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(:api_key) do
key = ApiKey.generate_secure_key
ApiKey.create!(
user: user,
name: 'API Docs Key',
key: key,
scopes: %w[read_write],
source: 'web'
)
end
let(:'X-Api-Key') { api_key.plain_key }
let(:account) do
Account.create!(
family: family,
name: 'Checking Account',
balance: 1000,
currency: 'USD',
accountable: Depository.create!
)
end
let(:category) do
family.categories.create!(
name: 'Groceries',
classification: 'expense',
color: '#4CAF50',
lucide_icon: 'shopping-cart'
)
end
let(:merchant) do
family.merchants.create!(name: 'Whole Foods')
end
let(:tag) do
family.tags.create!(name: 'Essential', color: '#2196F3')
end
let!(:transaction) do
entry = account.entries.create!(
name: 'Grocery shopping',
date: Date.current,
amount: 75.50,
currency: 'USD',
entryable: Transaction.new(
category: category,
merchant: merchant
)
)
entry.transaction.tags << tag
entry.transaction
end
let!(:another_transaction) do
entry = account.entries.create!(
name: 'Coffee',
date: Date.current - 1.day,
amount: 5.00,
currency: 'USD',
entryable: Transaction.new
)
entry.transaction
end
path '/api/v1/transactions' do
get 'List transactions' do
tags 'Transactions'
security [ { apiKeyAuth: [] } ]
produces 'application/json'
parameter name: :page, in: :query, type: :integer, required: false,
description: 'Page number (default: 1)'
parameter name: :per_page, in: :query, type: :integer, required: false,
description: 'Items per page (default: 25, max: 100)'
parameter name: :account_id, in: :query, type: :string, required: false,
description: 'Filter by account ID'
parameter name: :category_id, in: :query, type: :string, required: false,
description: 'Filter by category ID'
parameter name: :merchant_id, in: :query, type: :string, required: false,
description: 'Filter by merchant ID'
parameter name: :start_date, in: :query, required: false,
description: 'Filter transactions from this date',
schema: { type: :string, format: :date }
parameter name: :end_date, in: :query, required: false,
description: 'Filter transactions until this date',
schema: { type: :string, format: :date }
parameter name: :min_amount, in: :query, type: :number, required: false,
description: 'Filter by minimum amount'
parameter name: :max_amount, in: :query, type: :number, required: false,
description: 'Filter by maximum amount'
parameter name: :type, in: :query, required: false,
description: 'Filter by transaction type',
schema: { type: :string, enum: %w[income expense] }
parameter name: :search, in: :query, type: :string, required: false,
description: 'Search by name, notes, or merchant name'
parameter name: :account_ids, in: :query, required: false,
description: 'Filter by multiple account IDs',
schema: { type: :array, items: { type: :string } }
parameter name: :category_ids, in: :query, required: false,
description: 'Filter by multiple category IDs',
schema: { type: :array, items: { type: :string } }
parameter name: :merchant_ids, in: :query, required: false,
description: 'Filter by multiple merchant IDs',
schema: { type: :array, items: { type: :string } }
parameter name: :tag_ids, in: :query, required: false,
description: 'Filter by tag IDs',
schema: { type: :array, items: { type: :string } }
response '200', 'transactions listed' do
schema '$ref' => '#/components/schemas/TransactionCollection'
run_test!
end
response '200', 'transactions filtered by account' do
schema '$ref' => '#/components/schemas/TransactionCollection'
let(:account_id) { account.id }
run_test!
end
response '200', 'transactions filtered by date range' do
schema '$ref' => '#/components/schemas/TransactionCollection'
let(:start_date) { (Date.current - 7.days).to_s }
let(:end_date) { Date.current.to_s }
run_test!
end
end
post 'Create transaction' do
tags 'Transactions'
security [ { apiKeyAuth: [] } ]
consumes 'application/json'
produces 'application/json'
parameter name: :body, in: :body, required: true, schema: {
type: :object,
properties: {
transaction: {
type: :object,
properties: {
account_id: { type: :string, format: :uuid, description: 'Account ID (required)' },
date: { type: :string, format: :date, description: 'Transaction date' },
amount: { type: :number, description: 'Transaction amount' },
name: { type: :string, description: 'Transaction name/description' },
description: { type: :string, description: 'Alternative to name field' },
notes: { type: :string, description: 'Additional notes' },
currency: { type: :string, description: 'Currency code (defaults to family currency)' },
category_id: { type: :string, format: :uuid, description: 'Category ID' },
merchant_id: { type: :string, format: :uuid, description: 'Merchant ID' },
nature: { type: :string, enum: %w[income expense inflow outflow], description: 'Transaction nature (determines sign)' },
tag_ids: { type: :array, items: { type: :string, format: :uuid }, description: 'Array of tag IDs' }
},
required: %w[account_id date amount name]
}
},
required: %w[transaction]
}
let(:body) do
{
transaction: {
account_id: account.id,
date: Date.current.to_s,
amount: 50.00,
name: 'Test purchase',
nature: 'expense',
category_id: category.id,
merchant_id: merchant.id
}
}
end
response '201', 'transaction created' do
schema '$ref' => '#/components/schemas/Transaction'
run_test!
end
response '422', 'validation error - missing account_id' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
transaction: {
date: Date.current.to_s,
amount: 50.00,
name: 'Test purchase'
}
}
end
run_test!
end
response '422', 'validation error - missing required fields' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
transaction: {
account_id: account.id
}
}
end
run_test!
end
end
end
path '/api/v1/transactions/{id}' do
parameter name: :id, in: :path, type: :string, required: true, description: 'Transaction ID'
get 'Retrieve a transaction' do
tags 'Transactions'
security [ { apiKeyAuth: [] } ]
produces 'application/json'
let(:id) { transaction.id }
response '200', 'transaction retrieved' do
schema '$ref' => '#/components/schemas/Transaction'
run_test!
end
response '404', 'transaction not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
patch 'Update a transaction' do
tags 'Transactions'
security [ { apiKeyAuth: [] } ]
consumes 'application/json'
produces 'application/json'
let(:id) { transaction.id }
parameter name: :body, in: :body, required: true, schema: {
type: :object,
properties: {
transaction: {
type: :object,
properties: {
date: { type: :string, format: :date },
amount: { type: :number },
name: { type: :string },
description: { type: :string, description: 'Alternative to name field' },
notes: { type: :string },
currency: { type: :string, description: 'Currency code' },
category_id: { type: :string, format: :uuid },
merchant_id: { type: :string, format: :uuid },
nature: { type: :string, enum: %w[income expense inflow outflow] },
tag_ids: {
type: :array,
items: { type: :string, format: :uuid },
description: 'Array of tag IDs to assign. Omit to preserve existing tags; use [] to clear all tags.'
}
}
}
}
}
let(:body) do
{
transaction: {
name: 'Updated grocery shopping',
notes: 'Weekly groceries'
}
}
end
response '200', 'transaction updated' do
schema '$ref' => '#/components/schemas/Transaction'
run_test!
end
response '404', 'transaction not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
delete 'Delete a transaction' do
tags 'Transactions'
security [ { apiKeyAuth: [] } ]
produces 'application/json'
let(:id) { another_transaction.id }
response '200', 'transaction deleted' do
schema '$ref' => '#/components/schemas/DeleteResponse'
run_test!
end
response '404', 'transaction not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
end
end