Skip to content

Commit

Permalink
BE: Delete imports on useNamespaces change
Browse files Browse the repository at this point in the history
  • Loading branch information
StaNov committed Dec 18, 2024
1 parent 1d628bd commit ea6aebb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.tolgee.events

import io.tolgee.dtos.cacheable.ProjectDto

data class OnProjectSettingsUpdated(
val original: ProjectDto?,
val updated: ProjectDto,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -69,6 +71,7 @@ class ProjectService(
@Lazy
private val batchJobService: BatchJobService,
private val currentDateProvider: CurrentDateProvider,
private val eventPublisher: ApplicationEventPublisher,
) : Logging {
@set:Autowired
@set:Lazy
Expand Down Expand Up @@ -406,17 +409,19 @@ class ProjectService(
return addPermittedLanguagesToProjects(withoutPermittedLanguages, userAccountId)
}

@CacheEvict(cacheNames = [Caches.PROJECTS], allEntries = true)
fun saveAll(projects: Collection<Project>): MutableList<Project> = 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
}

Expand Down

0 comments on commit ea6aebb

Please sign in to comment.