Skip to content

Bug #1220 - Adding alpha ordering to /items/fields endpoint#1394

Open
bmyoungquist wants to merge 1 commit intosysadminsmedia:mainfrom
bmyoungquist:1220-sort-custom-fields-on-search-page
Open

Bug #1220 - Adding alpha ordering to /items/fields endpoint#1394
bmyoungquist wants to merge 1 commit intosysadminsmedia:mainfrom
bmyoungquist:1220-sort-custom-fields-on-search-page

Conversation

@bmyoungquist
Copy link
Copy Markdown

@bmyoungquist bmyoungquist commented Mar 23, 2026

What type of PR is this?

  • bug

What this PR does / why we need it:

  • fixes issue where custom fields can be difficult to find because they are not ordered

Which issue(s) this PR fixes:

Fixes #1220

Testing

Manually tested by interacting with the custom field filter on the search page

Summary by CodeRabbit

  • Bug Fixes
    • Fixed custom field names to appear in consistent alphabetical order.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 23, 2026

Walkthrough

The GetAllCustomFieldNames query in the repository layer now enforces ascending alphabetical ordering on custom field names, ensuring consistent and user-friendly display in search dropdown lists.

Changes

Cohort / File(s) Summary
Query Ordering
backend/internal/data/repo/repo_items.go
Added Order(ent.Asc(itemfield.FieldName)) to sort custom field names alphanumerically in ascending order.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

Alphabets dance in perfect line,
Custom fields now sorted fine,
Where chaos once did reign supreme,
Order flows like gentle stream.
A single line, a simple fix,
Makes searching easier—click, click, click!


Security Note: No security concerns identified. This change only adds deterministic ordering to field name retrieval with no impact on authorization, data exposure, or input validation.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly references the bug number and describes the specific fix: adding alpha ordering to the /items/fields endpoint.
Description check ✅ Passed The description covers all required sections: PR type (bug), what it does and why, the issue it fixes (#1220), and testing performed.
Linked Issues check ✅ Passed The code change directly addresses issue #1220 by adding deterministic ascending ordering to custom field names in the GetAllCustomFieldNames query.
Out of Scope Changes check ✅ Passed The single-line change is tightly scoped to the specific issue: adding Order() clause to sort custom fields alphanumerically with no extraneous modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/internal/data/repo/repo_items.go (1)

1190-1198: ⚠️ Potential issue | 🟠 Major

Field names are sorted, but field values are still unsorted

Line 1196 fixes deterministic ordering for names, but Issue #1220 also asks for the Values dropdown to be alphanumeric. GetAllCustomFieldValues still uses Unique(true) + Select(...) without Order(...), so value order remains nondeterministic.

Suggested follow-up patch
func (e *ItemsRepository) GetAllCustomFieldValues(ctx context.Context, gid uuid.UUID, name string) ([]string, error) {
    type st struct {
        Value string `json:"text_value"`
    }

    var values []st

    err := e.db.Item.Query().
        Where(
            item.HasGroupWith(group.ID(gid)),
        ).
        QueryFields().
        Where(
            itemfield.Name(name),
        ).
        Unique(true).
+       Order(ent.Asc(itemfield.FieldTextValue)).
        Select(itemfield.FieldTextValue).
        Scan(ctx, &values)

Security recommendation: keep the group.ID(gid) tenant predicate in both names and values queries (as currently implemented) to avoid cross-tenant metadata leakage.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/internal/data/repo/repo_items.go` around lines 1190 - 1198,
GetAllCustomFieldValues currently returns deduplicated values without
deterministic ordering; update the query that builds values to include an Order
clause (e.g., Order(ent.Asc(itemfield.FieldValue))) in the chain that uses
Unique(true) + Select(...) so the Values dropdown is alphanumeric and still
scoped by the tenant predicate (item.HasGroupWith(group.ID(gid))). Locate the
query in GetAllCustomFieldValues that calls
e.db.Item.Query()...QueryFields()...Select(itemfield.FieldValue) and append the
appropriate Order(...) using itemfield.FieldValue.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@backend/internal/data/repo/repo_items.go`:
- Around line 1190-1198: GetAllCustomFieldValues currently returns deduplicated
values without deterministic ordering; update the query that builds values to
include an Order clause (e.g., Order(ent.Asc(itemfield.FieldValue))) in the
chain that uses Unique(true) + Select(...) so the Values dropdown is
alphanumeric and still scoped by the tenant predicate
(item.HasGroupWith(group.ID(gid))). Locate the query in GetAllCustomFieldValues
that calls e.db.Item.Query()...QueryFields()...Select(itemfield.FieldValue) and
append the appropriate Order(...) using itemfield.FieldValue.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b7eb8a7a-4426-4678-bc71-e613634c8bc0

📥 Commits

Reviewing files that changed from the base of the PR and between 144920c and bbb36cd.

📒 Files selected for processing (1)
  • backend/internal/data/repo/repo_items.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lists displayed when searching custom fields should sort alphanumerically but currently do not

1 participant