From ba24f3c21fea75d91e983397e8dbba16f6f79ec8 Mon Sep 17 00:00:00 2001 From: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:13:01 -0300 Subject: [PATCH] fix: `default` field is not returned from the `setDefault` endpoint when setting to false (#30194) --- .changeset/real-pets-visit.md | 6 ++ apps/meteor/server/models/raw/Rooms.ts | 18 ++---- .../tests/end-to-end/api/02-channels.js | 60 +++++++++++++------ packages/core-typings/src/IRoom.ts | 2 +- 4 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 .changeset/real-pets-visit.md diff --git a/.changeset/real-pets-visit.md b/.changeset/real-pets-visit.md new file mode 100644 index 000000000000..d6531285597c --- /dev/null +++ b/.changeset/real-pets-visit.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": patch +"@rocket.chat/core-typings": patch +--- + +Fixed `default` field not being returned from the `setDefault` endpoints when setting to false diff --git a/apps/meteor/server/models/raw/Rooms.ts b/apps/meteor/server/models/raw/Rooms.ts index 9dad0fa60da3..673fd83f7800 100644 --- a/apps/meteor/server/models/raw/Rooms.ts +++ b/apps/meteor/server/models/raw/Rooms.ts @@ -1349,7 +1349,7 @@ export class RoomsRaw extends BaseRaw implements IRoomsModel { t: { $in: types, }, - ...(defaultValue ? { default: true } : { default: { $exists: false } }), + ...(defaultValue ? { default: true } : { default: { $ne: true } }), }; return this.find(query, options); @@ -1763,17 +1763,11 @@ export class RoomsRaw extends BaseRaw implements IRoomsModel { saveDefaultById(_id: IRoom['_id'], defaultValue: boolean): Promise { const query: Filter = { _id }; - const update: UpdateFilter = defaultValue - ? { - $set: { - default: true, - }, - } - : { - $unset: { - default: 1, - }, - }; + const update: UpdateFilter = { + $set: { + default: defaultValue, + }, + }; return this.updateOne(query, update); } diff --git a/apps/meteor/tests/end-to-end/api/02-channels.js b/apps/meteor/tests/end-to-end/api/02-channels.js index e1615dc34997..7cb2e2234e4e 100644 --- a/apps/meteor/tests/end-to-end/api/02-channels.js +++ b/apps/meteor/tests/end-to-end/api/02-channels.js @@ -1573,25 +1573,49 @@ describe('[Channels]', function () { }); }); - it('/channels.setDefault', async () => { - const roomInfo = await getRoomInfo(channel._id); + describe('/channels.setDefault', () => { + it('should set channel as default', async () => { + const roomInfo = await getRoomInfo(channel._id); - return request - .post(api('channels.setDefault')) - .set(credentials) - .send({ - roomId: channel._id, - default: true, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); - }); + return request + .post(api('channels.setDefault')) + .set(credentials) + .send({ + roomId: channel._id, + default: true, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); + expect(res.body).to.have.nested.property('channel.default', true); + }); + }); + it('should unset channel as default', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.setDefault')) + .set(credentials) + .send({ + roomId: channel._id, + default: false, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); + expect(res.body).to.have.nested.property('channel.default', false); + }); + }); }); it('/channels.leave', async () => { diff --git a/packages/core-typings/src/IRoom.ts b/packages/core-typings/src/IRoom.ts index 618c24a4fd3a..4ce8b84abb0e 100644 --- a/packages/core-typings/src/IRoom.ts +++ b/packages/core-typings/src/IRoom.ts @@ -23,7 +23,7 @@ export interface IRoom extends IRocketChatRecord { name?: string; fname?: string; msgs: number; - default?: true; + default?: boolean; broadcast?: true; featured?: true; announcement?: string;