diff --git a/test/api/v3/integration/groups/PUT-groups.test.js b/test/api/v3/integration/groups/PUT-groups.test.js index 36bdfdd7d4..77d4ad6487 100644 --- a/test/api/v3/integration/groups/PUT-groups.test.js +++ b/test/api/v3/integration/groups/PUT-groups.test.js @@ -11,6 +11,12 @@ describe('PUT /group', () => { const groupName = 'Test Public Guild'; const groupType = 'guild'; const groupUpdatedName = 'Test Public Guild Updated'; + const groupCategories = [ + { + slug: 'initialCat', + name: 'Initial Category', + }, + ]; beforeEach(async () => { const { group, groupLeader, members } = await createAndPopulateGroup({ @@ -18,6 +24,7 @@ describe('PUT /group', () => { name: groupName, type: groupType, privacy: 'public', + categories: groupCategories, }, members: 1, }); @@ -61,6 +68,35 @@ describe('PUT /group', () => { expect(updatedGroup.categories[0].name).to.eql(categories[0].name); }); + it('removes the initial group category', async () => { + const categories = []; + + const updatedGroup = await leader.put(`/groups/${groupToUpdate._id}`, { + categories, + }); + + expect(updatedGroup.categories.length).to.equal(0); + }); + + it('removes duplicate group categories', async () => { + const categories = [ + { + slug: 'newCat', + name: 'New Category', + }, + { + slug: 'newCat', + name: 'New Category', + }, + ]; + + const updatedGroup = await leader.put(`/groups/${groupToUpdate._id}`, { + categories, + }); + + expect(updatedGroup.categories.length).to.equal(1); + }); + it('allows an admin to update a guild', async () => { const updatedGroup = await adminUser.put(`/groups/${groupToUpdate._id}`, { name: groupUpdatedName, diff --git a/website/server/controllers/api-v3/groups.js b/website/server/controllers/api-v3/groups.js index cfa582ecd9..c16ba61c7e 100644 --- a/website/server/controllers/api-v3/groups.js +++ b/website/server/controllers/api-v3/groups.js @@ -492,7 +492,17 @@ api.updateGroup = { if (req.body.leader !== user._id && group.hasNotCancelled()) throw new NotAuthorized(res.t('cannotChangeLeaderWithActiveGroupPlan')); - _.assign(group, _.merge(group.toObject(), Group.sanitizeUpdate(req.body))); + const handleArrays = (currentValue, updatedValue) => { + if (!_.isArray(currentValue)) { + return undefined; + } + + // Previously, categories could get duplicated. By making the updated category list unique, + // the duplication issue is fixed on every group edit + return _.uniqBy(updatedValue, 'slug'); + }; + + _.assign(group, _.mergeWith(group.toObject(), Group.sanitizeUpdate(req.body), handleArrays)); const savedGroup = await group.save(); const response = await Group.toJSONCleanChat(savedGroup, user);