From bc71869bbd5df48c21976abf00373c4fc49e5075 Mon Sep 17 00:00:00 2001 From: StaNov Date: Tue, 17 Dec 2024 17:48:24 +0100 Subject: [PATCH] BE: Delete imports on useNamespaces change --- .../V2ImportControllerAddFilesTest.kt | 15 +++++++++++ .../tolgee/events/OnProjectSettingsUpdated.kt | 8 ++++++ .../service/dataImport/ImportService.kt | 25 +++++++++++-------- .../tolgee/service/project/ProjectService.kt | 11 +++++--- 4 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 backend/data/src/main/kotlin/io/tolgee/events/OnProjectSettingsUpdated.kt diff --git a/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/v2ImportController/V2ImportControllerAddFilesTest.kt b/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/v2ImportController/V2ImportControllerAddFilesTest.kt index c60bfe83fd..f33a0787d4 100644 --- a/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/v2ImportController/V2ImportControllerAddFilesTest.kt +++ b/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/v2ImportController/V2ImportControllerAddFilesTest.kt @@ -348,6 +348,21 @@ class V2ImportControllerAddFilesTest : ProjectAuthControllerTest("/v2/projects/" } } + @Test + fun `import gets deleted after namespaces feature is toggled`() { + val base = dbPopulator.createBase() + + performImport(projectId = base.project.id, listOf("simple.json" to simpleJson)) + .andIsOk + + assertThat(importService.getAllByProject(base.project.id)).isNotEmpty() + + base.project.useNamespaces = !base.project.useNamespaces + projectService.save(base.project) + + assertThat(importService.getAllByProject(base.project.id)).isEmpty() + } + private fun validateSavedJsonImportData( project: Project, userAccount: UserAccount, diff --git a/backend/data/src/main/kotlin/io/tolgee/events/OnProjectSettingsUpdated.kt b/backend/data/src/main/kotlin/io/tolgee/events/OnProjectSettingsUpdated.kt new file mode 100644 index 0000000000..1e328f930f --- /dev/null +++ b/backend/data/src/main/kotlin/io/tolgee/events/OnProjectSettingsUpdated.kt @@ -0,0 +1,8 @@ +package io.tolgee.events + +import io.tolgee.dtos.cacheable.ProjectDto + +data class OnProjectSettingsUpdated( + val original: ProjectDto?, + val updated: ProjectDto, +) diff --git a/backend/data/src/main/kotlin/io/tolgee/service/dataImport/ImportService.kt b/backend/data/src/main/kotlin/io/tolgee/service/dataImport/ImportService.kt index 0d456bbc9b..a45bf5b7f7 100644 --- a/backend/data/src/main/kotlin/io/tolgee/service/dataImport/ImportService.kt +++ b/backend/data/src/main/kotlin/io/tolgee/service/dataImport/ImportService.kt @@ -19,28 +19,22 @@ import io.tolgee.exceptions.NotFoundException import io.tolgee.model.Language import io.tolgee.model.Project import io.tolgee.model.UserAccount -import io.tolgee.model.dataImport.Import -import io.tolgee.model.dataImport.ImportFile -import io.tolgee.model.dataImport.ImportKey -import io.tolgee.model.dataImport.ImportLanguage -import io.tolgee.model.dataImport.ImportTranslation +import io.tolgee.model.dataImport.* import io.tolgee.model.dataImport.issues.ImportFileIssue import io.tolgee.model.dataImport.issues.ImportFileIssueParam import io.tolgee.model.views.ImportFileIssueView import io.tolgee.model.views.ImportLanguageView import io.tolgee.model.views.ImportTranslationView -import io.tolgee.repository.dataImport.ImportFileRepository -import io.tolgee.repository.dataImport.ImportKeyRepository -import io.tolgee.repository.dataImport.ImportLanguageRepository -import io.tolgee.repository.dataImport.ImportRepository -import io.tolgee.repository.dataImport.ImportTranslationRepository +import io.tolgee.repository.dataImport.* import io.tolgee.repository.dataImport.issues.ImportFileIssueParamRepository import io.tolgee.repository.dataImport.issues.ImportFileIssueRepository import io.tolgee.service.dataImport.status.ImportApplicationStatus +import io.tolgee.events.OnProjectSettingsUpdated import io.tolgee.util.getSafeNamespace import jakarta.persistence.EntityManager import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Lazy +import org.springframework.context.event.EventListener import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.jdbc.core.JdbcTemplate @@ -413,6 +407,17 @@ class ImportService( dataManager.resetCollisionsBetweenFiles(language, null) } + @EventListener + fun deleteExistingImportsOnUseNamespacesChange(event: OnProjectSettingsUpdated) { + if (event.original?.useNamespaces == event.updated.useNamespaces) { + return + } + + getAllByProject(event.updated.id).forEach { import -> + deleteImport(import) + } + } + fun findTranslation(translationId: Long): ImportTranslation? { return importTranslationRepository.findById(translationId).orElse(null) } diff --git a/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectService.kt b/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectService.kt index bbbb6f18c3..fad2fc1422 100644 --- a/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectService.kt +++ b/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectService.kt @@ -13,6 +13,7 @@ import io.tolgee.dtos.request.project.ProjectFilters import io.tolgee.dtos.request.validators.exceptions.ValidationException import io.tolgee.dtos.response.ProjectDTO import io.tolgee.dtos.response.ProjectDTO.Companion.fromEntityAndPermission +import io.tolgee.events.OnProjectSettingsUpdated import io.tolgee.exceptions.BadRequestException import io.tolgee.exceptions.NotFoundException import io.tolgee.model.Language @@ -46,6 +47,7 @@ import jakarta.persistence.EntityManager import org.springframework.beans.factory.annotation.Autowired import org.springframework.cache.annotation.CacheEvict import org.springframework.cache.annotation.Cacheable +import org.springframework.context.ApplicationEventPublisher import org.springframework.context.annotation.Lazy import org.springframework.data.domain.Page import org.springframework.data.domain.PageImpl @@ -69,6 +71,7 @@ class ProjectService( @Lazy private val batchJobService: BatchJobService, private val currentDateProvider: CurrentDateProvider, + private val eventPublisher: ApplicationEventPublisher, ) : Logging { @set:Autowired @set:Lazy @@ -406,17 +409,19 @@ class ProjectService( return addPermittedLanguagesToProjects(withoutPermittedLanguages, userAccountId) } - @CacheEvict(cacheNames = [Caches.PROJECTS], allEntries = true) - fun saveAll(projects: Collection): MutableList = projectRepository.saveAll(projects) - @CacheEvict(cacheNames = [Caches.PROJECTS], key = "#result.id") fun save(project: Project): Project { val isCreating = project.id == 0L + val originalProject = if (isCreating) null else ProjectDto.fromEntity(get(project.id)) + projectRepository.save(project) if (isCreating) { projectHolder.project = ProjectDto.fromEntity(project) activityHolder.activityRevision.projectId = projectHolder.project.id } + + val updatedProject = ProjectDto.fromEntity(project) + eventPublisher.publishEvent(OnProjectSettingsUpdated(originalProject, updatedProject)) return project }