public async Task <Guid?> Handle(RegisterDemandCommand request, CancellationToken cancellationToken)
        {
            var result = await _apiClient.PostWithResponseCode <PostEmployerCourseDemand>(new PostCreateCourseDemandRequest(new CreateCourseDemandData
            {
                Id = request.Id,
                ContactEmailAddress = request.ContactEmailAddress,
                OrganisationName = request.OrganisationName,
                NumberOfApprentices = request.NumberOfApprentices,
                Location = new Location
                {
                    Name = request.LocationName,
                    LocationPoint = new LocationPoint
                    {
                        GeoPoint = new List <double> {
                            request.Lat, request.Lon
                        }
                    }
                },
                Course = new Course
                {
                    Id = request.CourseId,
                    Title = request.CourseTitle,
                    Level = request.CourseLevel,
                    Route = request.CourseRoute,
                },
                StopSharingUrl = request.StopSharingUrl,
                StartSharingUrl = request.StartSharingUrl,
                ExpiredCourseDemandId = request.ExpiredCourseDemandId,
                EntryPoint = request.EntryPoint
            }));

            if (result.StatusCode == HttpStatusCode.Created)
            {
                var emailModel = new VerifyEmployerDemandEmail(
                    request.ContactEmailAddress,
                    request.OrganisationName,
                    request.CourseTitle,
                    request.CourseLevel,
                    request.ConfirmationLink);


                await _notificationService.Send(new SendEmailCommand(emailModel.TemplateId, emailModel.RecipientAddress, emailModel.Tokens));
            }

            if (result.StatusCode == HttpStatusCode.Conflict)
            {
                return(null);
            }

            if (!((int)result.StatusCode >= 200 && (int)result.StatusCode <= 299))
            {
                throw new HttpRequestContentException($"Response status code does not indicate success: {(int)result.StatusCode} ({result.StatusCode})", result.StatusCode, result.ErrorContent);
            }

            return(result.Body.Id);
        }
        public async Task Then_The_Api_Is_Called_And_Email_Sent_If_ResponseCode_Is_Created(
            RegisterDemandCommand command,
            PostEmployerCourseDemand responseBody,
            [Frozen] Mock <IEmployerDemandApiClient <EmployerDemandApiConfiguration> > apiClient,
            [Frozen] Mock <INotificationService> mockNotificationService,
            RegisterDemandCommandHandler handler)
        {
            //Arrange
            var apiResponse = new ApiResponse <PostEmployerCourseDemand>(responseBody, HttpStatusCode.Created, "");

            apiClient.Setup(x => x.PostWithResponseCode <PostEmployerCourseDemand>(It.Is <PostCreateCourseDemandRequest>(c =>

                                                                                                                         ((CreateCourseDemandData)c.Data).Id.Equals(command.Id) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).ContactEmailAddress.Equals(command.ContactEmailAddress) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).OrganisationName.Equals(command.OrganisationName) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).NumberOfApprentices.Equals(command.NumberOfApprentices) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Location.LocationPoint.GeoPoint.First() == command.Lat &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Location.LocationPoint.GeoPoint.Last() == command.Lon &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Location.Name.Equals(command.LocationName) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Course.Title.Equals(command.CourseTitle) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Course.Level.Equals(command.CourseLevel) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Course.Id.Equals(command.CourseId) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).Course.Route.Equals(command.CourseRoute) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).StopSharingUrl.Equals(command.StopSharingUrl) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).StartSharingUrl.Equals(command.StartSharingUrl) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).ExpiredCourseDemandId.Equals(command.ExpiredCourseDemandId) &&
                                                                                                                         ((CreateCourseDemandData)c.Data).EntryPoint.Equals(command.EntryPoint)
                                                                                                                         )))
            .ReturnsAsync(apiResponse);

            SendEmailCommand actualEmail = null;

            mockNotificationService
            .Setup(service => service.Send(It.IsAny <SendEmailCommand>()))
            .Callback((SendEmailCommand args) => actualEmail = args)
            .Returns(Task.CompletedTask);
            var expectedEmail = new VerifyEmployerDemandEmail(
                command.ContactEmailAddress,
                command.OrganisationName,
                command.CourseTitle,
                command.CourseLevel,
                command.ConfirmationLink);

            //Act
            var actual = await handler.Handle(command, CancellationToken.None);

            //Assert
            actual.Should().Be(apiResponse.Body.Id);
            actualEmail.Tokens.Should().BeEquivalentTo(expectedEmail.Tokens);
            actualEmail.RecipientsAddress.Should().BeEquivalentTo(expectedEmail.RecipientAddress);
            actualEmail.TemplateId.Should().BeEquivalentTo(expectedEmail.TemplateId);
        }
Exemple #3
0
        public void Then_The_Values_Are_Set_Correctly(string recipientEmail, string employerName, string standardName, int standardLevel, string confirmationLink)
        {
            var expectedTokens = new Dictionary <string, string>
            {
                { "AEDEmployerName", employerName },
                { "AEDApprenticeshipTrainingCourse", $"{standardName} (level {standardLevel})" },
                { "AEDConfirmEmailURL", confirmationLink }
            };

            var actual = new VerifyEmployerDemandEmail(recipientEmail, employerName, standardName, standardLevel, confirmationLink);

            actual.TemplateId.Should().Be(EmailConstants.VerifyContactEmailEmployerDemandTemplateId);
            actual.Tokens.Should().BeEquivalentTo(expectedTokens);
            actual.RecipientAddress.Should().Be(recipientEmail);
        }