diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java index 70efafd4..ee8909c3 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java @@ -105,26 +105,10 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { "ERROR: [400] debitorRel.mark must be null"); final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); - if (body.getDebitorRel() != null) { // FIXME: move this into RESOURCE_TO_ENTITY_POSTMAPPER - final var debitorRel = entityToSave.getDebitorRel(); - debitorRel.setType(DEBITOR); - entityValidator.validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor()); - entityValidator.validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder()); - entityValidator.validateEntityExists("debitorRel.contactUuid", debitorRel.getContact()); - entityToSave.setDebitorRel(realRelRepo.save(debitorRel)); - } else { - final var debitorRelOptional = realRelRepo.findByUuid(body.getDebitorRelUuid()); - debitorRelOptional.ifPresentOrElse( - debitorRel -> {entityToSave.setDebitorRel(realRelRepo.save(debitorRel));}, - () -> { - throw new ValidationException( - "Unable to find RealRelation by debitorRelUuid: " + body.getDebitorRelUuid()); - }); - } - final var savedEntity = debitorRepo.save(entityToSave); - em.flush(); // FIXME: necessary? - em.refresh(savedEntity); + final var savedEntity = debitorRepo.save(entityToSave).load(); +// em.flush(); // FIXME: necessary? +// em.refresh(savedEntity); final var uri = MvcUriComponentsBuilder.fromController(getClass()) @@ -219,11 +203,23 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { "debitorRel.contact.uuid", resource.getDebitorRel().getContactUuid(), realContactRepo::findByUuid)) .build()); entity.setDebitorRel(debitorRel); + } else { + final var debitorRelOptional = realRelRepo.findByUuid(resource.getDebitorRelUuid()); + debitorRelOptional.ifPresentOrElse( + debitorRel -> { + entity.setDebitorRel(realRelRepo.save(debitorRel)); + }, + () -> { + throw new ValidationException( + "Unable to find debitorRel.uuid: " + resource.getDebitorRelUuid()); + }); } + if (resource.getRefundBankAccountUuid() != null) { entity.setRefundBankAccount(resolve( "refundBankAccount.uuid", resource.getRefundBankAccountUuid(), bankAccountRepo::findByUuid)); } + }; final BiConsumer ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java index 0f0fc9cf..b27f1866 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java @@ -132,7 +132,7 @@ public class HsOfficeDebitorEntity implements BaseEntity, @Override public HsOfficeDebitorEntity load() { BaseEntity.super.load(); - if (partner != null) { + if (partner != null) { // FIXME: should not happen partner.load(); } debitorRel.load(); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java index 206df39d..38a6b24f 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java @@ -496,7 +496,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu .post("http://localhost/api/hs/office/debitors") .then().log().all().assertThat() .statusCode(400) - .body("message", is("ERROR: [400] Unable to find RealRelation by debitorRelUuid: 00000000-0000-0000-0000-000000000000")); + .body("message", is("ERROR: [400] Unable to find debitorRel.uuid: 00000000-0000-0000-0000-000000000000")); // @formatter:on } }