/// <summary> /// Get the postcode sector by the UDPRN id /// </summary> /// <param name="uDPRN">UDPRN id</param> /// <returns>PostCodeSectorDTO object</returns> public async Task <PostCodeSectorDTO> GetPostCodeSectorByUDPRN(int uDPRN) { using (loggingHelper.RMTraceManager.StartTrace("DataService.GetPostCodeSectorByUDPRN")) { string methodName = MethodHelper.GetActualAsyncMethodName(); loggingHelper.Log(methodName + LoggerTraceConstants.COLON + LoggerTraceConstants.MethodExecutionStarted, TraceEventType.Verbose, null, LoggerTraceConstants.Category, LoggerTraceConstants.PostCodePriority, LoggerTraceConstants.PostCodeSectorDataServiceMethodEntryEventId, LoggerTraceConstants.Title); PostalAddress postalAddress = await DataContext.PostalAddresses.AsNoTracking().Where(pa => pa.UDPRN == uDPRN).SingleOrDefaultAsync(); PostcodeSector postCodeSector = postalAddress.Postcode1.PostcodeSector; PostCodeSectorDTO postCodeSectorDTO = new PostCodeSectorDTO(); GenericMapper.Map(postCodeSector, postCodeSectorDTO); loggingHelper.Log(methodName + LoggerTraceConstants.COLON + LoggerTraceConstants.MethodExecutionCompleted, TraceEventType.Verbose, null, LoggerTraceConstants.Category, LoggerTraceConstants.PostCodePriority, LoggerTraceConstants.PostCodeSectorDataServiceMethodExitEventId, LoggerTraceConstants.Title); return(postCodeSectorDTO); } }
/// <summary> /// Create notification if the delivery point is not within tolerance /// </summary> /// <param name="notificationType">Type of notification</param> /// <param name="udprn">Postal address UDPRN</param> /// <param name="notificationBody">Message specified in body</param> /// <param name="notificationReference">Notification reference</param> /// <param name="notificationAction">Notification action</param> /// <returns>Integer value one if value is saved in database</returns> private int CreateNotificationAfterToleranceCheck(Guid notificationType, int udprn, string notificationBody, string notificationReference, string notificationAction, PostalAddressDataDTO postalAddressDTO) { PostCodeSectorDTO postCodeSectorDTO = thirdPartyAddressLocationIntegrationService.GetPostCodeSectorByUDPRN(udprn).Result; NotificationDTO notificationDetails = new NotificationDTO { ID = Guid.NewGuid(), NotificationType_GUID = notificationType, NotificationDueDate = DateTime.UtcNow.AddHours(ThirdPartyAddressLocationConstants.NOTIFICATIONDUE), NotificationSource = ThirdPartyAddressLocationConstants.USRNOTIFICATIONSOURCE, Notification_Heading = notificationAction, Notification_Message = string.Format(notificationBody, NotificationAddressFields(postalAddressDTO)), PostcodeDistrict = (postCodeSectorDTO == null || postCodeSectorDTO.District == null) ? string.Empty : postCodeSectorDTO.District, NotificationActionLink = string.Format(ThirdPartyAddressLocationConstants.USRNOTIFICATIONLINK, notificationReference) }; return(thirdPartyAddressLocationIntegrationService.AddNewNotification(notificationDetails).Result); }
/// <summary> /// Get the PostCodeSectorDTO on UDPRN /// </summary> /// <param name="uDPRN">UDPRN id</param> /// <returns>PostCodeSectorDTO</returns> public async Task <PostCodeSectorDTO> GetPostCodeSectorByUDPRN(int uDPRN) { using (loggingHelper.RMTraceManager.StartTrace("Integration.GetPostCodeSectorByUDPRN")) { string method = typeof(ThirdPartyAddressLocationIntegrationService) + "." + nameof(GetPostCodeSectorByUDPRN); loggingHelper.LogMethodEntry(method, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationIntegrationServiceMethodEntryEventId); string methodName = ThirdPartyAddressLocationConstants.GetPostCodeSectorByUDPRN; string serviceUrl = configurationHelper.ReadAppSettingsConfigurationValues(UnitManagerDataWebAPIName); string route = configurationHelper.ReadAppSettingsConfigurationValues(methodName); HttpResponseMessage result = await httpHandler.GetAsync(string.Format(serviceUrl + route, uDPRN.ToString())); if (!result.IsSuccessStatusCode) { var responseContent = result.ReasonPhrase; throw new ServiceException(responseContent); } PostCodeSectorDTO postcodeSectorDTO = JsonConvert.DeserializeObject <PostCodeSectorDTO>(result.Content.ReadAsStringAsync().Result); loggingHelper.LogMethodExit(method, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationIntegrationServiceMethodExitEventId); return(postcodeSectorDTO); } }
// TODO : Add method when ready // To be implemented in parallel /// <summary> /// Method to save the list of USR data into the database. /// </summary> /// <param name="addressLocationUsrpostdtos">List of Address Locations</param> /// <returns>Task</returns> public async Task SaveUSRDetails(List <AddressLocationUSRPOSTDTO> addressLocationUsrpostdtos) { int fileUdprn; string addressLocationChangeType = string.Empty; if (addressLocationUsrpostdtos == null) { throw new ArgumentNullException(nameof(addressLocationUsrpostdtos)); } using (loggingHelper.RMTraceManager.StartTrace("Business.SaveUSRDetails")) { string methodName = typeof(ThirdPartyAddressLocationBusinessService) + "." + nameof(SaveUSRDetails); loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodEntryEventId); List <string> categoryNamesSimpleLists = new List <string> { ThirdPartyAddressLocationConstants.TASKNOTIFICATION, ThirdPartyAddressLocationConstants.NETWORKLINKDATAPROVIDER, ThirdPartyAddressLocationConstants.DeliveryPointUseIndicator, ThirdPartyAddressLocationConstants.APPROXLOCATION, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ReferenceDataCategoryNames.NetworkNodeType }; // Get all the reference data Guids required for the below Guid tasktypeId = GetReferenceData(categoryNamesSimpleLists, ThirdPartyAddressLocationConstants.TASKNOTIFICATION, ThirdPartyAddressLocationConstants.TASKACTION); Guid locationProviderId = GetReferenceData(categoryNamesSimpleLists, ThirdPartyAddressLocationConstants.NETWORKLINKDATAPROVIDER, ThirdPartyAddressLocationConstants.EXTERNAL); Guid operationalStatusGUIDLive = GetReferenceData(categoryNamesSimpleLists, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ThirdPartyAddressLocationConstants.OperationalStatusGUIDLive, true); Guid networkNodeTypeRMGServiceNode = GetReferenceData(categoryNamesSimpleLists, ReferenceDataCategoryNames.NetworkNodeType, ThirdPartyAddressLocationConstants.NetworkNodeTypeRMGServiceNode, true); Guid approxLocation = GetReferenceData(categoryNamesSimpleLists, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ThirdPartyAddressLocationConstants.APPROXLOCATION, true); Guid notificationTypeId_GUID = await thirdPartyAddressLocationIntegrationService.GetReferenceDataId(ThirdPartyAddressLocationConstants.USRCATEGORY, ThirdPartyAddressLocationConstants.USRREFERENCEDATANAME); foreach (AddressLocationUSRPOSTDTO addressLocationUSRPOSTDTO in addressLocationUsrpostdtos) { // Get the udprn id for each USR record. fileUdprn = (int)addressLocationUSRPOSTDTO.UDPRN; addressLocationChangeType = addressLocationUSRPOSTDTO.ChangeType; // To save new location if (addressLocationChangeType.Equals(ThirdPartyAddressLocationConstants.INSERT)) { if (!await addressLocationDataService.AddressLocationExists(fileUdprn)) { DbGeometry spatialLocationXY = GetSpatialLocation(addressLocationUSRPOSTDTO); AddressLocationDataDTO newAddressLocationDTO = new AddressLocationDataDTO() { ID = Guid.NewGuid(), UDPRN = (int)addressLocationUSRPOSTDTO.UDPRN, LocationXY = spatialLocationXY, Lattitude = (decimal)addressLocationUSRPOSTDTO.Latitude, Longitude = (decimal)addressLocationUSRPOSTDTO.Longitude }; // Save the address location data record to the database. await addressLocationDataService.SaveNewAddressLocation(newAddressLocationDTO); PostalAddressDataDTO postalAddressDataDTO = await addressLocationDataService.GetPostalAddressData((int)fileUdprn); // Check if the delivery point exists if (postalAddressDataDTO.DeliveryPoints != null && postalAddressDataDTO.DeliveryPoints.Count > 0) { DeliveryPointDTO deliveryPointDTO = await thirdPartyAddressLocationIntegrationService.GetDeliveryPointByPostalAddress(postalAddressDataDTO.ID); // Check if the existing delivery point has an approx location. if (deliveryPointDTO.OperationalStatus_GUID == approxLocation) { deliveryPointDTO.LocationXY = spatialLocationXY; deliveryPointDTO.LocationProvider_GUID = locationProviderId; deliveryPointDTO.UDPRN = fileUdprn; deliveryPointDTO.OperationalStatus_GUID = operationalStatusGUIDLive; deliveryPointDTO.NetworkNodeType_GUID = networkNodeTypeRMGServiceNode; // Update the location details for the delivery point await thirdPartyAddressLocationIntegrationService.UpdateDeliveryPointById(deliveryPointDTO); // Check if a notification exists for the UDPRN. if (await addressLocationDataService.CheckIfNotificationExists(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION)) { // update the notification if it exists. await thirdPartyAddressLocationIntegrationService.UpdateNotificationByUDPRN(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION, ThirdPartyAddressLocationConstants.NOTIFICATIONCLOSED); } } else { // Calculates the straight line distance between the existing delivery // point and the new delivery point. var straightLineDistance = GetDeliveryPointDistance(deliveryPointDTO, spatialLocationXY); // Check if the new point is within the tolerance limit if (straightLineDistance <= ThirdPartyAddressLocationConstants.TOLERANCEDISTANCEINMETERS) { deliveryPointDTO.LocationXY = spatialLocationXY; deliveryPointDTO.LocationProvider_GUID = locationProviderId; deliveryPointDTO.UDPRN = fileUdprn; deliveryPointDTO.OperationalStatus_GUID = operationalStatusGUIDLive; deliveryPointDTO.NetworkNodeType_GUID = networkNodeTypeRMGServiceNode; // Update the delivery point location directly in case it is within // the tolerance limits. await thirdPartyAddressLocationIntegrationService.UpdateDeliveryPointById(deliveryPointDTO); // Check if the notification exists for the given UDPRN. if (await addressLocationDataService.CheckIfNotificationExists(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION)) { // update the notification if it exists. await thirdPartyAddressLocationIntegrationService.UpdateNotificationByUDPRN(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION, ThirdPartyAddressLocationConstants.NOTIFICATIONCLOSED); } } else { // Get the Postcode Sector by UDPRN PostCodeSectorDTO postCodeSectorDTO = await thirdPartyAddressLocationIntegrationService.GetPostCodeSectorByUDPRN(fileUdprn); PostalAddressDataDTO postalAddressDTO = await addressLocationDataService.GetPostalAddressData(fileUdprn); NotificationDTO notificationDO = new NotificationDTO { ID = Guid.NewGuid(), NotificationType_GUID = notificationTypeId_GUID, NotificationDueDate = DateTime.UtcNow.AddHours(ThirdPartyAddressLocationConstants.NOTIFICATIONDUE), NotificationSource = ThirdPartyAddressLocationConstants.USRNOTIFICATIONSOURCE, Notification_Heading = ThirdPartyAddressLocationConstants.USRACTION, Notification_Message = AddressFields(postalAddressDTO), PostcodeDistrict = (postCodeSectorDTO == null || postCodeSectorDTO.District == null) ? string.Empty : postCodeSectorDTO.District, PostcodeSector = (postCodeSectorDTO == null || postCodeSectorDTO.Sector == null) ? string.Empty : postCodeSectorDTO.Sector, NotificationActionLink = string.Format(ThirdPartyAddressLocationConstants.USRNOTIFICATIONLINK, fileUdprn) }; // Insert the new notification. await thirdPartyAddressLocationIntegrationService.AddNewNotification(notificationDO); } } } } } // To update existing location else if (addressLocationChangeType.Equals(ThirdPartyAddressLocationConstants.UPDATE)) { // Match to Location on UDPRN (update Location) if (await addressLocationDataService.AddressLocationExists(fileUdprn)) { // Update the Address location. await UpdateAddressLocationByUDPRN(addressLocationUSRPOSTDTO); // Update the DP location. Story: RFMO-276 await UpdateDPLocation(addressLocationUSRPOSTDTO, notificationTypeId_GUID); } // No Match to Location on UDPRN - Log Error else { loggingHelper.Log(string.Format(ThirdPartyAddressLocationConstants.NoMatchToAddressOnUDPRN, fileUdprn), TraceEventType.Error); } } // To delete existing location else if (addressLocationChangeType.Equals(ThirdPartyAddressLocationConstants.DELETE)) { await DeleteUSRDetails(addressLocationUSRPOSTDTO); } } loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodExitEventId); } }
/// <summary> /// Method to delete the list of Third Party Address Location data into the database. /// </summary> /// <param name="addressLocationUsrpostdtos">List of Address Locations</param> /// <returns>Task</returns> public async Task DeleteUSRDetails(AddressLocationUSRPOSTDTO addressLocationUsrpostdtos) { int fileUdprn; using (loggingHelper.RMTraceManager.StartTrace("Business.DeleteUSRDetails")) { string methodName = typeof(ThirdPartyAddressLocationBusinessService) + "." + nameof(DeleteUSRDetails); loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodEntryEventId); List <string> categoryNamesSimpleLists = new List <string> { ThirdPartyAddressLocationConstants.TASKNOTIFICATION, ThirdPartyAddressLocationConstants.NETWORKLINKDATAPROVIDER, ThirdPartyAddressLocationConstants.DeliveryPointUseIndicator, ThirdPartyAddressLocationConstants.APPROXLOCATION, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ReferenceDataCategoryNames.NetworkNodeType }; // Get all the reference data Guids required for the below Guid locationProviderInternalId = GetReferenceData(categoryNamesSimpleLists, ThirdPartyAddressLocationConstants.NETWORKLINKDATAPROVIDER, ThirdPartyAddressLocationConstants.INTERNAL); Guid notificationTypeId_GUID = await thirdPartyAddressLocationIntegrationService.GetReferenceDataId(ThirdPartyAddressLocationConstants.USRCATEGORY, ThirdPartyAddressLocationConstants.USRREFERENCEDATANAME); // Get the udprn id for each USR record. fileUdprn = (int)addressLocationUsrpostdtos.UDPRN; var addressLocation = addressLocationDataService.GetAddressLocationByUDPRN(fileUdprn).Result; if (addressLocation != null) { // Delete the address location data record to the database. await addressLocationDataService.DeleteAddressLocation(addressLocation); PostalAddressDataDTO postalAddressDataDTO = await addressLocationDataService.GetPostalAddressData((int)fileUdprn); // Check if the delivery point exists if (postalAddressDataDTO.DeliveryPoints != null && postalAddressDataDTO.DeliveryPoints.Count > 0) { DeliveryPointDTO deliveryPointDTO = await thirdPartyAddressLocationIntegrationService.GetDeliveryPointByPostalAddress(postalAddressDataDTO.ID); deliveryPointDTO.LocationProvider_GUID = locationProviderInternalId; // Update the location provider for the delivery point await thirdPartyAddressLocationIntegrationService.UpdateDeliveryPointById(deliveryPointDTO); // Get the Postcode Sector by UDPRN PostCodeSectorDTO postCodeSectorDTO = await thirdPartyAddressLocationIntegrationService.GetPostCodeSectorByUDPRN(fileUdprn); PostalAddressDataDTO postalAddressDTO = await addressLocationDataService.GetPostalAddressData(fileUdprn); NotificationDTO notificationDO = new NotificationDTO { ID = Guid.NewGuid(), NotificationType_GUID = notificationTypeId_GUID, NotificationDueDate = DateTime.UtcNow.AddHours(ThirdPartyAddressLocationConstants.NOTIFICATIONDUE), NotificationSource = ThirdPartyAddressLocationConstants.USRNOTIFICATIONSOURCE, Notification_Heading = ThirdPartyAddressLocationConstants.USRDELETEACTION, Notification_Message = Notification_Body(postalAddressDTO), PostcodeDistrict = (postCodeSectorDTO == null || postCodeSectorDTO.District == null) ? string.Empty : postCodeSectorDTO.District, NotificationActionLink = string.Format(ThirdPartyAddressLocationConstants.USRNOTIFICATIONLINK, fileUdprn) }; // Insert the new notification. await thirdPartyAddressLocationIntegrationService.AddNewNotification(notificationDO); } } else { // Log error loggingHelper.Log(string.Format(ThirdPartyAddressLocationConstants.USRERRORLOGMESSAGE, ThirdPartyAddressLocationConstants.ErrorMessageForAddressTypeUSRNotFound, fileUdprn, null, FileType.Usr, DateTime.UtcNow), TraceEventType.Error); } loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodExitEventId); } }