From 5beaee031dc95e479a1131770c10e37b842cbcd5 Mon Sep 17 00:00:00 2001 From: Tanmay Datta Date: Wed, 19 Apr 2023 14:59:49 +0100 Subject: [PATCH] [MM-51089] Fix sorting value of category in CreateSidebarCategoryForTeamForUser (#22455) Co-authored-by: Mattermost Build --- .../sqlstore/channel_store_categories.go | 2 +- .../storetest/channel_store_categories.go | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/server/channels/store/sqlstore/channel_store_categories.go b/server/channels/store/sqlstore/channel_store_categories.go index 3f2f726a0f..4eca0d5de0 100644 --- a/server/channels/store/sqlstore/channel_store_categories.go +++ b/server/channels/store/sqlstore/channel_store_categories.go @@ -335,7 +335,7 @@ func (s SqlChannelStore) CreateSidebarCategory(userId, teamId string, newCategor Id: newCategoryId, UserId: userId, TeamId: teamId, - Sorting: model.SidebarCategorySortDefault, + Sorting: newCategory.Sorting, SortOrder: int64(model.MinimalSidebarSortDistance * len(newOrder)), // first we place it at the end of the list Type: model.SidebarCategoryCustom, Muted: newCategory.Muted, diff --git a/server/channels/store/storetest/channel_store_categories.go b/server/channels/store/storetest/channel_store_categories.go index ecd49ef8c2..6ba934f45b 100644 --- a/server/channels/store/storetest/channel_store_categories.go +++ b/server/channels/store/storetest/channel_store_categories.go @@ -672,6 +672,38 @@ func testCreateSidebarCategory(t *testing.T, ss store.Store) { require.NoError(t, err) assert.Equal(t, []string{}, res2.Channels) }) + + t.Run("should store the correct sorting value", func(t *testing.T) { + userId := model.NewId() + + team := setupTeam(t, ss, userId) + + opts := &store.SidebarCategorySearchOpts{ + TeamID: team.Id, + ExcludeTeam: false, + } + res, nErr := ss.Channel().CreateInitialSidebarCategories(userId, opts) + require.NoError(t, nErr) + require.NotEmpty(t, res) + // Create the category + created, err := ss.Channel().CreateSidebarCategory(userId, team.Id, &model.SidebarCategoryWithChannels{ + SidebarCategory: model.SidebarCategory{ + DisplayName: model.NewId(), + Sorting: model.SidebarCategorySortManual, + }, + }) + require.NoError(t, err) + + // Confirm that sorting value is correct + res, err = ss.Channel().GetSidebarCategoriesForTeamForUser(userId, team.Id) + require.NoError(t, err) + require.Len(t, res.Categories, 4) + // first category will be favorites and second will be newly created + assert.Equal(t, model.SidebarCategoryCustom, res.Categories[1].Type) + assert.Equal(t, created.Id, res.Categories[1].Id) + assert.Equal(t, model.SidebarCategorySortManual, res.Categories[1].Sorting) + assert.Equal(t, model.SidebarCategorySortManual, created.Sorting) + }) } func testGetSidebarCategory(t *testing.T, ss store.Store, s SqlStore) {