Skip to content

Commit

Permalink
feat: Provide filtering by key tags when getting all translations (#2660
Browse files Browse the repository at this point in the history
)

Closes #2453.

---------

Co-authored-by: StaNov <[email protected]>
  • Loading branch information
StaNov and StaNov authored Nov 15, 2024
1 parent 5ac09e5 commit ff1344c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class TranslationsController(
description =
"Comma-separated language tags to return translations in. " +
"Languages you are not permitted to see will be silently dropped and not returned.",
example = "en,de,fr",
example = """["en","de","fr"]""",
)
@PathVariable("languages")
languages: Set<String>,
Expand All @@ -148,6 +148,15 @@ When null, resulting file will be a flat key-value object.
)
@RequestParam(value = "structureDelimiter", defaultValue = ".", required = false)
structureDelimiter: Char?,
@Parameter(
description =
"Enables filtering of returned keys by their tags.\n" +
"Only keys with at least one provided tag will be returned.\n" +
"Optional, filtering is not applied if not specified.",
example = """["productionReady", "nextRelease"]""",
)
@RequestParam(value = "filterTag", required = false)
filterTag: List<String>? = null,
request: WebRequest,
): ResponseEntity<Map<String, Any>>? {
val lastModified: Long = projectTranslationLastModifiedManager.getLastModified(projectHolder.project.id)
Expand All @@ -166,6 +175,7 @@ When null, resulting file will be a flat key-value object.
namespace = ns,
projectId = projectHolder.project.id,
structureDelimiter = request.getStructureDelimiter(),
filterTag = filterTag,
)

return ResponseEntity.ok()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ class TranslationsControllerViewTest : ProjectAuthControllerTest("/v2/projects/"
}
}

@ProjectJWTAuthTestMethod
@Test
fun `returns keys filtered by provided tags`() {
testData.addFewKeysWithTags()
testDataService.saveTestData(testData.root)
userAccount = testData.user
performProjectAuthGet("/translations/en,de?filterTag=Another cool tag&filterTag=Unknown Tag")
.andPrettyPrint.andIsOk.andAssertThatJson {
node("en")
.isObject
.containsOnlyKeys("Another key with tag")
node("de")
.isObject
.containsOnlyKeys("Another key with tag")
}
}

@ProjectApiKeyAuthTestMethod
@Test
fun `works with API key`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,40 @@ class TranslationsTestData {
name = "Key with tag"
}.build {
addTag("Cool tag")
addTranslation {
language = germanLanguage
text = "Key with tag DE"
}
addTranslation {
language = englishLanguage
text = "Key with tag EN"
}
}
addKey {
name = "Another key with tag"
}.build {
addTag("Another cool tag")
addTranslation {
language = germanLanguage
text = "Another key with tag DE"
}
addTranslation {
language = englishLanguage
text = "Another key with tag EN"
}
}
addKey {
name = "Key with tag 2"
}.build {
addTag("Cool tag")
addTranslation {
language = germanLanguage
text = "Key with tag 2 DE"
}
addTranslation {
language = englishLanguage
text = "Key with tag 2 EN"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ interface TranslationRepository : JpaRepository<Translation, Long> {
from Translation t
join t.key k
left join k.namespace n
left join k.keyMeta km
left join km.tags kmt
join t.language l
where t.key.project.id = :projectId
and l.tag in :languages
and ((n.name is null and :namespace is null) or n.name = :namespace)
and (:filterTags is null or kmt.name in :filterTags)
group by t.id, l.tag, k.name
order by k.name
""",
)
fun getTranslations(
languages: Set<String>,
namespace: String?,
projectId: Long,
filterTags: List<String>?,
): List<SimpleTranslationView>

@Query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ class TranslationService(
namespace: String?,
projectId: Long,
structureDelimiter: Char?,
filterTag: List<String>? = null,
): Map<String, Any> {
val safeNamespace = if (namespace == "") null else namespace
val allByLanguages = translationRepository.getTranslations(languageTags, safeNamespace, projectId)
val allByLanguages = translationRepository.getTranslations(languageTags, safeNamespace, projectId, filterTag)
val langTranslations: HashMap<String, Any> = LinkedHashMap()
for (translation in allByLanguages) {
val map =
Expand Down

0 comments on commit ff1344c

Please sign in to comment.