Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/meteor/app/apps/server/converters/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
return dept?._id;
}

async convertAppRoom(room, isPartial = false) {

Check warning on line 121 in apps/meteor/app/apps/server/converters/rooms.js

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Async method 'convertAppRoom' has a complexity of 32. Maximum allowed is 31
if (!room) {
return undefined;
}
Expand All @@ -137,12 +137,12 @@

const newRoom = {
...(room.id && { _id: room.id }),
t: room.type,
ts: room.createdAt,
msgs: room.messageCount || 0,
_updatedAt: room.updatedAt,
...(typeof room.type !== 'undefined' && { t: room.type }),
...(typeof room.createdAt !== 'undefined' && { ts: room.createdAt }),
...(typeof room.messageCount !== 'undefined' && { msgs: room.messageCount || 0 }),
...(typeof room.updatedAt !== 'undefined' && { _updatedAt: room.updatedAt }),
...(room.displayName && { fname: room.displayName }),
...(room.type !== 'd' && { name: room.slugifiedName }),
...(room.type !== 'd' && room.slugifiedName && { name: room.slugifiedName }),
...(room.members && { members: room.members }),
...(typeof room.isDefault !== 'undefined' && { default: room.isDefault }),
...(typeof room.isReadOnly !== 'undefined' && { ro: room.isReadOnly }),
Expand Down
13 changes: 13 additions & 0 deletions apps/meteor/tests/unit/app/apps/server/mocks/models/Rooms.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ export class RoomsMock extends BaseModelMock {
updatedAt: new Date('2019-04-10T17:44:34.931Z'),
},

GENERALPartialWithOptionalProps: {
id: 'GENERAL',
slugifiedName: 'general',
displaySystemMessages: true,
updatedAt: new Date('2019-04-10T17:44:34.931Z'),
messageCount: 40,
type: 'c',
},

UpdatedRoom: {
customFields: { custom: 'field' },
},

LivechatRoom: {
id: 'LivechatRoom',
slugifiedName: undefined,
Expand Down
57 changes: 56 additions & 1 deletion apps/meteor/tests/unit/app/apps/server/rooms.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,66 @@ describe('The AppMessagesConverter instance', () => {
expect(rocketchatRoom).to.have.property('_id', appRoom.id);
expect(rocketchatRoom).to.have.property('name', appRoom.slugifiedName);
expect(rocketchatRoom).to.have.property('sysMes', appRoom.displaySystemMessages);
expect(rocketchatRoom).to.have.property('msgs', 0);
expect(rocketchatRoom).to.have.property('_updatedAt', appRoom.updatedAt);

expect(rocketchatRoom).to.not.have.property('msgs');
expect(rocketchatRoom).to.not.have.property('ro');
expect(rocketchatRoom).to.not.have.property('default');
expect(rocketchatRoom).to.not.have.property('t');
});

it('should return a proper schema when receiving a partial object', async () => {
const appRoom = RoomsMock.convertedData.GENERALPartialWithOptionalProps as unknown as IAppsRoom;
const rocketchatRoom = await roomConverter.convertAppRoom(appRoom, true);

expect(rocketchatRoom).to.have.property('_id', appRoom.id);
expect(rocketchatRoom).to.have.property('name', appRoom.slugifiedName);
expect(rocketchatRoom).to.have.property('sysMes', appRoom.displaySystemMessages);
expect(rocketchatRoom).to.have.property('_updatedAt', appRoom.updatedAt);
expect(rocketchatRoom).to.have.property('msgs', appRoom.messageCount);
expect(rocketchatRoom).to.have.property('t', 'c');

expect(rocketchatRoom).to.not.have.property('ro');
expect(rocketchatRoom).to.not.have.property('default');
});

it('should not include properties that are not present in the app room', async () => {
const appRoom = RoomsMock.convertedData.UpdatedRoom as unknown as IAppsRoom;
const rocketchatRoom = await roomConverter.convertAppRoom(appRoom, true);

expect(rocketchatRoom).to.have.property('customFields');
expect(rocketchatRoom).to.not.have.property('_id');
expect(rocketchatRoom).to.not.have.property('t');
});

it('should not include name as undefined if the room doesnt have a name property', async () => {
const appRoom = RoomsMock.convertedData.UpdatedRoom as unknown as IAppsRoom;
const rocketchatRoom = await roomConverter.convertAppRoom(appRoom, true);

expect(rocketchatRoom.name).to.be.undefined;
});

it('should include a name if the source room has slugifiedName property', async () => {
const appRoom = RoomsMock.convertedData.GENERALPartialWithOptionalProps as unknown as IAppsRoom;
const rocketchatRoom = await roomConverter.convertAppRoom(appRoom, true);

expect(rocketchatRoom.name).to.equal(appRoom.slugifiedName);
});

it('should not use _unmappedProperties when the room is a partial object', async () => {
const appRoom = RoomsMock.convertedData.GENERALPartialWithOptionalProps as unknown as IAppsRoom;
// @ts-expect-error - _unmappedProperties
const rocketchatRoom = await roomConverter.convertAppRoom({ ...appRoom, _unmappedProperties_: { unmapped: 'property' } }, true);

expect(rocketchatRoom).to.not.have.property('unmapped');
});

it('should use _unmappedProperties when the room is a partial object', async () => {
const appRoom = RoomsMock.convertedData.GENERALPartialWithOptionalProps as unknown as IAppsRoom;
// @ts-expect-error - _unmappedProperties
const rocketchatRoom = await roomConverter.convertAppRoom({ ...appRoom, _unmappedProperties_: { unmapped: 'property' } }, false);

expect(rocketchatRoom).to.have.property('unmapped', 'property');
});
});
});
Loading