Skip to content

Commit

Permalink
fix: error in list teams
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosrodrigues94 committed Jul 19, 2022
1 parent 9387a58 commit fa7bd90
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions apps/meteor/server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,22 @@ async function getChannelsAndGroups(user, canViewAnon, searchTerm, sort, paginat
};
}

const getChannelsCountForTeam = mem((teamId) => Promise.await(Rooms.findByTeamId(teamId, { projection: { _id: 1 } }).count()), {
maxAge: 2000,
});
const getChannelsCountForTeam = mem(
(teamId) => {
return Rooms.findByTeamId(teamId, { projection: { _id: 1 } });
},
{
maxAge: 2000,
},
);

async function getTeams(user, searchTerm, sort, pagination) {
if (!user) {
return;
}

const userSubs = Subscriptions.cachedFindByUserId(user._id).fetch();

const ids = userSubs.map((sub) => sub.rid);
const { cursor, totalCount } = Rooms.findPaginatedContainingNameOrFNameInIdsAsTeamMain(
searchTerm ? new RegExp(searchTerm, 'i') : null,
Expand Down Expand Up @@ -143,19 +149,23 @@ async function getTeams(user, searchTerm, sort, pagination) {
},
);

const [rooms, total] = await Promise.all([
cursor
.map((room) => ({
...room,
roomsCount: getChannelsCountForTeam(room.teamId),
}))
.toArray(),
totalCount,
]);
const roomsPromises = cursor.map((room) => room).toArray();

const [rooms] = await Promise.all([roomsPromises]);

const teams = [];

for await (const room of rooms) {
const result = await getChannelsCountForTeam(room.teamId).toArray();

teams.push({ ...room, roomsCount: result.length });
}

const [total] = await Promise.all([totalCount]);

return {
total,
results: rooms,
results: teams,
};
}

Expand Down

0 comments on commit fa7bd90

Please sign in to comment.