Example #1
0
        public static async Task SetEmailTemplate(
            MatchingDbContext dbContext,
            EmailTemplate emailTemplate)
        {
            await dbContext.AddAsync(emailTemplate);

            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();
        }
Example #2
0
        public static async Task SetTestData(MatchingDbContext dbContext,
                                             Provider provider,
                                             ProviderVenue venue,
                                             Opportunity opportunity,
                                             BackgroundProcessHistory backgroundProcessHistory,
                                             bool isSaved = true, bool isSelectedForReferral = true)
        {
            backgroundProcessHistory.Status = BackgroundProcessHistoryStatus.Pending.ToString();

            await dbContext.AddAsync(provider);

            await dbContext.AddAsync(venue);

            await dbContext.AddAsync(opportunity);

            await dbContext.AddAsync(backgroundProcessHistory);

            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();

            var items = dbContext.OpportunityItem
                        .AsNoTracking()
                        .Where(oi => oi.OpportunityId == opportunity.Id)
                        .ToList();

            foreach (var opportunityItem in items)
            {
                opportunityItem.IsSaved               = isSaved;
                opportunityItem.IsCompleted           = false;
                opportunityItem.OpportunityType       = "Referral";
                opportunityItem.IsSelectedForReferral = isSelectedForReferral;

                dbContext.Entry(opportunityItem).Property("IsSaved").IsModified               = true;
                dbContext.Entry(opportunityItem).Property("IsCompleted").IsModified           = true;
                dbContext.Entry(opportunityItem).Property("IsSelectedForReferral").IsModified = true;
                dbContext.Entry(opportunityItem).Property("OpportunityType").IsModified       = true;
            }

            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();
        }
        private static async Task SetProvisionGapData(
            MatchingDbContext dbContext,
            OpportunityItem opportunityItem,
            bool hasBadExperience,
            bool hasNoSuitableStudent,
            bool areProvidersTooFarAway)
        {
            opportunityItem.OpportunityType = "ProvisionGap";
            opportunityItem.IsSaved         = true;
            opportunityItem.IsCompleted     = false;

            //Need to remove extra referral/provision gap rows created as AutoDomainData
            foreach (var x in opportunityItem.ProvisionGap.Where(pg => pg.Id != opportunityItem.ProvisionGap.First().Id).ToList())
            {
                opportunityItem.ProvisionGap.Remove(x);
            }
            opportunityItem.Referral.Clear();

            await dbContext.OpportunityItem.AddAsync(opportunityItem);

            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();

            //Set up the provision gap record
            var provisionGap = await dbContext.ProvisionGap.AsNoTracking().FirstOrDefaultAsync();

            provisionGap.HadBadExperience    = hasBadExperience;
            provisionGap.NoSuitableStudent   = hasNoSuitableStudent;
            provisionGap.ProvidersTooFarAway = areProvidersTooFarAway;

            dbContext.Entry(provisionGap).Property("HadBadExperience").IsModified    = true;
            dbContext.Entry(provisionGap).Property("NoSuitableStudent").IsModified   = true;
            dbContext.Entry(provisionGap).Property("ProvidersTooFarAway").IsModified = true;

            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();
        }
Example #4
0
        public static async Task SetEmailHistory(
            MatchingDbContext dbContext,
            EmailHistory emailHistory)
        {
            emailHistory.Status     = null;
            emailHistory.ModifiedBy = null;
            emailHistory.ModifiedOn = null;

            await dbContext.AddAsync(emailHistory);

            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();
        }
Example #5
0
        public async Task Then_Update_Email_History_With_Status_And_Push_To_Email_Delivery_Status_Queue(
            string status,
            MatchingDbContext dbContext,
            MatchingConfiguration configuration,
            [Frozen] Domain.Models.Opportunity opportunity,
            [Frozen] Domain.Models.Provider provider,
            [Frozen] Domain.Models.ProviderVenue venue,
            [Frozen] EmailHistory emailHistory,
            [Frozen] BackgroundProcessHistory backgroundProcessHistory,
            ILogger <OpportunityRepository> opportunityRepoLogger,
            IMessageQueueService messageQueueService,
            EmailDeliveryStatusPayLoad payload,
            ILogger <GenericRepository <EmailTemplate> > emailTemplateLogger,
            ILogger <GenericRepository <EmailHistory> > emailHistoryLogger,
            ILogger <GenericRepository <FunctionLog> > functionLogLogger,
            ILogger <Application.Services.EmailDeliveryStatusService> emailDeliveryServiceStatusLogger,
            ILogger <EmailService> emailServiceLogger,
            IAsyncNotificationClient notificationClient
            )
        {
            //Arrange
            await DataBuilder.SetTestData(dbContext, provider, venue, opportunity, backgroundProcessHistory);

            dbContext.Add(emailHistory);
            await dbContext.SaveChangesAsync();

            dbContext.DetachAllEntities();

            payload.Status = status;
            payload.Id     = emailHistory.NotificationId.GetValueOrDefault();

            var sut = SutSetUp(dbContext, opportunityRepoLogger, emailTemplateLogger, emailHistoryLogger, functionLogLogger,
                               emailDeliveryServiceStatusLogger, emailServiceLogger, notificationClient, configuration, messageQueueService);

            var serializedPayLoad = JsonConvert.SerializeObject(payload);

            //Act
            await sut.HandleEmailDeliveryStatusAsync(serializedPayLoad);

            //Assert
            var data = dbContext.EmailHistory.FirstOrDefault(em => em.NotificationId == payload.Id);

            data.Should().NotBeNull();
            data?.NotificationId.Should().Be(payload.Id);
            data?.Status.Should().Be(payload.Status);
            data?.ModifiedBy.Should().Be("System");

            await messageQueueService.Received(1).PushEmailDeliveryStatusMessageAsync(Arg.Any <SendEmailDeliveryStatus>());
        }
Example #6
0
        public async Task Then_Get_Back_Link(
            MatchingDbContext dbContext,
            HttpContext httpContext,
            HttpContextAccessor httpContextAccessor,
            ILogger <GenericRepository <UserCache> > logger
            )
        {
            httpContextAccessor.HttpContext = httpContext;

            var config = new MapperConfiguration(c =>
            {
                c.AddMaps(typeof(UserCacheMapper).Assembly);
                c.ConstructServicesUsing(type =>
                                         type.Name.Contains("LoggedInUserNameResolver") ?
                                         (object)new LoggedInUserNameResolver <UserCacheDto, UserCache>(httpContextAccessor) :
                                         type.Name.Contains("UtcNowResolver") ?
                                         new UtcNowResolver <UserCacheDto, UserCache>(new DateTimeProvider()) :
                                         null);
            });

            //Arrange
            var mapper   = new Mapper(config);
            var repo     = new GenericRepository <UserCache>(logger, dbContext);
            var username = httpContextAccessor.HttpContext.User.GetUserName();

            var sut = new Application.Services.NavigationService(mapper, repo);

            //Act
            await AddTestUrls(sut, dbContext, username, new List <string> {
                "/Start", "/find-providers", "/test-url"
            });

            //Assert
            var prevUrl = await sut.GetBackLinkAsync(username);

            prevUrl.Should().Be("/find-providers");

            dbContext.DetachAllEntities();

            prevUrl = await sut.GetBackLinkAsync(username);

            prevUrl.Should().Be("/Start");
        }
        public async Task Then_Send_Email_And_Save_Email_History_And_Update_Email_History_If_Status_Is_Null(
            string status,
            MatchingConfiguration configuration,
            [Frozen] MatchingDbContext dbContext,
            IAsyncNotificationClient notificationClient,
            ILogger <GenericRepository <EmailTemplate> > emailTemplateLogger,
            ILogger <GenericRepository <EmailHistory> > emailHistoryLogger,
            ILogger <GenericRepository <FunctionLog> > functionLogLogger,
            ILogger <EmailService> emailServiceLogger,
            [Frozen] Domain.Models.Opportunity opportunity,
            [Frozen] OpportunityItem opportunityItem,
            [Frozen] Domain.Models.Provider provider,
            [Frozen] Domain.Models.ProviderVenue venue,
            [Frozen] BackgroundProcessHistory backgroundProcessHistory,
            [Frozen] EmailHistory emailHistory,
            [Frozen] EmailTemplate emailTemplate,
            [Frozen] EmailNotificationResponse emailNotificationResponse,
            EmailDeliveryStatusPayLoad payLoad
            )
        {
            //Arrange
            Guid.TryParse(emailNotificationResponse.id, out var notificationId);
            payLoad.Status = status;
            payLoad.Id     = notificationId;

            var(templateRepository, emailHistoryRepository, functionLogRepository, mapper)
                = SetUp(dbContext, emailTemplateLogger, emailHistoryLogger, functionLogLogger);

            var sut = new EmailService(configuration, notificationClient, templateRepository, emailHistoryRepository,
                                       functionLogRepository, mapper, emailServiceLogger);

            var tokens = new Dictionary <string, string>
            {
                { "contactname", "name" }
            };

            notificationClient.SendEmailAsync(Arg.Any <string>(), Arg.Any <string>(),
                                              Arg.Any <Dictionary <string, dynamic> >()).Returns(Task.FromResult(emailNotificationResponse));

            await DataBuilder.SetTestData(dbContext, provider, venue, opportunity, backgroundProcessHistory);

            await DataBuilder.SetEmailTemplate(dbContext, emailTemplate);

            //Act
            await sut.SendEmailAsync(emailTemplate.TemplateName, "*****@*****.**", opportunity.Id, opportunityItem.Id, tokens, "System");

            //Assert
            var data = dbContext.EmailHistory.AsNoTracking().FirstOrDefault(x => x.NotificationId == notificationId);

            data.Should().NotBeNull();
            data?.EmailTemplateId.Should().Be(emailHistory.EmailTemplateId);
            data?.NotificationId.Should().Be(emailNotificationResponse.id);
            data?.CreatedBy.Should().Be("System");
            data?.Status.Should().BeNullOrEmpty();
            data?.ModifiedBy.Should().BeNullOrEmpty();
            data?.ModifiedOn.Should().BeNull();

            dbContext.DetachAllEntities();

            //Act - Update Email With Status
            await sut.UpdateEmailStatus(payLoad);

            data = dbContext.EmailHistory.AsNoTracking().FirstOrDefault(x => x.NotificationId == notificationId);

            data.Should().NotBeNull();
            data?.EmailTemplateId.Should().Be(emailHistory.EmailTemplateId);
            data?.Status.Should().NotBeNullOrEmpty();
            data?.Status.Should().Be(payLoad.Status);
            data?.NotificationId.Should().Be(emailNotificationResponse.id);
            data?.CreatedBy.Should().Be("System");
            data?.ModifiedBy.Should().Be("System");
        }