({ emailOfInvitee, teamId, userId })
| 787 | |
| 788 | // test |
| 789 | public static async createOrUpdateInvitedUserAndAddToTeam({ emailOfInvitee, teamId, userId }) { |
| 790 | if (!emailOfInvitee || !teamId) { |
| 791 | throw new Error('Bad data'); |
| 792 | } |
| 793 | |
| 794 | const { isSubscriptionActiveForAccount, isTrialPeriodOverForAccount } = |
| 795 | await this.getSubscriptionStatus(userId); |
| 796 | |
| 797 | if (isTrialPeriodOverForAccount && !isSubscriptionActiveForAccount) { |
| 798 | throw new Error('This team is not subscribed to a paid plan and free trial period is over.'); |
| 799 | } |
| 800 | |
| 801 | const teamLeader = await this.findOne({ 'teamsForTeamLeader.teamId': teamId }).setOptions({ |
| 802 | lean: true, |
| 803 | }); |
| 804 | |
| 805 | const team = teamLeader.teamsForTeamLeader.find((team) => { |
| 806 | return team.teamId === teamId; |
| 807 | }); |
| 808 | |
| 809 | const existingUser = await this.findOne({ email: emailOfInvitee }).setOptions({ lean: true }); |
| 810 | |
| 811 | let invitedUser; |
| 812 | if (existingUser) { |
| 813 | const teamMember = await this.findOne({ |
| 814 | email: emailOfInvitee, |
| 815 | 'teamsForTeamMember.teamId': teamId, |
| 816 | }).setOptions({ |
| 817 | lean: true, |
| 818 | }); |
| 819 | |
| 820 | const status = |
| 821 | teamMember && teamMember.teamsForTeamMember.find((t) => t.teamId === teamId).status; |
| 822 | |
| 823 | if (teamMember && status === 'removed') { |
| 824 | invitedUser = await this.findOneAndUpdate( |
| 825 | { email: emailOfInvitee, 'teamsForTeamMember.teamId': teamId }, |
| 826 | { |
| 827 | $set: { |
| 828 | 'teamsForTeamMember.$.status': 'invited', |
| 829 | }, |
| 830 | }, |
| 831 | { runValidators: true, new: true }, |
| 832 | ).setOptions({ lean: true }); |
| 833 | } else { |
| 834 | invitedUser = await this.findOneAndUpdate( |
| 835 | { email: emailOfInvitee }, |
| 836 | { |
| 837 | $push: { |
| 838 | teamsForTeamMember: { |
| 839 | teamId, |
| 840 | joinedAt: new Date(), |
| 841 | teamName: team.teamName, |
| 842 | teamLogoUrl: team.teamLogoUrl, |
| 843 | status: 'invited', |
| 844 | trialPeriodStartDate: teamLeader.accountCreationDate, |
| 845 | isSubscriptionActiveForTeam: teamLeader.isSubscriptionActiveForAccount, |
| 846 | isPaymentFailedForTeam: teamLeader.isPaymentFailedForAccount, |
nothing calls this directly
no test coverage detected