Skip to content

Commit

Permalink
feat(gate): Added verification on persistence upon saving output data…
Browse files Browse the repository at this point in the history
…. Show 400 if there isn't an Equal Input persisted
  • Loading branch information
alexsilva-CGI committed Jun 5, 2023
1 parent 2d4b5a5 commit 66f1be2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import org.eclipse.tractusx.bpdm.gate.entity.Site
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

@Service
class AddressPersistenceService(
Expand All @@ -48,16 +50,20 @@ class AddressPersistenceService(

addresses.forEach { address ->

val legalEntityRecord = address.legalEntityExternalId?.let { legalEntityRepository.findByExternalId(it) }
val siteRecord = address.siteExternalId?.let { siteEntityRepository.findByExternalId(it) }
val legalEntityRecord = address.legalEntityExternalId?.let { legalEntityRepository.findByExternalIdAndDataType(it, dataType) }
val siteRecord = address.siteExternalId?.let { siteEntityRepository.findByExternalIdAndDataType(it, dataType) }

val fullAddress = address.toAddressGate(legalEntityRecord, siteRecord, dataType)

addressRecord.find { it.externalId == address.externalId && it.dataType == dataType }?.let { existingAddress ->
updateAddress(existingAddress, address, legalEntityRecord, siteRecord)
gateAddressRepository.save(existingAddress)
} ?: run {
gateAddressRepository.save(fullAddress)
if (fullAddress.dataType == OutputInputEnum.Output && addressRecord.find { it.externalId == fullAddress.externalId && it.dataType == OutputInputEnum.Input } == null) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Input Logistic Address doesn't exist")
} else {
gateAddressRepository.save(fullAddress)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.entity.*
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.server.ResponseStatusException

@Service
class LegalEntityPersistenceService(
Expand All @@ -52,15 +54,16 @@ class LegalEntityPersistenceService(
?: throw BpdmNotFoundException("Business Partner", "Error")

updateAddress(logisticAddressRecord, fullLegalEntity.legalAddress)

updateLegalEntity(existingLegalEntity, legalEntity, logisticAddressRecord)
gateLegalEntityRepository.save(existingLegalEntity)

gateLegalEntityRepository.save(existingLegalEntity)
} ?: run {

gateLegalEntityRepository.save(fullLegalEntity)
if (fullLegalEntity.dataType == OutputInputEnum.Output && legalEntityRecord.find { it.externalId == fullLegalEntity.externalId && it.dataType == OutputInputEnum.Input } == null) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Input Legal Entity doesn't exist")
} else {
gateLegalEntityRepository.save(fullLegalEntity)
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import org.eclipse.tractusx.bpdm.gate.entity.*
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.server.ResponseStatusException

@Service
class SitePersistenceService(
Expand All @@ -49,7 +51,10 @@ class SitePersistenceService(

val legalEntityRecord =
site.legalEntityExternalId.let {
legalEntityRepository.findByExternalId(site.legalEntityExternalId) ?: throw BpdmNotFoundException("Business Partner", it)
legalEntityRepository.findByExternalIdAndDataType(site.legalEntityExternalId, datatype) ?: throw BpdmNotFoundException(
"Business Partner",
it
)
}

val fullSite = site.toSiteGate(legalEntityRecord, datatype)
Expand All @@ -68,7 +73,11 @@ class SitePersistenceService(

siteRepository.save(existingSite)
} ?: run {
siteRepository.save(fullSite)
if (fullSite.dataType == OutputInputEnum.Output && siteRecord.find { it.externalId == fullSite.externalId && it.dataType == OutputInputEnum.Input } == null) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Input Site doesn't exist")
} else {
siteRepository.save(fullSite)
}
}
}
}
Expand Down

0 comments on commit 66f1be2

Please sign in to comment.