public void ASTIUpdate_RegularConnectionsNotDeleted() { // Arrange var serviceId = Guid.NewGuid(); var channelId = Guid.NewGuid(); var request = new V7VmOpenApiServiceAndChannelRelationAstiInBase { IsASTI = true, ServiceId = serviceId, ChannelRelations = new List <V7VmOpenApiServiceServiceChannelAstiInBase> { new V7VmOpenApiServiceServiceChannelAstiInBase { ServiceGuid = serviceId, ChannelGuid = channelId, } } }; var unitOfWork = unitOfWorkMockSetup.Object; var contextManager = new TestContextManager(unitOfWork, unitOfWork); var serviceUtilities = new ServiceUtilities(UserIdentificationMock.Object, LockingManager, contextManager, UserOrganizationService, VersioningManager, UserInfoService, UserOrganizationChecker); translationManagerVModelMockSetup.Setup(s => s.Translate <V7VmOpenApiServiceAndChannelRelationAstiInBase, Model.Models.Service>(request, unitOfWork)) .Returns(new Model.Models.Service() { Id = serviceId, ServiceServiceChannels = new List <ServiceServiceChannel> { new ServiceServiceChannel { IsASTIConnection = true, ServiceChannelId = channelId }, new ServiceServiceChannel { IsASTIConnection = false, ServiceChannelId = Guid.NewGuid() } } }); // repositories ConnectionRepoMock.Setup(c => c.All()).Returns(new List <ServiceServiceChannel>().AsQueryable()); DescriptionRepoMock.Setup(c => c.All()).Returns(new List <ServiceServiceChannelDescription>().AsQueryable()); var service = new ServiceAndChannelService(contextManager, translationManagerMockSetup.Object, translationManagerVModelMockSetup.Object, Logger, serviceUtilities, DataUtils, ServiceService, ChannelService, PublishingStatusCache, VersioningManager, UserOrganizationChecker, CacheManager, UserOrganizationService); // Act var result = service.SaveServiceConnections(request, DefaultVersion); // Assert // We are not testing method GetServiceById so we expect result to be null. result.Should().BeNull(); translationManagerVModelMockSetup.Verify(t => t.Translate <V7VmOpenApiServiceAndChannelRelationAstiInBase, Model.Models.Service>(It.IsAny <V7VmOpenApiServiceAndChannelRelationAstiInBase>(), unitOfWork), Times.Once()); ConnectionRepoMock.Verify(x => x.Remove(It.IsAny <ServiceServiceChannel>()), Times.Never()); DescriptionRepoMock.Verify(x => x.Remove(It.IsAny <ServiceServiceChannelDescription>()), Times.Never()); }
/// <summary> /// Updates the channel connections for a defined service. External source identifiers are used. /// </summary> /// <param name="serviceSourceId">The external source identifier of the service.</param> /// <param name="relationsBySource">The connection model</param> /// <param name="openApiVersion">The open api version to be returned.</param> /// <returns>Updated service with connection information</returns> public IVmOpenApiServiceVersionBase SaveServiceConnectionsBySource(string serviceSourceId, V7VmOpenApiServiceAndChannelRelationBySourceAsti relationsBySource, int openApiVersion) { if (relationsBySource == null) { return(null); } var errors = new List <string>(); var userId = utilities.GetRelationIdForExternalSource(); var relations = new V7VmOpenApiServiceAndChannelRelationAstiInBase() { DeleteAllChannelRelations = relationsBySource.DeleteAllChannelRelations, IsASTI = relationsBySource.IsASTI }; contextManager.ExecuteReader(unitOfWork => { try { relations.ServiceId = GetPTVId <Service>(serviceSourceId, userId, unitOfWork); foreach (var channelRelation in relationsBySource.ChannelRelations) { var channelId = GetPTVId <ServiceChannel>(channelRelation.ServiceChannelSourceId, userId, unitOfWork); if (channelId.IsAssigned()) { CheckChannelData(unitOfWork, channelId, channelRelation.IsASTIConnection, channelRelation.ServiceHours, channelRelation.ContactDetails); channelRelation.ExtraTypes.ForEach(e => { e.ChannelGuid = channelId; e.ServiceGuid = relations.ServiceId.Value; }); relations.ChannelRelations.Add(new V7VmOpenApiServiceServiceChannelAstiInBase { ChannelGuid = channelId, ServiceGuid = relations.ServiceId.Value, Description = channelRelation.Description, ServiceChargeType = channelRelation.ServiceChargeType, ExtraTypes = channelRelation.ExtraTypes, ServiceHours = channelRelation.ServiceHours, ContactDetails = channelRelation.ContactDetails, IsASTIConnection = channelRelation.IsASTIConnection, DeleteServiceChargeType = channelRelation.DeleteServiceChargeType, DeleteAllDescriptions = channelRelation.DeleteAllDescriptions, DeleteAllServiceHours = channelRelation.DeleteAllServiceHours, DeleteAllExtraTypes = channelRelation.DeleteAllExtraTypes, }); } } } catch (Exception ex) { errors.Add(ex.Message); } }); if (errors.Count > 0) { throw new Exception(String.Join(", ", errors)); } return(SaveServiceConnections(relations, openApiVersion)); }
/// <summary> /// Update service and channel relationships. This is for ASTI connections. /// </summary> /// <param name="serviceId"></param> /// <param name="request"></param> /// <returns></returns> protected virtual IActionResult PutServiceAndChannelBase(string serviceId, V7VmOpenApiServiceAndChannelRelationAstiInBase request) { if (request == null) { ModelState.AddModelError("RequestIsNull", CoreMessages.OpenApi.RequestIsNull); return(new BadRequestObjectResult(ModelState)); } request.IsASTI = true; request.ChannelRelations.ForEach(r => r.IsASTIConnection = true); return(PutServiceAndChannelByService(serviceId, request)); }
/// <summary> /// Converts model into version 7. /// </summary> /// <returns></returns> public V7VmOpenApiServiceAndChannelRelationAstiInBase ConvertToLatestVersion() { var vm = new V7VmOpenApiServiceAndChannelRelationAstiInBase() { ServiceId = this.ServiceId, DeleteAllChannelRelations = this.DeleteAllChannelRelations, IsASTI = false }; this.ChannelRelations.ForEach(relation => vm.ChannelRelations.Add(relation.ConvertToLatestVersion())); return(vm); }
/// <summary> /// Updates channel connections for a defined service. This is used for both ASTI and regular connections /// Regular users can update data within ASTI connections but they cannot remove any ASTI connections. /// ASTI users can remove and update ASTI connections - regular connections are not removed. /// More rules in https://confluence.csc.fi/display/PAL/ASTI-project and https://jira.csc.fi/browse/PTV-2065. /// </summary> /// <param name="relations">The connection model</param> /// <param name="openApiVersion">The open api version to be returned.</param> /// <returns>Updated service with connection information</returns> public IVmOpenApiServiceVersionBase SaveServiceConnections(V7VmOpenApiServiceAndChannelRelationAstiInBase relations, int openApiVersion) { if (relations == null) { return(null); } Service service; var rootID = relations.ServiceId.Value; try { contextManager.ExecuteWriter(unitOfWork => { service = TranslationManagerToEntity.Translate <V7VmOpenApiServiceAndChannelRelationAstiInBase, Service>(relations, unitOfWork); // We need to manually remove right connections from collection. // If connections are ASTI connections regular connections should stay as they are. // If connections are regular connections ASTI connections should stay as they are - the data can be updated though (except extraTypes). var updatedConnectionIds = relations.ChannelRelations?.Count > 0 ? relations.ChannelRelations.Select(r => r.ChannelGuid).ToList() : new List <Guid>(); var updatedConnections = service.ServiceServiceChannels.Where(c => updatedConnectionIds.Contains(c.ServiceChannelId)).ToList(); var connections = service.ServiceServiceChannels.Where(c => c.IsASTIConnection == relations.IsASTI).ToList(); var connectionsToRemove = connections.Where(a => !updatedConnectionIds.Contains(a.ServiceChannelId)).ToList(); RemoveConnections(unitOfWork, connectionsToRemove); unitOfWork.Save(); }); } catch (Exception ex) { var errorMsg = $"Error occured while updating relations for a service with id {rootID}. {ex.Message}"; logger.LogError(errorMsg + " " + ex.StackTrace); throw new Exception(errorMsg); } return(serviceService.GetServiceById(rootID, openApiVersion, VersionStatusEnum.Latest)); }
public IActionResult ASTIPutServiceAndChannel(string serviceId, [FromBody] V7VmOpenApiServiceAndChannelRelationAstiInBase request) { return(PutServiceAndChannelBase(serviceId, request)); }
private IActionResult PutServiceAndChannelByService(string serviceId, V7VmOpenApiServiceAndChannelRelationAstiInBase request) { // Validate the items if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } if (!string.IsNullOrEmpty(serviceId)) { request.ServiceId = serviceId.ParseToGuid(); // check that service exists if (!request.ServiceId.HasValue || !serviceService.ServiceExists(request.ServiceId.Value)) { return(NotFound(new VmError() { ErrorMessage = $"Service with id '{serviceId}' not found." })); } var currentVersion = serviceService.GetServiceByIdSimple(request.ServiceId.Value); if (currentVersion == null || string.IsNullOrEmpty(currentVersion.PublishingStatus)) { this.ModelState.AddModelError("Service id", $"Version for service with id '{serviceId}' not found."); } else { // Validate publishing status PublishingStatusValidator status = new PublishingStatusValidator(PublishingStatus.Published.ToString(), currentVersion.PublishingStatus); status.Validate(ModelState); } // Validate the items if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } } else { return(NotFound(new VmError() { ErrorMessage = $"Service id has to be set." })); } // Check the item values from db and validate request.ChannelRelations.ForEach(r => { r.ServiceGuid = request.ServiceId.Value; r.ChannelGuid = r.ServiceChannelId.ParseToGuidWithExeption(); r.ExtraTypes.ForEach(e => { e.ServiceGuid = r.ServiceGuid; e.ChannelGuid = r.ChannelGuid; }); }); // Asti users have Eeva rights - so channel visibility is not checked! var channels = new ServiceConnectionListValidator(request.ChannelRelations, channelService, request.IsASTI ? UserRoleEnum.Eeva : UserRole()); channels.Validate(this.ModelState); // Validate the items if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } var srv = serviceAndChannelService.SaveServiceConnections(request, versionNumber); if (srv == null) { return(NotFound(new VmError() { ErrorMessage = $"Service with id '{serviceId}' not found." })); } return(Ok(srv)); }