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 15e576c commit bbec8e7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,23 @@ 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()

val project = projectService.get(base.project.id)
project.useNamespaces = !project.useNamespaces
projectService.save(project)
commitTransaction()

assertThat(importService.getAllByProject(project.id)).isEmpty()
}

private fun validateSavedJsonImportData(
project: Project,
userAccount: UserAccount,
Expand Down
1 change: 1 addition & 0 deletions backend/data/src/main/kotlin/io/tolgee/model/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Project(
override var icuPlaceholders: Boolean = true

@ColumnDefault("false")
@ActivityLoggedProp
var useNamespaces: Boolean = false

@ColumnDefault("0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,29 @@ import io.tolgee.dtos.dataImport.ImportAddFilesParams
import io.tolgee.dtos.dataImport.ImportFileDto
import io.tolgee.dtos.request.SingleStepImportRequest
import io.tolgee.events.OnImportSoftDeleted
import io.tolgee.events.OnProjectActivityEvent
import io.tolgee.exceptions.BadRequestException
import io.tolgee.exceptions.ErrorResponseBody
import io.tolgee.exceptions.ImportConflictNotResolvedException
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.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,20 @@ class ImportService(
dataManager.resetCollisionsBetweenFiles(language, null)
}

@EventListener
fun deleteExistingImportsOnUseNamespacesModification(event: OnProjectActivityEvent) {
val modifiedProjects = event.modifiedEntities[Project::class]

modifiedProjects
?.values
?.filter { project -> project.modifications.containsKey("useNamespaces") }
?.forEach { project ->
getAllByProject(project.entityId).forEach { import ->
deleteImport(import)
}
}
}

fun findTranslation(translationId: Long): ImportTranslation? {
return importTranslationRepository.findById(translationId).orElse(null)
}
Expand Down

0 comments on commit bbec8e7

Please sign in to comment.