From ff19123c86a2c1acde7e17f19d7d747e80e9d90c Mon Sep 17 00:00:00 2001 From: alexsilva Date: Wed, 14 Jun 2023 13:18:35 +0100 Subject: [PATCH] fix(gate): Added changelog to Logistic Address when Legal Entity or Site are Update/Created --- .../gate/service/LegalEntityPersistenceService.kt | 13 ++++++++++++- .../bpdm/gate/service/LegalEntityService.kt | 11 ++++------- .../bpdm/gate/service/SitePersistenceService.kt | 13 ++++++++++++- .../tractusx/bpdm/gate/service/SiteService.kt | 9 +-------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt index 043ff7fac..4abee05d2 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt @@ -25,7 +25,9 @@ import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum import org.eclipse.tractusx.bpdm.common.util.replace import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputRequest +import org.eclipse.tractusx.bpdm.gate.api.model.LsaType import org.eclipse.tractusx.bpdm.gate.entity.* +import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository import org.springframework.http.HttpStatus @@ -36,7 +38,8 @@ import org.springframework.web.server.ResponseStatusException @Service class LegalEntityPersistenceService( private val gateLegalEntityRepository: LegalEntityRepository, - private val gateAddressRepository: GateAddressRepository + private val gateAddressRepository: GateAddressRepository, + private val changelogRepository: ChangelogRepository ) { @Transactional @@ -58,12 +61,20 @@ class LegalEntityPersistenceService( updateLegalEntity(existingLegalEntity, legalEntity, logisticAddressRecord) gateLegalEntityRepository.save(existingLegalEntity) + saveChangelog(legalEntity) } ?: run { gateLegalEntityRepository.save(fullLegalEntity) + saveChangelog(legalEntity) } } } + //Creates Changelog For both Legal Entity and Logistic Address when they are created or updated + private fun saveChangelog(legalEntity: LegalEntityGateInputRequest) { + changelogRepository.save(ChangelogEntry(getMainAddressForLegalEntityExternalId(legalEntity.externalId), LsaType.ADDRESS)) + changelogRepository.save(ChangelogEntry(legalEntity.externalId, LsaType.LEGAL_ENTITY)) + } + private fun updateLegalEntity( legalEntity: LegalEntity, legalEntityRequest: LegalEntityGateInputRequest, diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt index df6739017..09d580da9 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt @@ -23,10 +23,11 @@ import mu.KotlinLogging import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum -import org.eclipse.tractusx.bpdm.gate.api.model.* -import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputRequest +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputResponse import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity -import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest @@ -34,7 +35,6 @@ import org.springframework.stereotype.Service @Service class LegalEntityService( - private val changelogRepository: ChangelogRepository, private val legalEntityPersistenceService: LegalEntityPersistenceService, private val legalEntityRepository: LegalEntityRepository ) { @@ -46,9 +46,6 @@ class LegalEntityService( **/ fun upsertLegalEntities(legalEntities: Collection) { - legalEntities.forEach { legalEntity -> - changelogRepository.save(ChangelogEntry(legalEntity.externalId, LsaType.LEGAL_ENTITY)) - } legalEntityPersistenceService.persistLegalEntitiesBP(legalEntities, OutputInputEnum.Input) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt index 9cb3895c6..7c689f718 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt @@ -22,9 +22,11 @@ package org.eclipse.tractusx.bpdm.gate.service import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum import org.eclipse.tractusx.bpdm.common.util.replace +import org.eclipse.tractusx.bpdm.gate.api.model.LsaType import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputRequest import org.eclipse.tractusx.bpdm.gate.entity.* +import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository @@ -37,7 +39,8 @@ import org.springframework.web.server.ResponseStatusException class SitePersistenceService( private val siteRepository: SiteRepository, private val legalEntityRepository: LegalEntityRepository, - private val addressRepository: GateAddressRepository + private val addressRepository: GateAddressRepository, + private val changelogRepository: ChangelogRepository ) { @Transactional @@ -59,12 +62,20 @@ class SitePersistenceService( updateAddress(logisticAddressRecord, fullSite.mainAddress) updateSite(existingSite, site, legalEntityRecord) siteRepository.save(existingSite) + saveChangelog(site) } ?: run { siteRepository.save(fullSite) + saveChangelog(site) } } } + //Creates Changelog For both Site and Logistic Address when they are created or updated + private fun saveChangelog(site: SiteGateInputRequest) { + changelogRepository.save(ChangelogEntry(getMainAddressForSiteExternalId(site.externalId), LsaType.ADDRESS)) + changelogRepository.save(ChangelogEntry(site.externalId, LsaType.SITE)) + } + private fun getAddressRecord(externalId: String, datatype: OutputInputEnum): LogisticAddress { return addressRepository.findByExternalIdAndDataType(externalId, datatype) ?: throw BpdmNotFoundException("Business Partner", "Error") diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt index 0fed808c6..f2921001f 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt @@ -23,17 +23,14 @@ import mu.KotlinLogging import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException -import org.eclipse.tractusx.bpdm.gate.api.model.LsaType import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties -import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry import org.eclipse.tractusx.bpdm.gate.entity.Site import org.eclipse.tractusx.bpdm.gate.exception.SaasNonexistentParentException -import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest @@ -44,7 +41,6 @@ class SiteService( private val saasRequestMappingService: SaasRequestMappingService, private val saasClient: SaasClient, private val bpnConfigProperties: BpnConfigProperties, - private val changelogRepository: ChangelogRepository, private val sitePersistenceService: SitePersistenceService, private val siteRepository: SiteRepository ) { @@ -111,11 +107,8 @@ class SiteService( **/ fun upsertSites(sites: Collection) { - sites.forEach { site -> - changelogRepository.save(ChangelogEntry(site.externalId, LsaType.SITE)) - } - sitePersistenceService.persistSitesBP(sites, OutputInputEnum.Input) + } /**