From 690454d80f67b339c7864e04a56510eaee0b23c8 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 16 Mar 2024 10:51:44 +0100 Subject: [PATCH] fix debitor_iv and person.optionalPartner --- .../office/debitor/HsOfficeDebitorEntity.java | 20 +++++++------- .../office/person/HsOfficePersonEntity.java | 19 ++++++++----- .../db/changelog/210-hs-office-person.sql | 1 + .../changelog/273-hs-office-debitor-rbac.md | 2 +- .../changelog/273-hs-office-debitor-rbac.sql | 27 ++++++++++--------- ...fficeDebitorRepositoryIntegrationTest.java | 10 +++---- 6 files changed, 43 insertions(+), 36 deletions(-) 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 0f1e4d54..45ead92f 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 @@ -107,16 +107,16 @@ public class HsOfficeDebitorEntity implements HasUuid, Stringifyable { public static RbacView rbac() { return rbacViewFor("debitor", HsOfficeDebitorEntity.class) .withIdentityView(SQL.query(""" - SELECT debitor.uuid AS uuid, - 'D-' || (SELECT partner.partnerNumber - FROM hs_office_partner partner - JOIN hs_office_relationship partnerRel - ON partnerRel.uuid = partner.partnerRoleUUid AND partnerRel.relType = 'PARTNER' - JOIN hs_office_relationship debitorRel - ON debitorRel.relAnchorUuid = partnerRel.relHolderUuid AND partnerRel.relType = 'ACCOUNTING' - WHERE debitorRel.uuid = debitor.debitorRelUuid) - || to_char(debitorNumberSuffix, 'fm00') as idName - FROM hs_office_debitor AS debitor + SELECT debitor.uuid AS uuid, + 'D-' || (SELECT partner.partnerNumber + FROM hs_office_partner partner + JOIN hs_office_relationship partnerRel + ON partnerRel.uuid = partner.partnerRoleUUid AND partnerRel.relType = 'PARTNER' + JOIN hs_office_relationship debitorRel + ON debitorRel.relAnchorUuid = partnerRel.relHolderUuid AND debitorRel.relType = 'ACCOUNTING' + WHERE debitorRel.uuid = debitor.debitorRelUuid) + || to_char(debitorNumberSuffix, 'fm00') as idName + FROM hs_office_debitor AS debitor """)) .withRestrictedViewOrderBy(SQL.projection("defaultPrefix")) .withUpdatableColumns( diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntity.java index 86ef8145..eaface95 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonEntity.java @@ -59,14 +59,19 @@ public class HsOfficePersonEntity implements HasUuid, Stringifyable { @ManyToOne(cascade = CascadeType.ALL) @JoinFormula( referencedColumnName = "uuid", + // FIXME: use _rv in sub-query value = """ - (SELECT partner.uuid AS uuid - FROM hs_office_partner partner - JOIN hs_office_relationship partnerRel - ON partnerRel.uuid = partner.partnerRoleUuid AND partnerRel.relType = 'PARTNER' - WHERE partnerRel.relHolderUuid = h1_0.uuid) - """) // FIXME: h1_0 is the generated self-reference, I should find a better solution - private HsOfficePartnerEntity optionalPartner; @Override + ( + SELECT DISTINCT partner.uuid AS uuid + FROM hs_office_partner partner + JOIN hs_office_relationship partnerRel + ON partnerRel.uuid = partner.partnerRoleUuid AND partnerRel.relType = 'PARTNER' + WHERE partnerRel.relHolderUuid = personUuid + ) -- uuid would be ambiguous with outer uuid + """) + private HsOfficePartnerEntity optionalPartner; + + @Override public String toString() { return toString.apply(this); } diff --git a/src/main/resources/db/changelog/210-hs-office-person.sql b/src/main/resources/db/changelog/210-hs-office-person.sql index 6a331277..30562033 100644 --- a/src/main/resources/db/changelog/210-hs-office-person.sql +++ b/src/main/resources/db/changelog/210-hs-office-person.sql @@ -17,6 +17,7 @@ CREATE CAST (character varying as HsOfficePersonType) WITH INOUT AS IMPLICIT; create table if not exists hs_office_person ( uuid uuid unique references RbacObject (uuid) initially deferred, + personUuid uuid GENERATED ALWAYS AS (uuid) stored, -- see usage in HsOfficePersonEntity personType HsOfficePersonType not null, tradeName varchar(96), givenName varchar(48), diff --git a/src/main/resources/db/changelog/273-hs-office-debitor-rbac.md b/src/main/resources/db/changelog/273-hs-office-debitor-rbac.md index 20940b35..d3a78d1f 100644 --- a/src/main/resources/db/changelog/273-hs-office-debitor-rbac.md +++ b/src/main/resources/db/changelog/273-hs-office-debitor-rbac.md @@ -1,6 +1,6 @@ ### rbac debitor -This code generated was by RbacViewMermaidFlowchartGenerator at 2024-03-12T16:22:27.339854728. +This code generated was by RbacViewMermaidFlowchartGenerator at 2024-03-16T10:26:46.080386825. ```mermaid %%{init:{'flowchart':{'htmlLabels':false}}}%% diff --git a/src/main/resources/db/changelog/273-hs-office-debitor-rbac.sql b/src/main/resources/db/changelog/273-hs-office-debitor-rbac.sql index 534d773d..6b45264a 100644 --- a/src/main/resources/db/changelog/273-hs-office-debitor-rbac.sql +++ b/src/main/resources/db/changelog/273-hs-office-debitor-rbac.sql @@ -1,5 +1,5 @@ --liquibase formatted sql --- This code generated was by RbacViewPostgresGenerator at 2024-03-12T16:22:27.348469700. +-- This code generated was by RbacViewPostgresGenerator at 2024-03-16T10:26:46.091076286. -- ============================================================================ @@ -220,11 +220,12 @@ create or replace function hs_office_debitor_global_insert_tf() strict as $$ begin call grantPermissionToRole( - globalAdmin(), - createPermission(NEW.uuid, 'INSERT', 'hs_office_debitor')); + createPermission(NEW.uuid, 'INSERT', 'hs_office_debitor'), + globalAdmin()); return NEW; end; $$; +-- z_... is to put it at the end of after insert triggers, to make sure the roles exist create trigger z_hs_office_debitor_global_insert_tg after insert on global for each row @@ -253,16 +254,16 @@ create trigger hs_office_debitor_insert_permission_check_tg -- ---------------------------------------------------------------------------- call generateRbacIdentityViewFromQuery('hs_office_debitor', $idName$ - SELECT debitor.uuid AS uuid, - 'D-' || (SELECT partner.partnerNumber - FROM hs_office_partner partner - JOIN hs_office_relationship partnerRel - ON partnerRel.uuid = partner.partnerRoleUUid AND partnerRel.relType = 'PARTNER' - JOIN hs_office_relationship debitorRel - ON debitorRel.relAnchorUuid = partnerRel.relHolderUuid AND partnerRel.relType = 'ACCOUNTING' - WHERE debitorRel.uuid = debitor.debitorRelUuid) - || to_char(debitorNumberSuffix, 'fm00') as idName - FROM hs_office_debitor AS debitor + SELECT debitor.uuid AS uuid, + 'D-' || (SELECT partner.partnerNumber + FROM hs_office_partner partner + JOIN hs_office_relationship partnerRel + ON partnerRel.uuid = partner.partnerRoleUUid AND partnerRel.relType = 'PARTNER' + JOIN hs_office_relationship debitorRel + ON debitorRel.relAnchorUuid = partnerRel.relHolderUuid AND debitorRel.relType = 'ACCOUNTING' + WHERE debitorRel.uuid = debitor.debitorRelUuid) + || to_char(debitorNumberSuffix, 'fm00') as idName +FROM hs_office_debitor AS debitor $idName$); --// diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java index 4118571d..53c47727 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java @@ -236,9 +236,9 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean // then allTheseDebitorsAreReturned( result, - "debitor(D-1000111: P-10001, fir)", - "debitor(D-1000212: P-10002, sec)", - "debitor(D-1000313: P-10003, thi)"); + "debitor(D-1000111: rel(relAnchor='LP First GmbH', relType='ACCOUNTING', relHolder='LP First GmbH'), fir)", + "debitor(D-1000212: rel(relAnchor='LP Second e.K.', relType='ACCOUNTING', relHolder='LP Second e.K.'), sec)", + "debitor(D-1000313: rel(relAnchor='IF Third OHG', relType='ACCOUNTING', relHolder='IF Third OHG'), thi)"); } @ParameterizedTest @@ -302,7 +302,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean final var result = debitorRepo.findDebitorByOptionalNameLike("third contact"); // then - exactlyTheseDebitorsAreReturned(result, "debitor(D-1000313: P-10003, thi)"); + exactlyTheseDebitorsAreReturned(result, "debitor(D-1000313: rel(relAnchor='IF Third OHG', relType='ACCOUNTING', relHolder='IF Third OHG'), thi)"); } } @@ -536,7 +536,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean // when final var result = jpaAttempt.transacted(() -> { - context("superuser-alex@hostsharing.net", "hs_office_debitor#1000420:FourtheG-fourthcontact.agent"); + context("superuser-alex@hostsharing.net", "hs_office_debitor#D-1000420.agent"); assertThat(debitorRepo.findByUuid(givenDebitor.getUuid())).isPresent(); debitorRepo.deleteByUuid(givenDebitor.getUuid());