From f8e9d770575797b180cfefd98ba13e99ae80695d Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 8 Apr 2024 12:41:36 +0200 Subject: [PATCH] automatically create ex-partner relation if partner.partnerRel is updated --- .../hs/office/partner/HsOfficePartnerController.java | 11 +++++++++++ .../hs/office/relation/HsOfficeRelationEntity.java | 2 +- .../HsOfficePartnerControllerAcceptanceTest.java | 6 +++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java index cacff85e..1b9707f7 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java @@ -26,6 +26,8 @@ import jakarta.persistence.PersistenceContext; import java.util.List; import java.util.UUID; +import static net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationType.EX_PARTNER; + @RestController public class HsOfficePartnerController implements HsOfficePartnersApi { @@ -128,14 +130,23 @@ public class HsOfficePartnerController implements HsOfficePartnersApi { context.define(currentUser, assumedRoles); final var current = partnerRepo.findByUuid(partnerUuid).orElseThrow(); + final var previousPartnerRel = current.getPartnerRel(); new HsOfficePartnerEntityPatcher(em, current).apply(body); final var saved = partnerRepo.save(current); + optionallyCreateExPartnerRelation(saved, previousPartnerRel); + final var mapped = mapper.map(saved, HsOfficePartnerResource.class); return ResponseEntity.ok(mapped); } + private void optionallyCreateExPartnerRelation(final HsOfficePartnerEntity saved, final HsOfficeRelationEntity previousPartnerRel) { + if (!saved.getPartnerRel().getUuid().equals(previousPartnerRel.getUuid())) { + relationRepo.save(previousPartnerRel.toBuilder().uuid(null).type(EX_PARTNER).build()); + } + } + private HsOfficePartnerEntity createPartnerEntity(final HsOfficePartnerInsertResource body) { final var entityToSave = new HsOfficePartnerEntity(); entityToSave.setPartnerNumber(body.getPartnerNumber()); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationEntity.java index c8c41db0..2bc9c452 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationEntity.java @@ -30,7 +30,7 @@ import static net.hostsharing.hsadminng.stringify.Stringify.stringify; @Table(name = "hs_office_relation_rv") @Getter @Setter -@Builder +@Builder(toBuilder = true) @NoArgsConstructor @AllArgsConstructor @FieldNameConstants diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java index 3f396d31..cc18943e 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java @@ -410,10 +410,10 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu }); // and an ex-partner-relation got created - final var partnerPersonUUid = givenPartner.getPartnerRel().getAnchor().getUuid(); - assertThat(relationRepo.findRelationRelatedToPersonUuidAndRelationType(partnerPersonUUid, EX_PARTNER)) + final var anchorpartnerPersonUUid = givenPartner.getPartnerRel().getAnchor().getUuid(); + assertThat(relationRepo.findRelationRelatedToPersonUuidAndRelationType(anchorpartnerPersonUUid, EX_PARTNER)) .map(HsOfficeRelationEntity::toShortString) - .contains("xxx"); + .contains("rel(anchor='LP Hostsharing eG', type='EX_PARTNER', holder='UF Erben Bessler')"); } @Test