private async Task <int> AssociateProjectGeofence(ProjectGeofence projectGeofence, ProjectGeofence existing) { var upsertedCount = 0; if (existing == null) { Log.LogDebug( $"ProjectRepository/AssociateProjectGeofence: projectGeofence={JsonConvert.SerializeObject(projectGeofence)}"); const string insert = @"INSERT ProjectGeofence (fk_GeofenceUID, fk_ProjectUID, LastActionedUTC) VALUES (@GeofenceUID, @ProjectUID, @LastActionedUTC)"; upsertedCount = await ExecuteWithAsyncPolicy(insert, projectGeofence); Log.LogDebug( $"ProjectRepository/AssociateProjectGeofence: inserted {upsertedCount} rows for: projectUid:{projectGeofence.ProjectUID} geofenceUid:{projectGeofence.GeofenceUID}"); return(upsertedCount); } Log.LogDebug( $"ProjectRepository/AssociateProjectGeofence: can't create as already exists projectGeofence={JsonConvert.SerializeObject(projectGeofence)}"); return(upsertedCount); }
private async Task <int> DissociateProjectGeofence(ProjectGeofence projectGeofence, ProjectGeofence existing) { var upsertedCount = 0; Log.LogDebug( $"ProjectRepository/DissociateProjectGeofence: projectGeofence={JsonConvert.SerializeObject(projectGeofence)} existing={JsonConvert.SerializeObject(existing)}"); if (existing != null) { if (projectGeofence.LastActionedUTC >= existing.LastActionedUTC) { const string delete = @"DELETE FROM ProjectGeofence WHERE fk_GeofenceUID = @GeofenceUID AND fk_ProjectUID = @ProjectUID"; upsertedCount = await ExecuteWithAsyncPolicy(delete, projectGeofence); Log.LogDebug( $"ProjectRepository/DissociateProjectGeofence: upserted {upsertedCount} rows for: geofenceUid:{projectGeofence.GeofenceUID}"); return(upsertedCount); } // may have been associated again since, so don't delete Log.LogDebug("ProjectRepository/DissociateProjectGeofence: old delete event ignored"); } else { Log.LogDebug("ProjectRepository/DissociateProjectGeofence: can't delete as none existing"); } return(upsertedCount); }
public async Task GetBoundaryExecutor() { string custUid = Guid.NewGuid().ToString(); string userUid = Guid.NewGuid().ToString(); string projectUid = Guid.NewGuid().ToString(); string boundaryUid = Guid.NewGuid().ToString(); string name = "blah"; string geometryWKT = "whatever"; var configStore = serviceProvider.GetRequiredService <IConfigurationStore>(); var logger = serviceProvider.GetRequiredService <ILoggerFactory>(); var geofenceRepo = new Mock <GeofenceRepository>(configStore, logger); var geofence = new Geofence { CustomerUID = custUid, UserUID = userUid, GeofenceUID = boundaryUid, Name = name, GeometryWKT = geometryWKT, GeofenceType = GeofenceType.Filter, LastActionedUTC = DateTime.UtcNow }; geofenceRepo.As <IGeofenceRepository>().Setup(g => g.GetGeofence(It.IsAny <string>())).ReturnsAsync(geofence); var projectRepo = new Mock <ProjectRepository>(configStore, logger); var projectGeofence = new ProjectGeofence { GeofenceUID = boundaryUid, ProjectUID = projectUid }; projectRepo.As <IProjectRepository>().Setup(p => p.GetAssociatedGeofences(It.IsAny <string>())).ReturnsAsync(new List <ProjectGeofence> { projectGeofence }); var geofenceToTest = new GeofenceDataSingleResult(AutoMapperUtility.Automapper.Map <GeofenceData>(geofence)); var request = BoundaryUidRequestFull.Create ( custUid, false, new ProjectData { ProjectUID = projectUid }, userUid, boundaryUid ); var executor = RequestExecutorContainer.Build <GetBoundaryExecutor>(configStore, logger, serviceExceptionHandler, geofenceRepo.Object, projectRepo.Object); var result = await executor.ProcessAsync(request) as GeofenceDataSingleResult; Assert.NotNull(result); Assert.Equal(geofenceToTest.GeofenceData.GeofenceUID, result.GeofenceData.GeofenceUID); Assert.Equal(geofenceToTest.GeofenceData.GeofenceName, result.GeofenceData.GeofenceName); Assert.Equal(geofenceToTest.GeofenceData.GeometryWKT, result.GeofenceData.GeometryWKT); }
private async Task <int> CreateGeofenceAndAssociation(ProjectDataModel project) { var geofence = new Geofence().Setup(); geofence.GeofenceUID = Guid.NewGuid().ToString(); geofence.Name = project.Name; geofence.GeofenceType = GeofenceType.Project; geofence.GeometryWKT = project.Boundary; geofence.CustomerUID = project.CustomerUID; var area = GeofenceValidation.CalculateAreaSqMeters(project.Boundary); geofence.AreaSqMeters = area > 1000000000 ? 0 : area; geofence.IsDeleted = false; geofence.LastActionedUTC = DateTime.UtcNow; string formattedPolygon = RepositoryHelper.WKTToSpatial(project.Boundary); string insert = string.Format( "INSERT Geofence " + " (GeofenceUID, Name, Description, PolygonST, FillColor, IsTransparent, IsDeleted, fk_CustomerUID, UserUID, LastActionedUTC, fk_GeofenceTypeID, AreaSqMeters) " + " VALUES " + " (@GeofenceUID, @Name, @Description, {0}, @FillColor, @IsTransparent, @IsDeleted, @CustomerUID, @UserUID, @LastActionedUTC, @GeofenceType, @AreaSqMeters)", formattedPolygon); var upsertedCount = await ExecuteWithAsyncPolicy(insert, geofence); Log.LogDebug( $"ProjectRepository/UpsertGeofence inserted. upsertedCount {upsertedCount} rows for: geofenceUid:{geofence.GeofenceUID}"); if (upsertedCount == 1) { var projectGeofence = new ProjectGeofence() { ProjectUID = project.ProjectUID, GeofenceUID = geofence.GeofenceUID, LastActionedUTC = DateTime.UtcNow }; await AssociateProjectGeofence(projectGeofence, null); return(upsertedCount); } return(0); }
private async Task <int> UpsertProjectGeofenceDetail(ProjectGeofence projectGeofence, string eventType) { var upsertedCount = 0; var existing = (await QueryWithAsyncPolicy <ProjectGeofence> (@"SELECT fk_GeofenceUID AS GeofenceUID, fk_ProjectUID AS ProjectUID, LastActionedUTC FROM ProjectGeofence WHERE fk_ProjectUID = @ProjectUID AND fk_GeofenceUID = @GeofenceUID", new { projectGeofence.ProjectUID, projectGeofence.GeofenceUID } )).FirstOrDefault(); if (eventType == "AssociateProjectGeofenceEvent") { upsertedCount = await AssociateProjectGeofence(projectGeofence, existing); } if (eventType == "DissociateProjectGeofenceEvent") { upsertedCount = await DissociateProjectGeofence(projectGeofence, existing); } return(upsertedCount); }
public async Task <int> StoreEvent(IProjectEvent evt) { var upsertedCount = 0; if (evt == null) { Log.LogWarning("Unsupported project event type"); return(0); } Log.LogDebug($"Event type is {evt.GetType()}"); if (evt is CreateImportedFileEvent) { var projectEvent = (CreateImportedFileEvent)evt; var importedFile = new ImportedFile { ProjectUid = projectEvent.ProjectUID.ToString(), ImportedFileUid = projectEvent.ImportedFileUID.ToString(), ImportedFileId = projectEvent.ImportedFileID, CustomerUid = projectEvent.CustomerUID.ToString(), ImportedFileType = projectEvent.ImportedFileType, Name = projectEvent.Name, FileDescriptor = projectEvent.FileDescriptor, FileCreatedUtc = projectEvent.FileCreatedUtc, FileUpdatedUtc = projectEvent.FileUpdatedUtc, ImportedBy = projectEvent.ImportedBy, SurveyedUtc = projectEvent.SurveyedUTC, DxfUnitsType = projectEvent.DxfUnitsType, MinZoomLevel = projectEvent.MinZoomLevel, MaxZoomLevel = projectEvent.MaxZoomLevel, LastActionedUtc = projectEvent.ActionUTC, ParentUid = projectEvent.ParentUID?.ToString(), Offset = projectEvent.Offset }; upsertedCount = await UpsertImportedFile(importedFile, "CreateImportedFileEvent"); } else if (evt is UpdateImportedFileEvent) { var projectEvent = (UpdateImportedFileEvent)evt; var importedFile = new ImportedFile { ProjectUid = projectEvent.ProjectUID.ToString(), ImportedFileUid = projectEvent.ImportedFileUID.ToString(), FileDescriptor = projectEvent.FileDescriptor, FileCreatedUtc = projectEvent.FileCreatedUtc, FileUpdatedUtc = projectEvent.FileUpdatedUtc, ImportedBy = projectEvent.ImportedBy, SurveyedUtc = projectEvent.SurveyedUtc, MinZoomLevel = projectEvent.MinZoomLevel, MaxZoomLevel = projectEvent.MaxZoomLevel, LastActionedUtc = projectEvent.ActionUTC, Offset = projectEvent.Offset }; upsertedCount = await UpsertImportedFile(importedFile, "UpdateImportedFileEvent"); } else if (evt is DeleteImportedFileEvent) { var projectEvent = (DeleteImportedFileEvent)evt; var importedFile = new ImportedFile { ProjectUid = projectEvent.ProjectUID.ToString(), ImportedFileUid = projectEvent.ImportedFileUID.ToString(), LastActionedUtc = projectEvent.ActionUTC }; upsertedCount = await UpsertImportedFile(importedFile, "DeleteImportedFileEvent", projectEvent.DeletePermanently); } else if (evt is UndeleteImportedFileEvent) { var projectEvent = (UndeleteImportedFileEvent)evt; var importedFile = new ImportedFile { ProjectUid = projectEvent.ProjectUID.ToString(), ImportedFileUid = projectEvent.ImportedFileUID.ToString(), LastActionedUtc = projectEvent.ActionUTC }; upsertedCount = await UpsertImportedFile(importedFile, "UndeleteImportedFileEvent"); } else if (evt is UpdateProjectSettingsEvent projectEvent) { var projectSettings = new ProjectSettings { ProjectUid = projectEvent.ProjectUID.ToString(), ProjectSettingsType = projectEvent.ProjectSettingsType, Settings = projectEvent.Settings, UserID = projectEvent.UserID, LastActionedUtc = projectEvent.ActionUTC }; upsertedCount = await UpsertProjectSettings(projectSettings); } // this geofence code is used by FilterSvc and refer to tables solely in the FilterSvc database (not the ProjectSvc one). else if (evt is AssociateProjectGeofence) { var projectGeofenceEvent = (AssociateProjectGeofence)evt; var projectGeofence = new ProjectGeofence { ProjectUID = projectGeofenceEvent.ProjectUID.ToString(), GeofenceUID = projectGeofenceEvent.GeofenceUID.ToString(), LastActionedUTC = projectGeofenceEvent.ActionUTC }; upsertedCount = await UpsertProjectGeofenceDetail(projectGeofence, "AssociateProjectGeofenceEvent"); } else if (evt is DissociateProjectGeofence) { var projectGeofenceEvent = (DissociateProjectGeofence)evt; var projectGeofence = new ProjectGeofence { ProjectUID = projectGeofenceEvent.ProjectUID.ToString(), GeofenceUID = projectGeofenceEvent.GeofenceUID.ToString(), LastActionedUTC = projectGeofenceEvent.ActionUTC }; upsertedCount = await UpsertProjectGeofenceDetail(projectGeofence, "DissociateProjectGeofenceEvent"); } return(upsertedCount); }
public async Task DeleteBoundaryExecutor() { string custUid = Guid.NewGuid().ToString(); string userUid = Guid.NewGuid().ToString(); string projectUid = Guid.NewGuid().ToString(); string boundaryUid = Guid.NewGuid().ToString(); string name = "not entry"; string geometryWKT = "whatever"; var configStore = serviceProvider.GetRequiredService <IConfigurationStore>(); var logger = serviceProvider.GetRequiredService <ILoggerFactory>(); var geofenceRepo = new Mock <GeofenceRepository>(configStore, logger); var geofence = new Geofence { CustomerUID = custUid, UserUID = userUid, GeofenceUID = boundaryUid, Name = name, GeometryWKT = geometryWKT, GeofenceType = GeofenceType.Filter, LastActionedUTC = DateTime.UtcNow }; geofenceRepo.As <IGeofenceRepository>().Setup(g => g.GetGeofence(It.IsAny <string>())).ReturnsAsync(geofence); geofenceRepo.As <IGeofenceRepository>().Setup(g => g.StoreEvent(It.IsAny <DeleteGeofenceEvent>())).ReturnsAsync(1); var projectRepo = new Mock <ProjectRepository>(configStore, logger); var projectGeofence = new ProjectGeofence { GeofenceUID = boundaryUid, ProjectUID = projectUid }; projectRepo.As <IProjectRepository>().Setup(p => p.GetAssociatedGeofences(It.IsAny <string>())).ReturnsAsync(new List <ProjectGeofence> { projectGeofence }); var productivity3dV2ProxyNotification = new Mock <IProductivity3dV2ProxyNotification>(); productivity3dV2ProxyNotification.Setup(ps => ps.NotifyFilterChange(It.IsAny <Guid>(), It.IsAny <Guid>(), null)).ReturnsAsync(new BaseMasterDataResult()); var request = BoundaryUidRequestFull.Create ( custUid, false, new ProjectData { ProjectUID = projectUid }, userUid, boundaryUid ); var executor = RequestExecutorContainer.Build <DeleteBoundaryExecutor>(configStore, logger, serviceExceptionHandler, geofenceRepo.Object, projectRepo.Object, productivity3dV2ProxyNotification: productivity3dV2ProxyNotification.Object); var result = await executor.ProcessAsync(request); Assert.NotNull(result); Assert.Equal(0, result.Code); Assert.Equal("success", result.Message); }
public async Task GetBoundariesExecutor_WithException() { string custUid = Guid.NewGuid().ToString(); string userUid = Guid.NewGuid().ToString(); string projectUid = Guid.NewGuid().ToString(); string boundaryUid = Guid.NewGuid().ToString(); string boundaryName = "blah"; string boundaryGeometryWKT = "whatever"; var configStore = serviceProvider.GetRequiredService <IConfigurationStore>(); var logger = serviceProvider.GetRequiredService <ILoggerFactory>(); var geofenceRepo = new Mock <GeofenceRepository>(configStore, logger); var geofenceBoundary = new Geofence { CustomerUID = custUid, UserUID = userUid, GeofenceUID = boundaryUid, Name = boundaryName, GeometryWKT = boundaryGeometryWKT, GeofenceType = GeofenceType.Filter, LastActionedUTC = DateTime.UtcNow }; geofenceRepo.As <IGeofenceRepository>().Setup(g => g.GetGeofences(It.IsAny <IEnumerable <string> >())).ReturnsAsync(new List <Geofence> { geofenceBoundary }); //var geofenceProxy = new Mock<IGeofenceProxy>(); //geofenceProxy.Setup(g => g.GetFavoriteGeofences(custUid, userUid, null)).ReturnsAsync(new List<GeofenceData>()); //var upProxy = new Mock<IUnifiedProductivityProxy>(); //upProxy.Setup(u => u.GetAssociatedGeofences(projectUid, null)).Throws(new Exception("No UserCustomerAssociation exists.")); var projectRepo = new Mock <ProjectRepository>(configStore, logger); var projectGeofence = new ProjectGeofence { GeofenceUID = boundaryUid, ProjectUID = projectUid }; projectRepo.As <IProjectRepository>().Setup(p => p.GetAssociatedGeofences(It.IsAny <string>())).ReturnsAsync(new List <ProjectGeofence> { projectGeofence }); //projectRepo.As<IProjectRepository>().Setup(p => p.DoPolygonsOverlap(It.IsAny<string>(), It.IsAny<IEnumerable<string>>())).ReturnsAsync(new List<bool> { true, false }); var geofenceToTest = AutoMapperUtility.Automapper.Map <GeofenceData>(geofenceBoundary); var request = BaseRequestFull.Create ( custUid, false, new ProjectData { ProjectUID = projectUid }, userUid, null ); var executor = RequestExecutorContainer.Build <GetBoundariesExecutor>(configStore, logger, serviceExceptionHandler, geofenceRepo.Object, projectRepo.Object /* , geofenceProxy: geofenceProxy.Object, unifiedProductivityProxy: upProxy.Object */); var result = await executor.ProcessAsync(request) as GeofenceDataListResult; Assert.NotNull(result); Assert.Single(result.GeofenceData); var actualBoundary = result.GeofenceData.SingleOrDefault(g => g.GeofenceUID == geofenceToTest.GeofenceUID); Assert.Equal(geofenceToTest.GeofenceUID, actualBoundary.GeofenceUID); Assert.Equal(geofenceToTest.GeofenceName, actualBoundary.GeofenceName); Assert.Equal(geofenceToTest.GeometryWKT, actualBoundary.GeometryWKT); Assert.Equal(geofenceToTest.GeofenceType, actualBoundary.GeofenceType); }