mirror of
https://github.com/we-promise/sure.git
synced 2026-12-06 17:25:37 +08:00
Compare tag ids in canonical UUID form, and store each once
The unknown-tag check counted raw strings, so one tag sent in two cases was two ids against one match, and the request failed as "Unknown tag id". Correcting the count exposed the writes: tag_ids= was given the raw list, so the same tag would have been stored twice. requested_tag_ids casts each id with the tags' id type and removes duplicates. The check, the create and the update all read it. An id that is not a UUID casts to nil, matches no tag and is still refused.
This commit is contained in:
@@ -157,7 +157,7 @@ class Api::V1::TransactionsController < Api::V1::BaseController
|
||||
# Handle tags separately - only when explicitly provided in the request
|
||||
# This allows clearing tags with tag_ids: [] while preserving tags when not specified
|
||||
if tags_provided?
|
||||
@entry.transaction.tag_ids = transaction_params[:tag_ids] || []
|
||||
@entry.transaction.tag_ids = requested_tag_ids
|
||||
@entry.transaction.save!
|
||||
@entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
|
||||
end
|
||||
@@ -340,7 +340,7 @@ class Api::V1::TransactionsController < Api::V1::BaseController
|
||||
entryable_attributes: {
|
||||
category_id: transaction_params[:category_id],
|
||||
merchant_id: transaction_params[:merchant_id],
|
||||
tag_ids: transaction_params[:tag_ids] || []
|
||||
tag_ids: requested_tag_ids
|
||||
}
|
||||
}
|
||||
if idempotency_key_requested?
|
||||
@@ -388,11 +388,22 @@ class Api::V1::TransactionsController < Api::V1::BaseController
|
||||
|
||||
# Tags belong to a family: ids are resolved through the caller's own, so
|
||||
# another family's tag is refused like one that does not exist.
|
||||
# An id that is not a UUID casts to nil, which matches no tag, so the
|
||||
# count falls short and it is unknown too.
|
||||
def unknown_tag_ids?
|
||||
ids = Array(transaction_params[:tag_ids]).compact_blank.uniq
|
||||
ids = requested_tag_ids
|
||||
ids.any? && current_resource_owner.family.tags.where(id: ids).count != ids.size
|
||||
end
|
||||
|
||||
# The request's tag ids in canonical UUID form, each once. Postgres matches
|
||||
# a UUID in any case, so one tag spelled two ways is one tag, and writing
|
||||
# the raw list would store it twice.
|
||||
def requested_tag_ids
|
||||
@requested_tag_ids ||= Array(transaction_params[:tag_ids]).compact_blank
|
||||
.map { |id| Tag.type_for_attribute(:id).cast(id) }
|
||||
.uniq
|
||||
end
|
||||
|
||||
def split_financial_fields_changed?
|
||||
params.dig(:transaction, :amount).present? ||
|
||||
params.dig(:transaction, :date).present? ||
|
||||
|
||||
@@ -858,6 +858,45 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_equal tags.map(&:id).sort, @transaction.reload.tag_ids.sort
|
||||
end
|
||||
|
||||
# One tag named in two spellings is one tag: Postgres matches a UUID in any
|
||||
# case, so counting raw strings would call it unknown.
|
||||
test "create with one tag spelled in two cases tags it once" do
|
||||
tag = @family.tags.first
|
||||
|
||||
assert_difference "Tagging.count", 1 do
|
||||
post api_v1_transactions_url,
|
||||
params: { transaction: { account_id: @account.id, name: "Tagged", amount: 25.00, date: Date.current,
|
||||
currency: "USD", nature: "expense", tag_ids: [ tag.id, tag.id.upcase ] } },
|
||||
headers: api_headers(@api_key)
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "update with one tag spelled in two cases assigns it once" do
|
||||
tag = @family.tags.first
|
||||
@transaction.update!(tag_ids: [])
|
||||
|
||||
put api_v1_transaction_url(@transaction),
|
||||
params: { transaction: { tag_ids: [ tag.id, tag.id.upcase ] } },
|
||||
headers: api_headers(@api_key)
|
||||
|
||||
assert_response :success
|
||||
assert_equal [ tag.id ], @transaction.reload.tag_ids
|
||||
end
|
||||
|
||||
test "update with a tag id that is not a UUID is refused and changes nothing" do
|
||||
@transaction.update!(tag_ids: [ @family.tags.first.id ])
|
||||
before_ids = @transaction.reload.tag_ids
|
||||
|
||||
put api_v1_transaction_url(@transaction),
|
||||
params: { transaction: { tag_ids: [ @family.tags.first.id, "not-a-uuid" ] } },
|
||||
headers: api_headers(@api_key)
|
||||
|
||||
assert_response :unprocessable_entity
|
||||
assert_equal before_ids, @transaction.reload.tag_ids
|
||||
end
|
||||
|
||||
# Strong parameters drops a tag_ids of any other shape, which would otherwise
|
||||
# read as "no tags" and clear them.
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user