From f189335e17d0b6c73cdebb9780d12d47dac3fffb Mon Sep 17 00:00:00 2001 From: kunyao-cofinity-x <138508171+kunyao-cofinity-x@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:25:32 +0200 Subject: [PATCH 1/3] feat: Adding a currentness attribute to the business partner input request --- .../gate/BusinessPartnerNonVerboseValues.kt | 3 +- .../gate/BusinessPartnerVerboseValues.kt | 70 +++++++++++++++++-- .../api/model/IBaseBusinessPartnerGateDto.kt | 3 + .../request/BusinessPartnerInputRequest.kt | 3 +- .../api/model/response/BusinessPartnerDto.kt | 1 + .../response/BusinessPartnerOutputDto.kt | 1 + .../gate/entity/generic/BusinessPartnerDb.kt | 4 ++ .../gate/service/BusinessPartnerMappings.kt | 6 ++ .../gate/service/BusinessPartnerService.kt | 15 +++- .../util/BusinessPartnerComparisonUtil.kt | 1 + .../bpdm/gate/util/BusinessPartnerCopyUtil.kt | 1 + .../bpdm/gate/util/PartnerFileUtil.kt | 4 +- .../V6_1_0_4__add_currentness_column.sql | 2 + .../tractusx/bpdm/gate/auth/AuthTestBase.kt | 2 +- .../controller/BusinessPartnerControllerIT.kt | 29 ++++++++ 15 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql diff --git a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt index ecc4f77d1..c22c32009 100644 --- a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt +++ b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt @@ -36,7 +36,8 @@ object BusinessPartnerNonVerboseValues { val bpInputRequestMinimal = BusinessPartnerInputRequest( externalId = BusinessPartnerVerboseValues.externalId2, - address = bpPostalAddressInputDtoMinimal + address = bpPostalAddressInputDtoMinimal, + currentness = null ) val bpInputRequestFull = BusinessPartnerVerboseValues.bpInputRequestFull diff --git a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt index 0f28d3f24..d00e59262 100644 --- a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt +++ b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt @@ -72,6 +72,10 @@ object BusinessPartnerVerboseValues { const val businessStatusDescription1 = "Active" const val businessStatusDescription2 = "Insolvent" + val currentness1 = "2024-06-28 11:59:00" + val currentness2 = "2024-06-28 12:00:00" + val currentness3 = "2024-06-28 12:01:00" + val businessStatusValidFrom1 = LocalDateTime.of(2020, 1, 1, 0, 0) val businessStatusValidFrom2 = LocalDateTime.of(2019, 1, 1, 0, 0) @@ -331,7 +335,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAddress, physicalPostalAddress = postalAddress2, alternativePostalAddress = alternativeAddressFull - ) + ), + currentness = null ) @@ -631,7 +636,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAndSiteMainAddress, physicalPostalAddress = physicalAddressChina, alternativePostalAddress = AlternativePostalAddressDto() - ) + ), + currentness = null ) val bpInputRequestCleaned = BusinessPartnerInputRequest( @@ -659,7 +665,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAddress, physicalPostalAddress = postalAddress2, alternativePostalAddress = alternativeAddressFull - ) + ), + currentness = null ) val bpInputRequestError = BusinessPartnerInputRequest( @@ -687,7 +694,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAddress, physicalPostalAddress = postalAddress2, alternativePostalAddress = alternativeAddressFull - ) + ), + currentness = null ) val bpOutputDtoCleaned = BusinessPartnerOutputDto( @@ -802,6 +810,60 @@ object BusinessPartnerVerboseValues { updatedAt = Instant.now() ) + val bpInputRequestWithCurrentness1 = BusinessPartnerInputRequest( + externalId = externalId1, + legalEntity = LegalEntityRepresentationInputDto( + legalEntityBpn = "BPNL0000000000XY", + shortName = "short", + legalName = "Limited Liability Company Name", + legalForm = "Limited Liability Company" + ), + address = AddressRepresentationInputDto( + addressBpn = "BPNA0000000001XY", + name = "Address Name", + addressType = null, + physicalPostalAddress = physicalAddressMinimal + ), + currentness = currentness1 + + ) + + val bpInputRequestWithCurrentness2 = BusinessPartnerInputRequest( + externalId = externalId1, + legalEntity = LegalEntityRepresentationInputDto( + legalEntityBpn = "BPNL0000000000XY", + shortName = "short1", + legalName = "Limited Liability Company Name", + legalForm = "Limited Liability Company" + ), + address = AddressRepresentationInputDto( + addressBpn = "BPNA0000000001XY", + name = "Address Name", + addressType = null, + physicalPostalAddress = physicalAddressMinimal + ), + currentness = currentness2 + + ) + + val bpInputRequestWithCurrentness3 = BusinessPartnerInputRequest( + externalId = externalId1, + legalEntity = LegalEntityRepresentationInputDto( + legalEntityBpn = "BPNL0000000000XY", + shortName = "short2", + legalName = "Limited Liability Company Name", + legalForm = "Limited Liability Company" + ), + address = AddressRepresentationInputDto( + addressBpn = "BPNA0000000001XY", + name = "Another address Name", + addressType = null, + physicalPostalAddress = physicalAddressMinimal + ), + currentness = currentness3 + + ) + val now = Instant.now() diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt index c3f129bb6..601ba68ac 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt @@ -30,5 +30,8 @@ interface IBaseBusinessPartnerGateDto : IBaseBusinessPartnerDto { @get:Schema(description = "Indicates whether the sharing member claims (in the initial upload) the business partner to belong to the company data of the sharing member.") val isOwnCompanyData: Boolean + + @get:Schema(description = "The timestamp indicates the last time point of change from the user side") + val currentness: String? } diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt index 538ef9f08..a30d39bcd 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt @@ -42,6 +42,7 @@ data class BusinessPartnerInputRequest( override val isOwnCompanyData: Boolean = false, override val legalEntity: LegalEntityRepresentationInputDto = LegalEntityRepresentationInputDto(), override val site: SiteRepresentationInputDto = SiteRepresentationInputDto(), - override val address: AddressRepresentationInputDto = AddressRepresentationInputDto() + override val address: AddressRepresentationInputDto = AddressRepresentationInputDto(), + override val currentness: String? ) : IBaseBusinessPartnerGateDto diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt index 497ade6a8..f7f50dd4c 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt @@ -41,6 +41,7 @@ data class BusinessPartnerInputDto( override val legalEntity: LegalEntityRepresentationInputDto = LegalEntityRepresentationInputDto(), override val site: SiteRepresentationInputDto = SiteRepresentationInputDto(), override val address: AddressRepresentationInputDto = AddressRepresentationInputDto(), + override val currentness:String? = null, @get:Schema(description = CommonDescription.createdAt) val createdAt: Instant, diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt index c6c6f4cd1..0d81f766f 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt @@ -40,6 +40,7 @@ data class BusinessPartnerOutputDto( override val legalEntity: LegalEntityRepresentationOutputDto, override val site: SiteRepresentationOutputDto?, override val address: AddressComponentOutputDto, + override val currentness: String? = null, @get:Schema(description = CommonDescription.createdAt) val createdAt: Instant, diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt index fb2301349..9606bf5dd 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt @@ -24,6 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole import org.eclipse.tractusx.bpdm.common.model.BaseEntity import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.entity.SharingStateDb +import java.sql.Timestamp import java.util.* @Entity @@ -104,6 +105,9 @@ class BusinessPartnerDb( @JoinColumn(name = "address_confidence_id", unique = true) var addressConfidence: ConfidenceCriteriaDb?, + @Column(name = "currentness") + var currentness: Timestamp? = null, + ) : BaseEntity() { companion object { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt index 17d31da7a..8207f58fe 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt @@ -31,6 +31,7 @@ import org.eclipse.tractusx.bpdm.gate.entity.* import org.eclipse.tractusx.bpdm.gate.entity.generic.* import org.eclipse.tractusx.bpdm.gate.exception.BpdmInvalidPartnerException import org.springframework.stereotype.Service +import java.sql.Timestamp @Service class BusinessPartnerMappings { @@ -88,6 +89,11 @@ class BusinessPartnerMappings { bpnS = dto.site.siteBpn, bpnA = dto.address.addressBpn, postalAddress = toPostalAddress(dto.address), + currentness = try{ + Timestamp.valueOf(dto.currentness) + }catch (e:Exception) { + null + }, legalEntityConfidence = null, siteConfidence = null, addressConfidence = null, diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt index 5bbe159c0..42c49307d 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt @@ -124,12 +124,21 @@ class BusinessPartnerService( val partnerToUpsert = existingPartner ?: BusinessPartnerDb.createEmpty(upsertData.sharingState, upsertData.stage) val hasChanges = compareUtil.hasChanges(upsertData, partnerToUpsert) + val shouldUpdate = if(upsertData.currentness == null) { + true + }else { + upsertData.currentness?.let { + existingPartner?.currentness == null || upsertData.currentness!! > existingPartner.currentness + } ?: false + } if (hasChanges) { - changelogRepository.save(ChangelogEntryDb(sharingState.externalId, sharingState.tenantBpnl, changeType, stage)) + if (shouldUpdate) { + changelogRepository.save(ChangelogEntryDb(sharingState.externalId, sharingState.tenantBpnl, changeType, stage)) - copyUtil.copyValues(upsertData, partnerToUpsert) - businessPartnerRepository.save(partnerToUpsert) + copyUtil.copyValues(upsertData, partnerToUpsert) + businessPartnerRepository.save(partnerToUpsert) + } } return UpsertResult(hasChanges, changeType, partnerToUpsert) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt index f6d17db62..dce5c528a 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt @@ -43,6 +43,7 @@ class BusinessPartnerComparisonUtil { entity.identifiers != persistedBP.identifiers || entity.states != persistedBP.states || entity.classifications != persistedBP.classifications || + entity.currentness != persistedBP.currentness || postalAddressHasChanges(entity.postalAddress, persistedBP.postalAddress) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt index 0b55ba853..3a0ee60c4 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt @@ -42,6 +42,7 @@ class BusinessPartnerCopyUtil { legalEntityConfidence = fromPartner.legalEntityConfidence siteConfidence = fromPartner.siteConfidence addressConfidence = fromPartner.addressConfidence + currentness = fromPartner.currentness nameParts.replace(fromPartner.nameParts) roles.replace(fromPartner.roles) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt index 95a2794c3..d0022b464 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt @@ -91,7 +91,9 @@ object PartnerFileUtil { isOwnCompanyData = row.isOwnCompanyData?.toBoolean() ?: false, legalEntity = row.toLegalEntityRepresentationInputDto(formatter, errors, index), site = row.toSiteRepresentationInputDto(formatter, errors, index), - address = row.toAddressRepresentationInputDto(formatter, errors, index) + address = row.toAddressRepresentationInputDto(formatter, errors, index), + currentness = null + ) } catch (e: Exception) { errors.add("Row ${index + 1} has error: ${e.message}") diff --git a/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql b/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql new file mode 100644 index 000000000..d837749c1 --- /dev/null +++ b/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql @@ -0,0 +1,2 @@ +ALTER TABLE business_partners +ADD COLUMN currentness TIMESTAMP WITHOUT TIME ZONE NULL \ No newline at end of file diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt index daea091c6..0e87cbb6e 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt @@ -50,7 +50,7 @@ abstract class AuthTestBase( @Test fun `PUT Partner Input`() { authAssertions.assert(authExpectations.businessPartner.putInput) { gateClient.businessParters.upsertBusinessPartnersInput(listOf( - BusinessPartnerInputRequest("externalId") + BusinessPartnerInputRequest("externalId", currentness = null) )) } } diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt index 689e77e7a..e1bb2683f 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt @@ -270,6 +270,35 @@ class BusinessPartnerControllerIT @Autowired constructor( assertEquals(0, searchResponsePage2.content.size) } + @Test + fun `insert a late arrival request with minimal business partner and the record won't be updated`() { + + val firstUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness2) //12:00 + gateClient.businessParters.upsertBusinessPartnersInput(firstUpsertRequest).body!! + + val beforeFirstUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness1) // 11:59 + gateClient.businessParters.upsertBusinessPartnersInput(beforeFirstUpsertRequest).body!! + + val searchResponsePage = gateClient.businessParters.getBusinessPartnersInput( + listOf(BusinessPartnerVerboseValues.externalId1)) + + this.mockAndAssertUtils.assertUpsertResponsesMatchRequests(searchResponsePage.content, firstUpsertRequest) + } + + @Test + fun `upsert a new request with later currentness timestamp and the record updated`() { + + val firstUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness2) //12:00 + gateClient.businessParters.upsertBusinessPartnersInput(firstUpsertRequest).body!! + + val laterUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness3) // 12:01 + gateClient.businessParters.upsertBusinessPartnersInput(laterUpsertRequest).body!! + + val searchResponsePage = gateClient.businessParters.getBusinessPartnersInput( + listOf(BusinessPartnerVerboseValues.externalId1)) + + this.mockAndAssertUtils.assertUpsertResponsesMatchRequests(searchResponsePage.content, laterUpsertRequest) + } From 74ba8864fbdcde0268175fc22d73e16cae4f7fd5 Mon Sep 17 00:00:00 2001 From: kunyao-cofinity-x <138508171+kunyao-cofinity-x@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:25:32 +0200 Subject: [PATCH 2/3] feat: Adding a currentness attribute to the business partner input request --- .../gate/BusinessPartnerNonVerboseValues.kt | 3 +- .../gate/BusinessPartnerVerboseValues.kt | 70 +++++++++++++++++-- .../api/model/IBaseBusinessPartnerGateDto.kt | 3 + .../request/BusinessPartnerInputRequest.kt | 3 +- .../api/model/response/BusinessPartnerDto.kt | 1 + .../response/BusinessPartnerOutputDto.kt | 1 + .../gate/entity/generic/BusinessPartnerDb.kt | 4 ++ .../gate/service/BusinessPartnerMappings.kt | 6 ++ .../gate/service/BusinessPartnerService.kt | 15 +++- .../util/BusinessPartnerComparisonUtil.kt | 1 + .../bpdm/gate/util/BusinessPartnerCopyUtil.kt | 1 + .../bpdm/gate/util/PartnerFileUtil.kt | 4 +- .../V6_1_0_4__add_currentness_column.sql | 2 + .../tractusx/bpdm/gate/auth/AuthTestBase.kt | 2 +- .../controller/BusinessPartnerControllerIT.kt | 29 ++++++++ 15 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql diff --git a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt index ecc4f77d1..c22c32009 100644 --- a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt +++ b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerNonVerboseValues.kt @@ -36,7 +36,8 @@ object BusinessPartnerNonVerboseValues { val bpInputRequestMinimal = BusinessPartnerInputRequest( externalId = BusinessPartnerVerboseValues.externalId2, - address = bpPostalAddressInputDtoMinimal + address = bpPostalAddressInputDtoMinimal, + currentness = null ) val bpInputRequestFull = BusinessPartnerVerboseValues.bpInputRequestFull diff --git a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt index 0f28d3f24..d00e59262 100644 --- a/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt +++ b/bpdm-common-test/src/main/kotlin/org/eclipse/tractusx/bpdm/test/testdata/gate/BusinessPartnerVerboseValues.kt @@ -72,6 +72,10 @@ object BusinessPartnerVerboseValues { const val businessStatusDescription1 = "Active" const val businessStatusDescription2 = "Insolvent" + val currentness1 = "2024-06-28 11:59:00" + val currentness2 = "2024-06-28 12:00:00" + val currentness3 = "2024-06-28 12:01:00" + val businessStatusValidFrom1 = LocalDateTime.of(2020, 1, 1, 0, 0) val businessStatusValidFrom2 = LocalDateTime.of(2019, 1, 1, 0, 0) @@ -331,7 +335,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAddress, physicalPostalAddress = postalAddress2, alternativePostalAddress = alternativeAddressFull - ) + ), + currentness = null ) @@ -631,7 +636,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAndSiteMainAddress, physicalPostalAddress = physicalAddressChina, alternativePostalAddress = AlternativePostalAddressDto() - ) + ), + currentness = null ) val bpInputRequestCleaned = BusinessPartnerInputRequest( @@ -659,7 +665,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAddress, physicalPostalAddress = postalAddress2, alternativePostalAddress = alternativeAddressFull - ) + ), + currentness = null ) val bpInputRequestError = BusinessPartnerInputRequest( @@ -687,7 +694,8 @@ object BusinessPartnerVerboseValues { addressType = AddressType.LegalAddress, physicalPostalAddress = postalAddress2, alternativePostalAddress = alternativeAddressFull - ) + ), + currentness = null ) val bpOutputDtoCleaned = BusinessPartnerOutputDto( @@ -802,6 +810,60 @@ object BusinessPartnerVerboseValues { updatedAt = Instant.now() ) + val bpInputRequestWithCurrentness1 = BusinessPartnerInputRequest( + externalId = externalId1, + legalEntity = LegalEntityRepresentationInputDto( + legalEntityBpn = "BPNL0000000000XY", + shortName = "short", + legalName = "Limited Liability Company Name", + legalForm = "Limited Liability Company" + ), + address = AddressRepresentationInputDto( + addressBpn = "BPNA0000000001XY", + name = "Address Name", + addressType = null, + physicalPostalAddress = physicalAddressMinimal + ), + currentness = currentness1 + + ) + + val bpInputRequestWithCurrentness2 = BusinessPartnerInputRequest( + externalId = externalId1, + legalEntity = LegalEntityRepresentationInputDto( + legalEntityBpn = "BPNL0000000000XY", + shortName = "short1", + legalName = "Limited Liability Company Name", + legalForm = "Limited Liability Company" + ), + address = AddressRepresentationInputDto( + addressBpn = "BPNA0000000001XY", + name = "Address Name", + addressType = null, + physicalPostalAddress = physicalAddressMinimal + ), + currentness = currentness2 + + ) + + val bpInputRequestWithCurrentness3 = BusinessPartnerInputRequest( + externalId = externalId1, + legalEntity = LegalEntityRepresentationInputDto( + legalEntityBpn = "BPNL0000000000XY", + shortName = "short2", + legalName = "Limited Liability Company Name", + legalForm = "Limited Liability Company" + ), + address = AddressRepresentationInputDto( + addressBpn = "BPNA0000000001XY", + name = "Another address Name", + addressType = null, + physicalPostalAddress = physicalAddressMinimal + ), + currentness = currentness3 + + ) + val now = Instant.now() diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt index c3f129bb6..601ba68ac 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/IBaseBusinessPartnerGateDto.kt @@ -30,5 +30,8 @@ interface IBaseBusinessPartnerGateDto : IBaseBusinessPartnerDto { @get:Schema(description = "Indicates whether the sharing member claims (in the initial upload) the business partner to belong to the company data of the sharing member.") val isOwnCompanyData: Boolean + + @get:Schema(description = "The timestamp indicates the last time point of change from the user side") + val currentness: String? } diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt index 538ef9f08..a30d39bcd 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/request/BusinessPartnerInputRequest.kt @@ -42,6 +42,7 @@ data class BusinessPartnerInputRequest( override val isOwnCompanyData: Boolean = false, override val legalEntity: LegalEntityRepresentationInputDto = LegalEntityRepresentationInputDto(), override val site: SiteRepresentationInputDto = SiteRepresentationInputDto(), - override val address: AddressRepresentationInputDto = AddressRepresentationInputDto() + override val address: AddressRepresentationInputDto = AddressRepresentationInputDto(), + override val currentness: String? ) : IBaseBusinessPartnerGateDto diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt index 497ade6a8..f7f50dd4c 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerDto.kt @@ -41,6 +41,7 @@ data class BusinessPartnerInputDto( override val legalEntity: LegalEntityRepresentationInputDto = LegalEntityRepresentationInputDto(), override val site: SiteRepresentationInputDto = SiteRepresentationInputDto(), override val address: AddressRepresentationInputDto = AddressRepresentationInputDto(), + override val currentness:String? = null, @get:Schema(description = CommonDescription.createdAt) val createdAt: Instant, diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt index c6c6f4cd1..0d81f766f 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/BusinessPartnerOutputDto.kt @@ -40,6 +40,7 @@ data class BusinessPartnerOutputDto( override val legalEntity: LegalEntityRepresentationOutputDto, override val site: SiteRepresentationOutputDto?, override val address: AddressComponentOutputDto, + override val currentness: String? = null, @get:Schema(description = CommonDescription.createdAt) val createdAt: Instant, diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt index fb2301349..9606bf5dd 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt @@ -24,6 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole import org.eclipse.tractusx.bpdm.common.model.BaseEntity import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.entity.SharingStateDb +import java.sql.Timestamp import java.util.* @Entity @@ -104,6 +105,9 @@ class BusinessPartnerDb( @JoinColumn(name = "address_confidence_id", unique = true) var addressConfidence: ConfidenceCriteriaDb?, + @Column(name = "currentness") + var currentness: Timestamp? = null, + ) : BaseEntity() { companion object { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt index 17d31da7a..8207f58fe 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt @@ -31,6 +31,7 @@ import org.eclipse.tractusx.bpdm.gate.entity.* import org.eclipse.tractusx.bpdm.gate.entity.generic.* import org.eclipse.tractusx.bpdm.gate.exception.BpdmInvalidPartnerException import org.springframework.stereotype.Service +import java.sql.Timestamp @Service class BusinessPartnerMappings { @@ -88,6 +89,11 @@ class BusinessPartnerMappings { bpnS = dto.site.siteBpn, bpnA = dto.address.addressBpn, postalAddress = toPostalAddress(dto.address), + currentness = try{ + Timestamp.valueOf(dto.currentness) + }catch (e:Exception) { + null + }, legalEntityConfidence = null, siteConfidence = null, addressConfidence = null, diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt index bc4580a14..b19db7503 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt @@ -127,12 +127,21 @@ class BusinessPartnerService( val partnerToUpsert = existingPartner ?: BusinessPartnerDb.createEmpty(upsertData.sharingState, upsertData.stage) val hasChanges = compareUtil.hasChanges(upsertData, partnerToUpsert) + val shouldUpdate = if(upsertData.currentness == null) { + true + }else { + upsertData.currentness?.let { + existingPartner?.currentness == null || upsertData.currentness!! > existingPartner.currentness + } ?: false + } if (hasChanges) { - changelogRepository.save(ChangelogEntryDb(sharingState.externalId, sharingState.tenantBpnl, changeType, stage)) + if (shouldUpdate) { + changelogRepository.save(ChangelogEntryDb(sharingState.externalId, sharingState.tenantBpnl, changeType, stage)) - copyUtil.copyValues(upsertData, partnerToUpsert) - businessPartnerRepository.save(partnerToUpsert) + copyUtil.copyValues(upsertData, partnerToUpsert) + businessPartnerRepository.save(partnerToUpsert) + } } return UpsertResult(hasChanges, changeType, partnerToUpsert) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt index f6d17db62..dce5c528a 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerComparisonUtil.kt @@ -43,6 +43,7 @@ class BusinessPartnerComparisonUtil { entity.identifiers != persistedBP.identifiers || entity.states != persistedBP.states || entity.classifications != persistedBP.classifications || + entity.currentness != persistedBP.currentness || postalAddressHasChanges(entity.postalAddress, persistedBP.postalAddress) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt index 0b55ba853..3a0ee60c4 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/BusinessPartnerCopyUtil.kt @@ -42,6 +42,7 @@ class BusinessPartnerCopyUtil { legalEntityConfidence = fromPartner.legalEntityConfidence siteConfidence = fromPartner.siteConfidence addressConfidence = fromPartner.addressConfidence + currentness = fromPartner.currentness nameParts.replace(fromPartner.nameParts) roles.replace(fromPartner.roles) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt index afa7ba274..0ac256915 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/PartnerFileUtil.kt @@ -92,7 +92,9 @@ object PartnerFileUtil { // Legal entity's business partner number is nothing but tenant's partner number who is performing business partner upload action legalEntity = LegalEntityRepresentationInputDto(legalEntityBpn = tenantBpnl?.takeIf { it.isNotEmpty() }), site = row.toSiteRepresentationInputDto(formatter, errors, index), - address = row.toAddressRepresentationInputDto(formatter, errors, index) + address = row.toAddressRepresentationInputDto(formatter, errors, index), + currentness = null + ) } catch (e: Exception) { errors.add("Row ${index + 1} has error: ${e.message}") diff --git a/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql b/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql new file mode 100644 index 000000000..d837749c1 --- /dev/null +++ b/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql @@ -0,0 +1,2 @@ +ALTER TABLE business_partners +ADD COLUMN currentness TIMESTAMP WITHOUT TIME ZONE NULL \ No newline at end of file diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt index daea091c6..0e87cbb6e 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/auth/AuthTestBase.kt @@ -50,7 +50,7 @@ abstract class AuthTestBase( @Test fun `PUT Partner Input`() { authAssertions.assert(authExpectations.businessPartner.putInput) { gateClient.businessParters.upsertBusinessPartnersInput(listOf( - BusinessPartnerInputRequest("externalId") + BusinessPartnerInputRequest("externalId", currentness = null) )) } } diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt index 689e77e7a..e1bb2683f 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/BusinessPartnerControllerIT.kt @@ -270,6 +270,35 @@ class BusinessPartnerControllerIT @Autowired constructor( assertEquals(0, searchResponsePage2.content.size) } + @Test + fun `insert a late arrival request with minimal business partner and the record won't be updated`() { + + val firstUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness2) //12:00 + gateClient.businessParters.upsertBusinessPartnersInput(firstUpsertRequest).body!! + + val beforeFirstUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness1) // 11:59 + gateClient.businessParters.upsertBusinessPartnersInput(beforeFirstUpsertRequest).body!! + + val searchResponsePage = gateClient.businessParters.getBusinessPartnersInput( + listOf(BusinessPartnerVerboseValues.externalId1)) + + this.mockAndAssertUtils.assertUpsertResponsesMatchRequests(searchResponsePage.content, firstUpsertRequest) + } + + @Test + fun `upsert a new request with later currentness timestamp and the record updated`() { + + val firstUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness2) //12:00 + gateClient.businessParters.upsertBusinessPartnersInput(firstUpsertRequest).body!! + + val laterUpsertRequest = listOf(BusinessPartnerVerboseValues.bpInputRequestWithCurrentness3) // 12:01 + gateClient.businessParters.upsertBusinessPartnersInput(laterUpsertRequest).body!! + + val searchResponsePage = gateClient.businessParters.getBusinessPartnersInput( + listOf(BusinessPartnerVerboseValues.externalId1)) + + this.mockAndAssertUtils.assertUpsertResponsesMatchRequests(searchResponsePage.content, laterUpsertRequest) + } From c0f9b5dacab29be4a5c5a16534033695fa6cb19a Mon Sep 17 00:00:00 2001 From: kunyao-cofinity-x <138508171+kunyao-cofinity-x@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:02:32 +0200 Subject: [PATCH 3/3] fix: Update migration script version number. Use instant instead of timestamp. --- .../bpdm/gate/entity/generic/BusinessPartnerDb.kt | 4 ++-- .../bpdm/gate/service/BusinessPartnerMappings.kt | 8 ++++---- .../bpdm/gate/service/BusinessPartnerService.kt | 14 +++++--------- .../eclipse/tractusx/bpdm/gate/util/Extensions.kt | 10 ++++++++++ ...mn.sql => V6_1_0_5__add_currentness_column.sql} | 0 5 files changed, 21 insertions(+), 15 deletions(-) rename bpdm-gate/src/main/resources/db/migration/{V6_1_0_4__add_currentness_column.sql => V6_1_0_5__add_currentness_column.sql} (100%) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt index 9606bf5dd..420af4552 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/generic/BusinessPartnerDb.kt @@ -24,7 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole import org.eclipse.tractusx.bpdm.common.model.BaseEntity import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.entity.SharingStateDb -import java.sql.Timestamp +import java.time.Instant import java.util.* @Entity @@ -106,7 +106,7 @@ class BusinessPartnerDb( var addressConfidence: ConfidenceCriteriaDb?, @Column(name = "currentness") - var currentness: Timestamp? = null, + var currentness: Instant? = null, ) : BaseEntity() { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt index 8207f58fe..663bd1d36 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerMappings.kt @@ -30,8 +30,8 @@ import org.eclipse.tractusx.bpdm.gate.api.model.response.* import org.eclipse.tractusx.bpdm.gate.entity.* import org.eclipse.tractusx.bpdm.gate.entity.generic.* import org.eclipse.tractusx.bpdm.gate.exception.BpdmInvalidPartnerException +import org.eclipse.tractusx.bpdm.gate.util.getTimestampToInstant import org.springframework.stereotype.Service -import java.sql.Timestamp @Service class BusinessPartnerMappings { @@ -89,9 +89,9 @@ class BusinessPartnerMappings { bpnS = dto.site.siteBpn, bpnA = dto.address.addressBpn, postalAddress = toPostalAddress(dto.address), - currentness = try{ - Timestamp.valueOf(dto.currentness) - }catch (e:Exception) { + currentness = try { + getTimestampToInstant(dto.currentness) + } catch (e: Exception) { null }, legalEntityConfidence = null, diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt index b19db7503..b2c9575e5 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/BusinessPartnerService.kt @@ -127,21 +127,17 @@ class BusinessPartnerService( val partnerToUpsert = existingPartner ?: BusinessPartnerDb.createEmpty(upsertData.sharingState, upsertData.stage) val hasChanges = compareUtil.hasChanges(upsertData, partnerToUpsert) - val shouldUpdate = if(upsertData.currentness == null) { - true - }else { - upsertData.currentness?.let { - existingPartner?.currentness == null || upsertData.currentness!! > existingPartner.currentness - } ?: false + val shouldUpdate = when { + upsertData.currentness == null -> true + existingPartner?.currentness == null -> true + else -> upsertData.currentness!!.isAfter(existingPartner.currentness) } - if (hasChanges) { - if (shouldUpdate) { + if (hasChanges && shouldUpdate) { changelogRepository.save(ChangelogEntryDb(sharingState.externalId, sharingState.tenantBpnl, changeType, stage)) copyUtil.copyValues(upsertData, partnerToUpsert) businessPartnerRepository.save(partnerToUpsert) - } } return UpsertResult(hasChanges, changeType, partnerToUpsert) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/Extensions.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/Extensions.kt index 401e7e1c8..810ce1e4a 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/Extensions.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/util/Extensions.kt @@ -21,6 +21,10 @@ package org.eclipse.tractusx.bpdm.gate.util import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneId +import java.time.format.DateTimeFormatter fun List.containsDuplicates(): Boolean = size != distinct().size @@ -35,4 +39,10 @@ fun getCurrentUserBpn(): String? { return authentication.tokenAttributes["bpn"] as String? ?: null } return null; +} + +fun getTimestampToInstant(dateTimeString: String?): Instant { + val inputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") + val ldt = LocalDateTime.parse(dateTimeString, inputFormat) + return ldt.atZone(ZoneId.systemDefault()).toInstant() } \ No newline at end of file diff --git a/bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql b/bpdm-gate/src/main/resources/db/migration/V6_1_0_5__add_currentness_column.sql similarity index 100% rename from bpdm-gate/src/main/resources/db/migration/V6_1_0_4__add_currentness_column.sql rename to bpdm-gate/src/main/resources/db/migration/V6_1_0_5__add_currentness_column.sql