private static IEnumerable <string> GetMissingMemberProfileProperties(CreateMemberProfile profile, IEnumerable <ChapterProperty> chapterProperties, IEnumerable <UpdateMemberProperty> memberProperties) { if (string.IsNullOrWhiteSpace(profile.EmailAddress)) { yield return("Email address"); } foreach (string property in GetMissingMemberProfileProperties(profile as UpdateMemberProfile, chapterProperties, memberProperties)) { yield return(property); } }
public async Task CreateMember(Guid chapterId, CreateMemberProfile profile) { await ValidateMemberProfile(chapterId, profile); ValidateMemberImage(profile.Image.MimeType, profile.Image.ImageData); Member existing = await _memberRepository.FindMemberByEmailAddress(profile.EmailAddress); if (existing != null) { return; } Member create = new Member(Guid.Empty, chapterId, profile.EmailAddress, profile.EmailOptIn, profile.FirstName, profile.LastName, DateTime.UtcNow, false, false, 0); Guid id = await _memberRepository.CreateMember(create); ChapterMembershipSettings membershipSettings = await _chapterRepository.GetChapterMembershipSettings(chapterId); MemberSubscription subscription = new MemberSubscription(id, SubscriptionType.Trial, create.CreatedDate.AddMonths(membershipSettings.TrialPeriodMonths)); await _memberRepository.UpdateMemberSubscription(subscription); MemberImage image = CreateMemberImage(id, profile.Image.MimeType, profile.Image.ImageData); await _memberRepository.AddMemberImage(image); IEnumerable <MemberProperty> memberProperties = profile.Properties .Select(x => new MemberProperty(Guid.Empty, id, x.ChapterPropertyId, x.Value)); await _memberRepository.UpdateMemberProperties(id, memberProperties); string activationToken = RandomStringGenerator.Generate(64); await _memberRepository.AddActivationToken(new MemberActivationToken(id, activationToken)); Chapter chapter = await _chapterRepository.GetChapter(chapterId); string url = _settings.ActivateAccountUrl.Interpolate(new Dictionary <string, string> { { "chapter.name", chapter.Name }, { "token", HttpUtility.UrlEncode(activationToken) } }); await _emailService.SendEmail(chapter, create.EmailAddress, EmailType.ActivateAccount, new Dictionary <string, string> { { "chapter.name", chapter.Name }, { "url", url } }); }
private async Task ValidateMemberProfile(Guid chapterId, CreateMemberProfile profile) { IReadOnlyCollection <ChapterProperty> chapterProperties = await _chapterRepository.GetChapterProperties(chapterId); IReadOnlyCollection <string> missingProperties = GetMissingMemberProfileProperties(profile, chapterProperties, profile.Properties).ToArray(); if (missingProperties.Count > 0) { throw new OdkServiceException($"The following properties are required: {string.Join(", ", missingProperties)}"); } if (!MailUtils.ValidEmailAddress(profile.EmailAddress)) { throw new OdkServiceException("Invalid email address format"); } }