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 3a592907..32721f0d 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 @@ -77,61 +77,38 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { "ERROR: [400] exactly one of debitorRel and debitorRelUuid must be supplied, but found both"); Validate.isTrue(body.getDebitorRel() != null || body.getDebitorRelUuid() != null, "ERROR: [400] exactly one of debitorRel and debitorRelUuid must be supplied, but found none"); - Validate.isTrue(body.getDebitorRel() == null || - body.getDebitorRel().getType() == null || DEBITOR.name().equals(body.getDebitorRel().getType()), - "ERROR: [400] debitorRel.type must be '"+DEBITOR.name()+"' or null for default"); Validate.isTrue(body.getDebitorRel() == null || body.getDebitorRel().getMark() == null, "ERROR: [400] debitorRel.mark must be null"); final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class); - try { - if (body.getDebitorRel() != null) { - body.getDebitorRel().setType(DEBITOR.name()); - final var debitorRel = mapper.map("debitorRel.", body.getDebitorRel(), HsOfficeRelationRealEntity.class); - entityValidator.validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor()); - entityValidator.validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder()); - entityValidator.validateEntityExists("debitorRel.contactUuid", debitorRel.getContact()); - entityToSave.setDebitorRel(relrealRepo.save(debitorRel)); - } else { - final var debitorRelOptional = relrealRepo.findByUuid(body.getDebitorRelUuid()); - debitorRelOptional.ifPresentOrElse( - debitorRel -> {entityToSave.setDebitorRel(relrealRepo.save(debitorRel));}, - () -> { - throw new ValidationException( - "Unable to find RealRelation by debitorRelUuid: " + body.getDebitorRelUuid()); - }); - } - - final var partnerRel = em.createNativeQuery(""" - SELECT partnerRel.* - FROM hs_office.relation AS partnerRel - JOIN hs_office.relation AS debitorRel - ON debitorRel.type = 'DEBITOR' AND debitorRel.anchorUuid = partnerRel.holderUuid - WHERE partnerRel.type = 'PARTNER' - AND :NEW_DebitorRelUuid = debitorRel.uuid - """).setParameter("NEW_DebitorRelUuid", entityToSave.getDebitorRel().getUuid()).getResultList(); - - - final var debitorRel = em.createNativeQuery(""" - SELECT debitorRel.* - FROM hs_office.relation AS debitorRel - WHERE :NEW_DebitorRelUuid = debitorRel.uuid - """).setParameter("NEW_DebitorRelUuid", entityToSave.getDebitorRel().getUuid()).getResultList(); - - final var savedEntity = debitorRepo.save(entityToSave); - em.flush(); - em.refresh(savedEntity); - - final var uri = - MvcUriComponentsBuilder.fromController(getClass()) - .path("/api/hs/office/debitors/{id}") - .buildAndExpand(savedEntity.getUuid()) - .toUri(); - final var mapped = mapper.map(savedEntity, HsOfficeDebitorResource.class); - return ResponseEntity.created(uri).body(mapped); - } catch (final RuntimeException exc) { - throw exc; + if (body.getDebitorRel() != null) { + final var debitorRel = mapper.map("debitorRel.", body.getDebitorRel(), HsOfficeRelationRealEntity.class); + debitorRel.setType(DEBITOR); + entityValidator.validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor()); + entityValidator.validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder()); + entityValidator.validateEntityExists("debitorRel.contactUuid", debitorRel.getContact()); + entityToSave.setDebitorRel(relrealRepo.save(debitorRel)); + } else { + final var debitorRelOptional = relrealRepo.findByUuid(body.getDebitorRelUuid()); + debitorRelOptional.ifPresentOrElse( + debitorRel -> {entityToSave.setDebitorRel(relrealRepo.save(debitorRel));}, + () -> { + throw new ValidationException( + "Unable to find RealRelation by debitorRelUuid: " + body.getDebitorRelUuid()); + }); } + + final var savedEntity = debitorRepo.save(entityToSave); + em.flush(); + em.refresh(savedEntity); + + final var uri = + MvcUriComponentsBuilder.fromController(getClass()) + .path("/api/hs/office/debitors/{id}") + .buildAndExpand(savedEntity.getUuid()) + .toUri(); + final var mapped = mapper.map(savedEntity, HsOfficeDebitorResource.class); + return ResponseEntity.created(uri).body(mapped); } @Override diff --git a/src/main/resources/api-definition/hs-office/hs-office-debitor-schemas.yaml b/src/main/resources/api-definition/hs-office/hs-office-debitor-schemas.yaml index ca457ece..ed4d8c26 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-debitor-schemas.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-debitor-schemas.yaml @@ -74,7 +74,7 @@ components: type: object properties: debitorRel: - $ref: 'hs-office-relation-schemas.yaml#/components/schemas/HsOfficeRelationInsert' + $ref: 'hs-office-relation-schemas.yaml#/components/schemas/HsOfficeRelationSubInsert' debitorRelUuid: type: string format: uuid diff --git a/src/main/resources/api-definition/hs-office/hs-office-relation-schemas.yaml b/src/main/resources/api-definition/hs-office/hs-office-relation-schemas.yaml index e0448a6f..cbad776c 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-relation-schemas.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-relation-schemas.yaml @@ -41,6 +41,7 @@ components: format: uuid nullable: true + # arbitrary relation with explicit type HsOfficeRelationInsert: type: object properties: @@ -64,3 +65,24 @@ components: - holderUuid - type - contactUuid + + # relation created as a sub-element with implicitly known type + HsOfficeRelationSubInsert: + type: object + properties: + anchorUuid: + type: string + format: uuid + holderUuid: + type: string + format: uuid + mark: + type: string + nullable: true + contactUuid: + type: string + format: uuid + required: + - anchorUuid + - holderUuid + - contactUuid diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/debitor/CreateExternalDebitorForPartner.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/debitor/CreateExternalDebitorForPartner.java index d5a3d8a0..a96a2596 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/debitor/CreateExternalDebitorForPartner.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/debitor/CreateExternalDebitorForPartner.java @@ -46,7 +46,6 @@ public class CreateExternalDebitorForPartner extends UseCase