Skip to content

Commit

Permalink
fix: default field is not returned from the setDefault endpoint w…
Browse files Browse the repository at this point in the history
…hen setting to false (#30194)
  • Loading branch information
matheusbsilva137 committed Sep 1, 2023
1 parent 2033047 commit ba24f3c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .changeset/real-pets-visit.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 6 additions & 12 deletions apps/meteor/server/models/raw/Rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
t: {
$in: types,
},
...(defaultValue ? { default: true } : { default: { $exists: false } }),
...(defaultValue ? { default: true } : { default: { $ne: true } }),
};

return this.find(query, options);
Expand Down Expand Up @@ -1763,17 +1763,11 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
saveDefaultById(_id: IRoom['_id'], defaultValue: boolean): Promise<UpdateResult> {
const query: Filter<IRoom> = { _id };

const update: UpdateFilter<IRoom> = defaultValue
? {
$set: {
default: true,
},
}
: {
$unset: {
default: 1,
},
};
const update: UpdateFilter<IRoom> = {
$set: {
default: defaultValue,
},
};

return this.updateOne(query, update);
}
Expand Down
60 changes: 42 additions & 18 deletions apps/meteor/tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-typings/src/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ba24f3c

Please sign in to comment.