From 506ce664340036a91a84d0ef0f43df30de44dae8 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 5 Jun 2024 09:54:44 +0200 Subject: [PATCH] fix(typesense): correctly join task position table when sorting by it This change fixes a bug where the project view to use for joining was empty, since Typesense only supports 3 sorting parameters. When using more than that, the logic to fetch the view ID parameter would not return the correct parameter, but the logic building the order by statement would. That led to inconsistencies where the task position was included in the order by statement, but the table would not be joined, failing the query. --- pkg/models/task_search.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/models/task_search.go b/pkg/models/task_search.go index b113c5891..f9fbfc6b2 100644 --- a/pkg/models/task_search.go +++ b/pkg/models/task_search.go @@ -516,7 +516,6 @@ func (t *typesenseTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, filterBy = append(filterBy, "("+filter+")") } - var projectViewIDForPosition int64 var sortbyFields []string for i, param := range opts.sortby { // Validate the params @@ -533,7 +532,6 @@ func (t *typesenseTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, if param.sortBy == taskPropertyPosition { sortBy = "positions.view_" + strconv.FormatInt(param.projectViewID, 10) - projectViewIDForPosition = param.projectViewID } sortbyFields = append(sortbyFields, sortBy+"(missing_values:last):"+param.orderBy.String()) @@ -599,8 +597,11 @@ func (t *typesenseTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, In("id", taskIDs). OrderBy(orderby) - if projectViewIDForPosition != 0 { - query = query.Join("LEFT", "task_positions", "task_positions.task_id = tasks.id AND task_positions.project_view_id = ?", projectViewIDForPosition) + for _, param := range opts.sortby { + if param.sortBy == taskPropertyPosition { + query = query.Join("LEFT", "task_positions", "task_positions.task_id = tasks.id AND task_positions.project_view_id = ?", param.projectViewID) + break + } } err = query.Find(&tasks)