({ teamId, userId, teamName, teamLogoUrl })
| 660 | // only Team Leader can use it |
| 661 | // test |
| 662 | public static async createOrUpdateTeam({ teamId, userId, teamName, teamLogoUrl }) { |
| 663 | if (!teamName) { |
| 664 | throw new Error('Bad data'); |
| 665 | } |
| 666 | |
| 667 | if (teamId && teamId !== 'new-team') { |
| 668 | // update Team Leader's teamsForTeamLeader |
| 669 | await this.updateOne( |
| 670 | { 'teamsForTeamLeader.teamId': teamId }, |
| 671 | { |
| 672 | $set: { |
| 673 | 'teamsForTeamLeader.$.teamName': teamName, |
| 674 | 'teamsForTeamLeader.$.teamLogoUrl': teamLogoUrl, |
| 675 | }, |
| 676 | }, |
| 677 | { runValidators: true }, |
| 678 | ); |
| 679 | |
| 680 | // update all Team Members's teamsForTeamMember |
| 681 | await this.updateMany( |
| 682 | { 'teamsForTeamMember.teamId': teamId }, |
| 683 | { |
| 684 | $set: { |
| 685 | 'teamsForTeamMember.$.teamName': teamName, |
| 686 | 'teamsForTeamMember.$.teamLogoUrl': teamLogoUrl, |
| 687 | }, |
| 688 | }, |
| 689 | { runValidators: true }, |
| 690 | ); |
| 691 | |
| 692 | return; |
| 693 | } else { |
| 694 | const generatedTeamId = |
| 695 | Math.random().toString(36).substring(2, 10) + |
| 696 | Math.random().toString(36).substring(2, 10) + |
| 697 | Math.random().toString(36).substring(2, 10) + |
| 698 | Math.random().toString(36).substring(2, 10); |
| 699 | |
| 700 | const teamLeader = await this.findById(userId).setOptions({ |
| 701 | lean: true, |
| 702 | }); |
| 703 | |
| 704 | const updateTeamLeader = await this.findOneAndUpdate( |
| 705 | { _id: userId }, |
| 706 | { |
| 707 | $push: { |
| 708 | teamsForTeamLeader: { |
| 709 | teamId: generatedTeamId, |
| 710 | createdAt: new Date(), |
| 711 | teamName, |
| 712 | teamLogoUrl, |
| 713 | status: 'team-leader', |
| 714 | idsOfTeamMembers: [], |
| 715 | customerId: '', |
| 716 | subscriptionId: '', |
| 717 | invoices: [], |
| 718 | initialMembers: [], |
| 719 | teamLeaderEmail: teamLeader.email, |
nothing calls this directly
no outgoing calls
no test coverage detected