Example #1
0
        public async Task InviteMemberToGroupAndPlatformAsync(Guid adminUserId, Guid?groupId, string email, CancellationToken cancellationToken)
        {
            if (Guid.Empty == adminUserId)
            {
                throw new ArgumentOutOfRangeException(nameof(adminUserId));
            }

            var userCanPerformAction = await _permissionsService.UserCanPerformActionAsync(adminUserId, AddMembersRole, cancellationToken);

            if (!userCanPerformAction)
            {
                _logger.LogError($"Error: InviteMemberToGroupAndPlatformAsync - User:{0} does not have access to perform admin actions", adminUserId);
                throw new SecurityException($"Error: User does not have access");
            }

            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException($"Email was not provided");
            }

            if (email.Length > 254)
            {
                throw new ArgumentOutOfRangeException($"Email must be less than 254 characters");
            }

            MailAddress emailAddress;

            try
            {
                emailAddress = new MailAddress(email);
            }
            catch (Exception)
            {
                throw new ArgumentOutOfRangeException($"Email is not in a valid format");
            }

            var userInvite = new GroupInviteDto
            {
                EmailAddress = emailAddress.Address.ToLowerInvariant(),
                GroupId      = groupId,
                CreatedAtUTC = _systemClock.UtcNow.UtcDateTime,
            };

            var registrationLink = CreateRegistrationLink();
            var personalisation  = new Dictionary <string, dynamic>
            {
                { "registration_link", registrationLink }
            };

            await _userCommand.CreateInviteUserAsync(userInvite, cancellationToken);

            await _emailService.SendEmailAsync(emailAddress, _registrationEmailId, personalisation);
        }