Пример #1
0
        private bool ValidateDependent(AddDependentRequest dependent, PatientModel patientModel)
        {
            if (patientModel is null)
            {
                return(false);
            }

            if (!patientModel.LastName.Equals(dependent.LastName, StringComparison.OrdinalIgnoreCase))
            {
                this.logger.LogInformation("Validate Dependent: LastName mismatch.");
                return(false);
            }

            if (!patientModel.FirstName.Equals(dependent.FirstName, StringComparison.OrdinalIgnoreCase))
            {
                this.logger.LogInformation("Validate Dependent: FirstName mismatch.");
                return(false);
            }

            if (patientModel.Birthdate.Year != dependent.DateOfBirth.Year ||
                patientModel.Birthdate.Month != dependent.DateOfBirth.Month ||
                patientModel.Birthdate.Day != dependent.DateOfBirth.Day)
            {
                this.logger.LogInformation("Validate Dependent: DateOfBirth mismatch.");
                return(false);
            }

            return(true);
        }
        public void ValidateDependent()
        {
            AddDependentRequest            addDependentRequest = SetupMockInput();
            IDependentService              service             = SetupMockDependentService(addDependentRequest);
            RequestResult <DependentModel> actualResult        = service.AddDependent(mockParentHdId, addDependentRequest);

            Assert.Equal(Common.Constants.ResultType.Success, actualResult.ResultStatus);
        }
Пример #3
0
        public IActionResult AddDependent([FromBody] AddDependentRequest addDependentRequest)
        {
            ClaimsPrincipal?user                  = this.httpContextAccessor.HttpContext?.User;
            string?         delegateHdId          = user?.FindFirst("hdid")?.Value;
            RequestResult <DependentModel> result = this.dependentService.AddDependent(delegateHdId ?? string.Empty, addDependentRequest);

            return(new JsonResult(result));
        }
        public void ValidateDependentWithEmptyPatientResPayloadError()
        {
            AddDependentRequest          addDependentRequest = SetupMockInput();
            RequestResult <PatientModel> patientResult       = new RequestResult <PatientModel>();
            // Test Scenario - Happy Path: Found HdId for the PHN, Found Patient.
            IDependentService service = SetupMockDependentService(addDependentRequest, null, patientResult);
            RequestResult <DependentModel> actualResult = service.AddDependent(mockParentHdId, addDependentRequest);

            Assert.Equal(Common.Constants.ResultType.Error, actualResult.ResultStatus);
            Assert.Equal("testhostServer-CE-PAT", actualResult.ResultError.ErrorCode);
        }
        public void ValidateDependentWithWrongDateOfBirth()
        {
            AddDependentRequest addDependentRequest = SetupMockInput();

            addDependentRequest.DateOfBirth = DateTime.Now;
            IDependentService service = SetupMockDependentService(addDependentRequest);
            RequestResult <DependentModel> actualResult = service.AddDependent(mockParentHdId, addDependentRequest);

            var userError = ErrorTranslator.ActionRequired(ErrorMessages.DataMismatch, ActionType.DataMismatch);

            Assert.Equal(Common.Constants.ResultType.ActionRequired, actualResult.ResultStatus);
            Assert.Equal(userError.ErrorCode, actualResult.ResultError.ErrorCode);
            Assert.Equal(mismatchedError, actualResult.ResultError.ResultMessage);
        }
        public void ValidateDependentWithWrongFirstName()
        {
            AddDependentRequest addDependentRequest = SetupMockInput();

            addDependentRequest.FirstName = "wrong";
            IDependentService service = SetupMockDependentService(addDependentRequest);
            RequestResult <DependentModel> actualResult = service.AddDependent(mockParentHdId, addDependentRequest);

            Assert.Equal(Common.Constants.ResultType.Error, actualResult.ResultStatus);
            var serviceError = ErrorTranslator.ServiceError(ErrorType.InvalidState, ServiceType.Patient);

            Assert.Equal(serviceError, actualResult.ResultError.ErrorCode);
            Assert.Equal(mismatchedError, actualResult.ResultError.ResultMessage);
        }
        public void ValidateDependentWithDbError()
        {
            DBResult <ResourceDelegate> insertResult = new DBResult <ResourceDelegate>
            {
                Payload = null,
                Status  = DBStatusCode.Error
            };
            AddDependentRequest            addDependentRequest = SetupMockInput();
            IDependentService              service             = SetupMockDependentService(addDependentRequest, insertResult);
            RequestResult <DependentModel> actualResult        = service.AddDependent(mockParentHdId, addDependentRequest);

            Assert.Equal(Common.Constants.ResultType.Error, actualResult.ResultStatus);
            var serviceError = ErrorTranslator.ServiceError(ErrorType.CommunicationInternal, ServiceType.Database);

            Assert.Equal(serviceError, actualResult.ResultError.ErrorCode);
        }
        public void ValidateDependentWithNoHdId()
        {
            RequestResult <PatientModel> patientResult = new RequestResult <PatientModel>();

            patientResult.ResultStatus    = Common.Constants.ResultType.Success;
            patientResult.ResourcePayload = new PatientModel()
            {
                HdId = string.Empty,
                PersonalHealthNumber = mockPHN,
                FirstName            = mockFirstName,
                LastName             = mockLastName,
                Birthdate            = mockDateOfBirth,
                Gender = mockGender,
            };
            AddDependentRequest            addDependentRequest = SetupMockInput();
            IDependentService              service             = SetupMockDependentService(addDependentRequest, patientResult: patientResult);
            RequestResult <DependentModel> actualResult        = service.AddDependent(mockParentHdId, addDependentRequest);

            var userError = ErrorTranslator.ActionRequired(ErrorMessages.InvalidServicesCard, ActionType.NoHdId);

            Assert.Equal(Common.Constants.ResultType.ActionRequired, actualResult.ResultStatus);
            Assert.Equal(userError.ErrorCode, actualResult.ResultError.ErrorCode);
            Assert.Equal(noHdIdError, actualResult.ResultError.ResultMessage);
        }
        private IDependentService SetupMockDependentService(AddDependentRequest addDependentRequest, DBResult <ResourceDelegate> insertResult = null, RequestResult <PatientModel> patientResult = null)
        {
            var mockPatientService = new Mock <IPatientService>();

            RequestResult <string> patientHdIdResult = new RequestResult <string>()
            {
                ResultStatus    = Common.Constants.ResultType.Success,
                ResourcePayload = mockHdId
            };

            if (addDependentRequest.PHN.Equals(mockPHN))
            {
                // Test Scenario - Happy Path: HiId found for the mockPHN
                patientHdIdResult.ResultStatus    = Common.Constants.ResultType.Success;
                patientHdIdResult.ResourcePayload = mockHdId;
            }
            if (patientResult == null)
            {
                patientResult = new RequestResult <PatientModel>();
                // Test Scenario - Happy Path: Found HdId for the PHN, Found Patient.
                patientResult.ResultStatus    = Common.Constants.ResultType.Success;
                patientResult.ResourcePayload = new PatientModel()
                {
                    HdId = mockHdId,
                    PersonalHealthNumber = mockPHN,
                    FirstName            = mockFirstName,
                    LastName             = mockLastName,
                    Birthdate            = mockDateOfBirth,
                    Gender = mockGender,
                };
            }
            mockPatientService.Setup(s => s.GetPatient(It.IsAny <string>(), It.IsAny <PatientIdentifierType>())).Returns(Task.FromResult(patientResult));

            ResourceDelegate expectedDbDependent = new ResourceDelegate()
            {
                ProfileHdid = mockParentHdId, ResourceOwnerHdid = mockHdId
            };

            if (insertResult == null)
            {
                insertResult = new DBResult <ResourceDelegate>
                {
                    Status = DBStatusCode.Created
                };
            }
            insertResult.Payload = expectedDbDependent;

            Mock <IResourceDelegateDelegate> mockDependentDelegate = new Mock <IResourceDelegateDelegate>();

            mockDependentDelegate.Setup(s => s.Insert(It.Is <ResourceDelegate>(r => r.ProfileHdid == expectedDbDependent.ProfileHdid && r.ResourceOwnerHdid == expectedDbDependent.ResourceOwnerHdid), true)).Returns(insertResult);

            Mock <IUserProfileDelegate> mockUserProfileDelegate = new Mock <IUserProfileDelegate>();

            mockUserProfileDelegate.Setup(s => s.GetUserProfile(mockParentHdId)).Returns(new DBResult <UserProfile>()
            {
                Payload = new UserProfile()
            });
            Mock <INotificationSettingsService> mockNotificationSettingsService = new Mock <INotificationSettingsService>();

            mockNotificationSettingsService.Setup(s => s.QueueNotificationSettings(It.IsAny <NotificationSettingsRequest>()));
            Mock <IConfigurationService> configServiceMock = new Mock <IConfigurationService>();

            configServiceMock.Setup(s => s.GetConfiguration()).Returns(new ExternalConfiguration());
            return(new DependentService(
                       new Mock <ILogger <DependentService> >().Object,
                       mockUserProfileDelegate.Object,
                       mockPatientService.Object,
                       mockNotificationSettingsService.Object,
                       mockDependentDelegate.Object,
                       configServiceMock.Object
                       ));
        }
Пример #10
0
        /// <inheritdoc />
        public RequestResult <DependentModel> AddDependent(string delegateHdId, AddDependentRequest addDependentRequest)
        {
            this.logger.LogTrace($"Dependent hdid: {delegateHdId}");

            int?maxDependentAge = this.configurationService.GetConfiguration().WebClient.MaxDependentAge;

            if (maxDependentAge.HasValue)
            {
                DateTime minimumBirthDate = DateTime.UtcNow.AddYears(maxDependentAge.Value * -1);
                if (addDependentRequest.DateOfBirth < minimumBirthDate)
                {
                    return(new RequestResult <DependentModel>()
                    {
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = "Dependent age exceeds the maximum limit", ErrorCode = ErrorTranslator.ServiceError(ErrorType.InvalidState, ServiceType.Patient)
                        },
                    });
                }
            }

            this.logger.LogTrace("Getting dependent details...");
            RequestResult <PatientModel> patientResult = Task.Run(async() => await this.patientService.GetPatient(addDependentRequest.PHN, PatientIdentifierType.PHN).ConfigureAwait(true)).Result;

            if (patientResult.ResultStatus == ResultType.Error)
            {
                return(new RequestResult <DependentModel>()
                {
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = "Communication Exception when trying to retrieve the Dependent", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.Patient)
                    },
                });
            }

            if (patientResult.ResultStatus == ResultType.ActionRequired)
            {
                return(new RequestResult <DependentModel>()
                {
                    ResultStatus = ResultType.ActionRequired,
                    ResultError = ErrorTranslator.ActionRequired(ErrorMessages.DataMismatch, ActionType.DataMismatch),
                });
            }

            this.logger.LogDebug($"Finished getting dependent details...{JsonSerializer.Serialize(patientResult)}");

            // Verify dependent's details entered by user
            if (patientResult.ResourcePayload == null || !this.ValidateDependent(addDependentRequest, patientResult.ResourcePayload))
            {
                this.logger.LogDebug($"Dependent information does not match request: {JsonSerializer.Serialize(addDependentRequest)} response: {JsonSerializer.Serialize(patientResult.ResourcePayload)}");
                return(new RequestResult <DependentModel>()
                {
                    ResultStatus = ResultType.ActionRequired,
                    ResultError = ErrorTranslator.ActionRequired(ErrorMessages.DataMismatch, ActionType.DataMismatch),
                });
            }

            // Verify dependent has HDID
            if (string.IsNullOrEmpty(patientResult.ResourcePayload.HdId))
            {
                return(new RequestResult <DependentModel>()
                {
                    ResultStatus = ResultType.ActionRequired,
                    ResultError = ErrorTranslator.ActionRequired(ErrorMessages.InvalidServicesCard, ActionType.NoHdId),
                });
            }

            string       json    = JsonSerializer.Serialize(addDependentRequest.TestDate, addDependentRequest.TestDate.GetType());
            JsonDocument jsonDoc = JsonDocument.Parse(json);

            // Insert Dependent to database
            var dependent = new ResourceDelegate()
            {
                ResourceOwnerHdid = patientResult.ResourcePayload.HdId,
                ProfileHdid       = delegateHdId,
                ReasonCode        = ResourceDelegateReason.COVIDLab,
                ReasonObjectType  = addDependentRequest.TestDate.GetType().AssemblyQualifiedName,
                ReasonObject      = jsonDoc,
            };
            DBResult <ResourceDelegate> dbDependent = this.resourceDelegateDelegate.Insert(dependent, true);

            if (dbDependent.Status == DBStatusCode.Created)
            {
                this.logger.LogTrace("Finished adding dependent");
                this.UpdateNotificationSettings(dependent.ResourceOwnerHdid, delegateHdId);

                return(new RequestResult <DependentModel>()
                {
                    ResourcePayload = DependentModel.CreateFromModels(dbDependent.Payload, patientResult.ResourcePayload),
                    ResultStatus = ResultType.Success,
                });
            }
            else
            {
                this.logger.LogError("Error adding dependent");
                return(new RequestResult <DependentModel>()
                {
                    ResourcePayload = new DependentModel(),
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = dbDependent.Message, ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationInternal, ServiceType.Database)
                    },
                });
            }
        }
Пример #11
0
        /// <inheritdoc />
        public RequestResult <DependentModel> AddDependent(string delegateHdId, AddDependentRequest addDependentRequest)
        {
            this.logger.LogTrace($"Dependent hdid: {delegateHdId}");
            this.logger.LogDebug("Getting dependent details...");
            RequestResult <PatientModel> patientResult = Task.Run(async() => await this.patientService.GetPatient(addDependentRequest.PHN, PatientIdentifierType.PHN).ConfigureAwait(true)).Result;

            if (patientResult.ResourcePayload == null)
            {
                return(new RequestResult <DependentModel>()
                {
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = "Communication Exception when trying to retrieve the Dependent", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.Patient)
                    },
                });
            }

            // Verify dependent's details entered by user
            if (!addDependentRequest.Equals(patientResult.ResourcePayload))
            {
                return(new RequestResult <DependentModel>()
                {
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = "The information you entered did not match. Please try again.", ErrorCode = ErrorTranslator.ServiceError(ErrorType.InvalidState, ServiceType.Patient)
                    },
                });
            }
            else
            {
                // Insert Dependent to database
                var dependent = new UserDelegate()
                {
                    OwnerId = patientResult.ResourcePayload.HdId, DelegateId = delegateHdId
                };

                DBResult <UserDelegate> dbDependent = this.userDelegateDelegate.Insert(dependent, true);
                if (dbDependent.Status == DBStatusCode.Created)
                {
                    this.logger.LogDebug("Finished adding dependent");
                    this.UpdateNotificationSettings(dependent.OwnerId, delegateHdId);
                    return(new RequestResult <DependentModel>()
                    {
                        ResourcePayload = DependentModel.CreateFromModels(dbDependent.Payload, patientResult.ResourcePayload),
                        ResultStatus = ResultType.Success,
                    });
                }
                else
                {
                    this.logger.LogError("Error adding dependent");
                    return(new RequestResult <DependentModel>()
                    {
                        ResourcePayload = new DependentModel(),
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = dbDependent.Message, ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationInternal, ServiceType.Database)
                        },
                    });
                }
            }
        }