/// <summary> /// Constructs a TermsOfService model from a LegalAgreement database model. /// </summary> /// <param name="model">The legal agreement database model.</param> /// <returns>The terms of service model.</returns> public static TermsOfServiceModel CreateFromDbModel(LegalAgreement model) { return(new TermsOfServiceModel() { Id = model?.Id, EffectiveDate = model?.EffectiveDate !.Value, Content = model?.LegalText, });
private void ProcessLegalAgreement(LegalAgreement agreement, LegalAgreementConfig config) { this.logger.LogInformation($"{config.Name} found, last updated {agreement.EffectiveDate}"); this.logger.LogInformation($"Fetching {config.LastCheckedKey} from application settings"); ApplicationSetting?lastCheckedSetting = this.applicationSettingsDelegate.GetApplicationSetting(ApplicationType.JobScheduler, this.GetType().Name, config.LastCheckedKey); if (lastCheckedSetting != null) { this.logger.LogInformation($"Found {config.LastCheckedKey} with value of {lastCheckedSetting.Value}"); } else { lastCheckedSetting = new ApplicationSetting() { Application = ApplicationType.JobScheduler, Component = this.GetType().Name, Key = config.LastCheckedKey, Value = DateTime.MinValue.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture), }; this.dbContext.ApplicationSetting.Add(lastCheckedSetting); } DateTime lastChecked = System.DateTime.Parse(lastCheckedSetting.Value !, CultureInfo.InvariantCulture); if (agreement.EffectiveDate > lastChecked) { Dictionary <string, string> keyValues = new Dictionary <string, string>(); keyValues.Add("host", this.host); keyValues.Add("path", config.Path); keyValues.Add("effectivedate", agreement.EffectiveDate.Value.ToString("MMMM dd, yyyy", CultureInfo.InvariantCulture)); keyValues.Add("contactemail", config.ContactEmail); int page = 0; DBResult <List <UserProfile> > profileResult; do { profileResult = this.profileDelegate.GetAllUserProfilesAfter(agreement.EffectiveDate.Value, page, this.profilesPageSize); foreach (UserProfile profile in profileResult.Payload) { this.emailService.QueueNewEmail(profile.Email !, config.EmailTemplate, keyValues, false); } this.logger.LogInformation($"Sent {profileResult.Payload.Count} emails"); // TODO: Resume functionality?? this.dbContext.SaveChanges(); // commit after every page page++; }while (profileResult.Payload.Count == this.profilesPageSize); this.logger.LogInformation($"Completed sending emails after processing {page} page(s) with pagesize set to {this.profilesPageSize}"); lastCheckedSetting.Value = DateTime.UtcNow.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); this.logger.LogInformation($"Saving rundate of {lastCheckedSetting.Value} to DB"); this.dbContext.SaveChanges(); } else { this.logger.LogInformation($"{config.Name} has not been updated since last run"); } }
public DBResult <LegalAgreement> GetActiveByAgreementType(LegalAgreementType agreementTypeCode) { LegalAgreement legalAgreement = this.dbContext.LegalAgreement .Where(la => la.EffectiveDate <= DateTime.UtcNow) .Where(la => agreementTypeCode.Equals(la.LegalAgreementCode)) .OrderByDescending(la => la.EffectiveDate) .FirstOrDefault(); return(new DBResult <LegalAgreement>() { Payload = legalAgreement, Status = DBStatusCode.Read, }); }
// TODO: Remove helper methods private Tuple <RequestResult <UserProfileModel>, UserProfileModel> ExecuteGetUserProfile(Database.Constants.DBStatusCode dbResultStatus, DateTime lastLoginDateTime) { UserProfile userProfile = new UserProfile { HdId = hdid, AcceptedTermsOfService = true, LastLoginDateTime = lastLoginDateTime, }; DBResult <UserProfile> userProfileDBResult = new DBResult <UserProfile> { Payload = userProfile, Status = dbResultStatus }; UserProfileModel expected = UserProfileModel.CreateFromDbModel(userProfile); expected.HasTermsOfServiceUpdated = true; LegalAgreement termsOfService = new LegalAgreement() { Id = Guid.NewGuid(), LegalText = "", EffectiveDate = DateTime.Now }; Mock <IEmailQueueService> emailer = new Mock <IEmailQueueService>(); Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(userProfileDBResult); profileDelegateMock.Setup(s => s.Update(userProfile, true)).Returns(userProfileDBResult); UserPreference dbUserPreference = new UserPreference { HdId = hdid, Preference = "TutorialPopover", Value = true.ToString(), }; List <UserPreference> userPreferences = new List <UserPreference>(); userPreferences.Add(dbUserPreference); DBResult <IEnumerable <UserPreference> > readResult = new DBResult <IEnumerable <UserPreference> > { Payload = userPreferences, Status = DBStatusCode.Read }; Mock <IUserPreferenceDelegate> preferenceDelegateMock = new Mock <IUserPreferenceDelegate>(); preferenceDelegateMock.Setup(s => s.GetUserPreferences(hdid)).Returns(readResult); Mock <IMessagingVerificationDelegate> emailInviteDelegateMock = new Mock <IMessagingVerificationDelegate>(); emailInviteDelegateMock.Setup(s => s.GetLastByInviteKey(It.IsAny <Guid>())).Returns(new MessagingVerification()); Mock <IConfigurationService> configServiceMock = new Mock <IConfigurationService>(); configServiceMock.Setup(s => s.GetConfiguration()).Returns(new ExternalConfiguration()); Mock <ILegalAgreementDelegate> legalAgreementDelegateMock = new Mock <ILegalAgreementDelegate>(); legalAgreementDelegateMock .Setup(s => s.GetActiveByAgreementType(LegalAgreementType.TermsofService)) .Returns(new DBResult <LegalAgreement>() { Payload = termsOfService }); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); Mock <INotificationSettingsService> notificationServiceMock = new Mock <INotificationSettingsService>(); Mock <IMessagingVerificationDelegate> messageVerificationDelegateMock = new Mock <IMessagingVerificationDelegate>(); IUserProfileService service = new UserProfileService( new Mock <ILogger <UserProfileService> >().Object, new Mock <IPatientService>().Object, new Mock <IUserEmailService>().Object, new Mock <IUserSMSService>().Object, configServiceMock.Object, new Mock <IEmailQueueService>().Object, notificationServiceMock.Object, profileDelegateMock.Object, preferenceDelegateMock.Object, legalAgreementDelegateMock.Object, messageVerificationDelegateMock.Object, cryptoDelegateMock.Object, new Mock <IHttpContextAccessor>().Object ); RequestResult <UserProfileModel> actualResult = service.GetUserProfile(hdid, DateTime.Now); return(new Tuple <RequestResult <UserProfileModel>, UserProfileModel>(actualResult, expected)); }
public void ShouldGetUserProfile() { UserProfile userProfile = new UserProfile { HdId = hdid, AcceptedTermsOfService = true, }; DBResult <UserProfile> userProfileDBResult = new DBResult <UserProfile> { Payload = userProfile, Status = DBStatusCode.Read }; UserProfileModel expected = UserProfileModel.CreateFromDbModel(userProfile); LegalAgreement termsOfService = new LegalAgreement() { Id = Guid.NewGuid(), LegalText = "", EffectiveDate = DateTime.Now }; Mock <IEmailQueueService> emailer = new Mock <IEmailQueueService>(); Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(userProfileDBResult); profileDelegateMock.Setup(s => s.Update(userProfile, true)).Returns(userProfileDBResult); UserPreference dbUserPreference = new UserPreference { HdId = hdid, Preference = "TutorialPopover", Value = true.ToString(), }; List <UserPreference> userPreferences = new List <UserPreference>(); userPreferences.Add(dbUserPreference); DBResult <IEnumerable <UserPreference> > readResult = new DBResult <IEnumerable <UserPreference> > { Payload = userPreferences, Status = DBStatusCode.Read }; Mock <IUserPreferenceDelegate> preferenceDelegateMock = new Mock <IUserPreferenceDelegate>(); preferenceDelegateMock.Setup(s => s.GetUserPreferences(hdid)).Returns(readResult); Mock <IEmailDelegate> emailDelegateMock = new Mock <IEmailDelegate>(); Mock <IMessagingVerificationDelegate> emailInviteDelegateMock = new Mock <IMessagingVerificationDelegate>(); emailInviteDelegateMock.Setup(s => s.GetByInviteKey(It.IsAny <Guid>())).Returns(new MessagingVerification()); Mock <IConfigurationService> configServiceMock = new Mock <IConfigurationService>(); configServiceMock.Setup(s => s.GetConfiguration()).Returns(new ExternalConfiguration()); Mock <ILegalAgreementDelegate> legalAgreementDelegateMock = new Mock <ILegalAgreementDelegate>(); legalAgreementDelegateMock .Setup(s => s.GetActiveByAgreementType(AgreementType.TermsofService)) .Returns(new DBResult <LegalAgreement>() { Payload = termsOfService }); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); Mock <INotificationSettingsService> notificationServiceMock = new Mock <INotificationSettingsService>(); Mock <IMessagingVerificationDelegate> messageVerificationDelegateMock = new Mock <IMessagingVerificationDelegate>(); IUserProfileService service = new UserProfileService( new Mock <ILogger <UserProfileService> >().Object, profileDelegateMock.Object, preferenceDelegateMock.Object, emailDelegateMock.Object, emailInviteDelegateMock.Object, configServiceMock.Object, emailer.Object, legalAgreementDelegateMock.Object, cryptoDelegateMock.Object, notificationServiceMock.Object, messageVerificationDelegateMock.Object); RequestResult <UserProfileModel> actualResult = service.GetUserProfile(hdid); Assert.Equal(ResultType.Success, actualResult.ResultStatus); Assert.True(actualResult.ResourcePayload.IsDeepEqual(expected)); }