Skip to content

Commit

Permalink
1644: Return error if duplicated stores found in csv import
Browse files Browse the repository at this point in the history
  • Loading branch information
seluianova committed Feb 19, 2025
1 parent 8db31eb commit 981cba5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package app.ehrenamtskarte.backend.exception.service

class NotEakProjectException : Exception("This query can only be used for EAK project")
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.ehrenamtskarte.backend.common.webservice.GraphQLContext
import app.ehrenamtskarte.backend.exception.service.ForbiddenException
import app.ehrenamtskarte.backend.exception.service.ProjectNotFoundException
import app.ehrenamtskarte.backend.exception.service.UnauthorizedException
import app.ehrenamtskarte.backend.exception.webservice.exceptions.InvalidJsonException
import app.ehrenamtskarte.backend.projects.database.ProjectEntity
import app.ehrenamtskarte.backend.projects.database.Projects
import app.ehrenamtskarte.backend.stores.database.repos.AcceptingStoresRepository
Expand All @@ -22,21 +23,30 @@ class AcceptingStoresMutationService {
val context = dfe.getContext<GraphQLContext>()
val jwtPayload = context.enforceSignedIn()
return transaction {
val projectEntity =
ProjectEntity.find { Projects.project eq project }.firstOrNull() ?: throw ProjectNotFoundException(
project
)
val projectEntity = ProjectEntity.find { Projects.project eq project }.firstOrNull()
?: throw ProjectNotFoundException(project)
val user = AdministratorEntity.findById(jwtPayload.adminId)
?: throw UnauthorizedException()

if (!Authorizer.mayUpdateStoresInProject(user, projectEntity.id.value)) {
throw ForbiddenException()
}

checkForDuplicates(stores)

val projectId = projectEntity.id
val (storesCreated, storesDeleted, storesUntouched) = AcceptingStoresRepository.importAcceptingStores(stores, projectId, dryRun)

return@transaction StoreImportReturnResultModel(storesCreated, storesDeleted, storesUntouched)
}
}

private fun checkForDuplicates(stores: List<CSVAcceptingStore>) {
val seen = mutableSetOf<String>()
stores.firstOrNull {
!seen.add(it.name + it.street + it.houseNumber + it.postalCode + it.location)
}?.let { store ->
throw InvalidJsonException("Duplicate store found: ${store.name} ${store.street} ${store.houseNumber} ${store.postalCode} ${store.location}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ internal class ImportAcceptingStoresTest : GraphqlApiTest() {
}

@Test
fun `POST returns a successful response if two duplicate acceptance stores are submitted`() = JavalinTest.test(app) { _, client ->
fun `POST returns an error if two duplicate acceptance stores are submitted`() = JavalinTest.test(app) { _, client ->
val csvStore = createAcceptingStoreInput(
name = "Test store",
street = "Teststr.",
Expand All @@ -227,18 +227,8 @@ internal class ImportAcceptingStoresTest : GraphqlApiTest() {

val jsonResponse = response.json()

jsonResponse.apply {
assertEquals(1, findValue("storesCreated").intValue())
assertEquals(0, findValue("storesDeleted").intValue())
assertEquals(1, findValue("storesUntouched").intValue())
}

transaction {
assertEquals(1, AcceptingStores.selectAll().count())
assertEquals(1, Contacts.selectAll().count())
assertEquals(1, Addresses.selectAll().count())
assertEquals(1, PhysicalStores.selectAll().count())
}
assertEquals("Error INVALID_JSON occurred.", jsonResponse.findValue("message").textValue())
assertEquals("Duplicate store found: Test store Teststr. 10 90408 Nürnberg", jsonResponse.findValue("reason").textValue())
}

@Test
Expand Down

0 comments on commit 981cba5

Please sign in to comment.