Skip to content

Commit

Permalink
[NEW] Create Team with a member list of usernames (#25868)
Browse files Browse the repository at this point in the history
<!-- This is a pull request template, you do not need to uncomment or remove the comments, they won't show up in the PR text. -->

<!-- Your Pull Request name should start with one of the following tags
  [NEW] For new features
  [IMPROVE] For an improvement (performance or little improvements) in existing features
  [FIX] For bug fixes that affect the end-user
  [BREAK] For pull requests including breaking changes
  Chore: For small tasks
  Doc: For documentation
-->

<!-- Checklist!!! If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code. 
  - I have read the Contributing Guide - https://github.com/RocketChat/Rocket.Chat/blob/develop/.github/CONTRIBUTING.md#contributing-to-rocketchat doc
  - I have signed the CLA - https://cla-assistant.io/RocketChat/Rocket.Chat
  - Lint and unit tests pass locally with my changes
  - I have added tests that prove my fix is effective or that my feature works (if applicable)
  - I have added necessary documentation (if applicable)
  - Any dependent changes have been merged and published in downstream modules
-->

## Proposed changes (including videos or screenshots)
<!-- CHANGELOG -->
<!--
  Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request.
  If it fixes a bug or resolves a feature request, be sure to link to that issue below.
  This description will appear in the release notes if we accept the contribution.
-->

<!-- END CHANGELOG -->

## Issue(s)
<!-- Link the issues being closed by or related to this PR. For example, you can use #594 if this PR closes issue number 594 -->

## Steps to test or reproduce
<!-- Mention how you would reproduce the bug if not mentioned on the issue page already. Also mention which screens are going to have the changes if applicable -->

## Further comments
<!-- If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc... -->


Co-authored-by: Douglas Fabris <27704687+dougfabris@users.noreply.github.com>
  • Loading branch information
pierre-lehnen-rc and dougfabris committed Jun 17, 2022
1 parent 92e3230 commit 610670c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
9 changes: 9 additions & 0 deletions apps/meteor/app/models/server/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ export class UsersRaw extends BaseRaw {
return this.find(query, options);
}

findActiveByIdsOrUsernames(userIds, options = {}) {
const query = {
$or: [{ _id: { $in: userIds } }, { username: { $in: userIds } }],
active: true,
};

return this.find(query, options);
}

findByIds(userIds, options = {}) {
const query = {
_id: { $in: userIds },
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/server/services/team/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export class TeamService extends ServiceClassInternal implements ITeamService {

// TODO add validations to `data` and `members`

const membersResult = await this.Users.findActiveByIds(members, {
projection: { username: 1, _id: 0 },
const membersResult = await this.Users.findActiveByIdsOrUsernames(members, {
projection: { username: 1, _id: 1 },
}).toArray();
const memberUsernames = membersResult.map(({ username }) => username);
const memberIds = membersResult.map(({ _id }) => _id);

const teamData = {
...team,
Expand All @@ -96,7 +97,7 @@ export class TeamService extends ServiceClassInternal implements ITeamService {

// filter empty strings and falsy values from members list
const membersList: Array<InsertionModel<ITeamMember>> =
members
memberIds
?.filter(Boolean)
.filter((memberId) => !excludeFromMembers.includes(memberId))
.map((memberId) => ({
Expand Down
35 changes: 35 additions & 0 deletions apps/meteor/tests/end-to-end/api/25-teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ describe('[Teams]', () => {
.end(done);
});

it('should create a public team with a member', (done) => {
request
.post(api('teams.create'))
.set(credentials)
.send({
name: `test-team-${Date.now()}`,
type: 0,
members: [testUser.username],
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('team');
expect(res.body).to.have.nested.property('team._id');
publicTeam = res.body.team;
})
.then((response) => {
const teamId = response.body.team._id;
return request
.get(api('teams.members'))
.set(credentials)
.query({ teamId })
.expect(200)
.expect((response) => {
expect(response.body).to.have.property('success', true);
expect(response.body).to.have.property('members');
const member = response.body.members[0];
expect(member.user.username).to.be.equal(testUser.username);
});
})
.then(() => done())
.catch(done);
});

it('should create private team with a defined owner', (done) => {
request
.post(api('teams.create'))
Expand Down

0 comments on commit 610670c

Please sign in to comment.