Example #1
0
        public async Task AddFacilityTest()
        {
            //Arrange
            _mockRepo.Reset();
            _mockRepo.Setup(b => b.GetFacilityByCode(It.IsAny <string>())).ReturnsAsync(() => null);
            _mockRepo.Setup(b => b.AddFacility(It.IsAny <FacilityEntity>())).ReturnsAsync(() => true);

            //Act
            var result = await _subjectUnderTest.Add(new NewFacilityRequest());

            //Assert
            _mockRepo.VerifyAll();
            Assert.NotNull(result);
            Assert.Equal(CreateUpdateResultEnum.Success, result.OperationResult);
        }
        public async Task <IActionResult> AddFacility([FromBody] NewFacilityRequest request)
        {
            _logger.LogDebug("Add facility request received, data: {@NewFacilityRequest}", request);
            if (ModelState.IsValid)
            {
                var facilityCreationResult = await _facilityManager.Add(request);

                if (facilityCreationResult.OperationResult == CreateUpdateResultEnum.Success)
                {
                    _logger.LogDebug("Entity has been saved with facility {Facility}:",
                                     facilityCreationResult.Object.FacilityKey);
                    //var eventMessage = new FacilityAddedIntegrationEvent()
                    //{
                    //    //TODO: following code needs to be updated.
                    //    IsActive = facilityCreationResult.Object.ActiveFlag,
                    //    ProcessInactiveAsException = false,// facilityEntity.ProcessInactiveAsException,
                    //    FacilityId = request.Id,//ID
                    //    FacilityCode = facilityCreationResult.Object.FacilityCode,
                    //    ADUIgnoreStockOut = false,//facilityEntity.ADUIgnoreStockOut,
                    //    AduIgnoreCritLow = false,//facilityEntity.AduIgnoreCritLow

                    //};
                    //TODO: This code is to be removed. commented temporarily to test out event bus.
                    //var headers =
                    //    _httpContextAccessor.HttpContext.Request.Headers
                    //        .ToDictionary<KeyValuePair<string, StringValues>, string, string>(item => item.Key,
                    //            item => item.Value);
                    //_eventBus.Publish(_messageBusTopicsConfiguration.KafkaFacilityDetailsTopic, eventMessage, headers);
                    //_logger.LogDebug("data published to {EventBus} and data object :{@Data}",
                    //    _messageBusTopicsConfiguration.KafkaFacilityDetailsTopic, eventMessage);
                    return(new CreatedResult(
                               facilityCreationResult.Object.FacilityKey.ToString(), facilityCreationResult.Object));
                }

                if (facilityCreationResult.OperationResult == CreateUpdateResultEnum.ErrorAlreadyExists)
                {
                    _logger.LogError("Record already exists with facility code {FacilityCode}:",
                                     request.FacilityCode);
                    ModelState.AddModelError(nameof(request.FacilityCode), "Record already exists with facility code");
                }
                if (facilityCreationResult.OperationResult == CreateUpdateResultEnum.ValidationFailed ||
                    facilityCreationResult.OperationResult == CreateUpdateResultEnum.UnknownError)
                {
                    _logger.LogError(
                        "Error in saving facility due to validation failure for data {@Facility}",
                        request);
                    ModelState.AddModelError(string.Empty,
                                             "Error in saving facility due to validation failure for data.");
                }
            }


            _logger.LogDebug("ModelState validation failed :{@Data}", request);
            return(BadRequest(new ModelStateRequestValidationAdaptor(ModelState)));
        }