automatically-create-ex-partner-relation #35

Merged
hsh-michaelhoennig merged 3 commits from automatically-create-ex-partner-relation into master 2024-04-09 10:08:49 +02:00
3 changed files with 15 additions and 4 deletions
Showing only changes of commit f8e9d77057 - Show all commits

View File

@ -26,6 +26,8 @@ import jakarta.persistence.PersistenceContext;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationType.EX_PARTNER;
@RestController @RestController
public class HsOfficePartnerController implements HsOfficePartnersApi { public class HsOfficePartnerController implements HsOfficePartnersApi {
@ -128,14 +130,23 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var current = partnerRepo.findByUuid(partnerUuid).orElseThrow(); final var current = partnerRepo.findByUuid(partnerUuid).orElseThrow();
final var previousPartnerRel = current.getPartnerRel();
new HsOfficePartnerEntityPatcher(em, current).apply(body); new HsOfficePartnerEntityPatcher(em, current).apply(body);
final var saved = partnerRepo.save(current); final var saved = partnerRepo.save(current);
optionallyCreateExPartnerRelation(saved, previousPartnerRel);
final var mapped = mapper.map(saved, HsOfficePartnerResource.class); final var mapped = mapper.map(saved, HsOfficePartnerResource.class);
return ResponseEntity.ok(mapped); 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) { private HsOfficePartnerEntity createPartnerEntity(final HsOfficePartnerInsertResource body) {
final var entityToSave = new HsOfficePartnerEntity(); final var entityToSave = new HsOfficePartnerEntity();
entityToSave.setPartnerNumber(body.getPartnerNumber()); entityToSave.setPartnerNumber(body.getPartnerNumber());

View File

@ -30,7 +30,7 @@ import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
@Table(name = "hs_office_relation_rv") @Table(name = "hs_office_relation_rv")
@Getter @Getter
@Setter @Setter
@Builder @Builder(toBuilder = true)
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@FieldNameConstants @FieldNameConstants

View File

@ -410,10 +410,10 @@ class HsOfficePartnerControllerAcceptanceTest extends ContextBasedTestWithCleanu
}); });
// and an ex-partner-relation got created // and an ex-partner-relation got created
final var partnerPersonUUid = givenPartner.getPartnerRel().getAnchor().getUuid(); final var anchorpartnerPersonUUid = givenPartner.getPartnerRel().getAnchor().getUuid();
assertThat(relationRepo.findRelationRelatedToPersonUuidAndRelationType(partnerPersonUUid, EX_PARTNER)) assertThat(relationRepo.findRelationRelatedToPersonUuidAndRelationType(anchorpartnerPersonUUid, EX_PARTNER))
.map(HsOfficeRelationEntity::toShortString) .map(HsOfficeRelationEntity::toShortString)
.contains("xxx"); .contains("rel(anchor='LP Hostsharing eG', type='EX_PARTNER', holder='UF Erben Bessler')");
} }
@Test @Test