From 074187b4997f862edd8bf442b12cd1ebc4bc8491 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 8 Apr 2024 19:45:41 +0200 Subject: [PATCH 1/7] import cancelled memberships if shares or asset booking exists --- .../hs/office/debitor/HsOfficeDebitorEntity.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 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 67313b4f..7a2cb1ec 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 @@ -19,8 +19,13 @@ import org.hibernate.annotations.JoinFormula; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import jakarta.persistence.*; -import jakarta.persistence.Version; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import jakarta.validation.constraints.Pattern; import java.io.IOException; import java.util.UUID; -- 2.39.5 From 1e0583bab2363bbd04d6ffdb07726f51073cf0bd Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 9 Apr 2024 08:31:02 +0200 Subject: [PATCH 2/7] add coop-assets-transaction reverse-entry to entity+table --- .../HsOfficeCoopAssetsTransactionEntity.java | 8 ++++++ .../5120-hs-office-coopassets.sql | 17 ++++++------ ...ceCoopAssetsTransactionEntityUnitTest.java | 26 +++++++++++++++++-- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java index e5aaf789..8959c962 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java @@ -50,6 +50,7 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO .withProp(HsOfficeCoopAssetsTransactionEntity::getAssetValue) .withProp(HsOfficeCoopAssetsTransactionEntity::getReference) .withProp(HsOfficeCoopAssetsTransactionEntity::getComment) + .withProp(at -> ofNullable(at.getReverseEntry()).map(HsOfficeCoopAssetsTransactionEntity::toShortString).orElse(null)) .quotedValues(false); @Id @@ -93,6 +94,13 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO @Column(name = "comment") private String comment; + /** + * Optionally, the corresponding transaction for an adjustment transaction, + * linked in both directions. + */ + @OneToOne + @JoinColumn(name = "reverseentryuuid") + private HsOfficeCoopAssetsTransactionEntity reverseEntry; public String getTaggedMemberNumber() { return ofNullable(membership).map(HsOfficeMembershipEntity::toShortString).orElse("M-?????"); diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql index 2051c833..29ac74f8 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql @@ -17,14 +17,15 @@ CREATE CAST (character varying as HsOfficeCoopAssetsTransactionType) WITH INOUT create table if not exists hs_office_coopassetstransaction ( - uuid uuid unique references RbacObject (uuid) initially deferred, - version int not null default 0, - membershipUuid uuid not null references hs_office_membership(uuid), - transactionType HsOfficeCoopAssetsTransactionType not null, - valueDate date not null, - assetValue money, - reference varchar(48), - comment varchar(512) + uuid uuid unique references RbacObject (uuid) initially deferred, + version int not null default 0, + membershipUuid uuid not null references hs_office_membership(uuid), + transactionType HsOfficeCoopAssetsTransactionType not null, + valueDate date not null, + assetValue money not null, + reference varchar(48) not null, + reverseEntryUuid uuid references hs_office_coopassetstransaction (uuid), + comment varchar(512) ); --// diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java index 82ba35e3..c7a0690c 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java @@ -16,14 +16,36 @@ class HsOfficeCoopAssetsTransactionEntityUnitTest { .valueDate(LocalDate.parse("2020-01-01")) .transactionType(HsOfficeCoopAssetsTransactionType.DEPOSIT) .assetValue(new BigDecimal("128.00")) + .comment("some comment") .build(); + + + final HsOfficeCoopAssetsTransactionEntity givenCoopAssetAdjustmentTransaction = HsOfficeCoopAssetsTransactionEntity.builder() + .membership(TEST_MEMBERSHIP) + .reference("some-ref") + .valueDate(LocalDate.parse("2020-01-15")) + .transactionType(HsOfficeCoopAssetsTransactionType.ADJUSTMENT) + .assetValue(new BigDecimal("-128.00")) + .comment("some comment") + .reverseEntry(givenCoopAssetTransaction) + .build(); + final HsOfficeCoopAssetsTransactionEntity givenEmptyCoopAssetsTransaction = HsOfficeCoopAssetsTransactionEntity.builder().build(); @Test - void toStringContainsAlmostAllPropertiesAccount() { + void toStringContainsAllNonNullProperties() { final var result = givenCoopAssetTransaction.toString(); - assertThat(result).isEqualTo("CoopAssetsTransaction(M-1000101: 2020-01-01, DEPOSIT, 128.00, some-ref)"); + assertThat(result).isEqualTo("CoopAssetsTransaction(M-1000101: 2020-01-01, DEPOSIT, 128.00, some-ref, some comment)"); + } + + @Test + void toStringWithReverseEntryContainsReverseEntry() { + givenCoopAssetTransaction.setReverseEntry(givenCoopAssetAdjustmentTransaction); + + final var result = givenCoopAssetTransaction.toString(); + + assertThat(result).isEqualTo("CoopAssetsTransaction(M-1000101: 2020-01-01, DEPOSIT, 128.00, some-ref, some comment, M-1000101:-128.00)"); } @Test -- 2.39.5 From 82cd5a9ac6f7b99698f926cd4a823a13e2aab87f Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 9 Apr 2024 10:07:46 +0200 Subject: [PATCH 3/7] coop assets transaction with reverse entry --- ...OfficeCoopAssetsTransactionController.java | 18 +++-- .../HsOfficeCoopAssetsTransactionEntity.java | 2 +- .../office/debitor/HsOfficeDebitorEntity.java | 1 + .../hs-office-coopassets-schemas.yaml | 27 +++++++ .../5120-hs-office-coopassets.sql | 2 +- .../5128-hs-office-coopassets-test-data.sql | 18 +++-- ...tsTransactionControllerAcceptanceTest.java | 72 ++++++++++++++++++- ...sTransactionRepositoryIntegrationTest.java | 21 +++--- .../hs/office/migration/ImportOfficeData.java | 21 +++--- 9 files changed, 150 insertions(+), 32 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java index add8333c..f72a855d 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java @@ -2,8 +2,7 @@ package net.hostsharing.hsadminng.hs.office.coopassets; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeCoopAssetsApi; -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionInsertResource; -import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionResource; +import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*; import net.hostsharing.hsadminng.mapper.Mapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; @@ -13,11 +12,13 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; +import jakarta.persistence.EntityNotFoundException; import jakarta.validation.ValidationException; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.function.BiConsumer; import static java.lang.String.join; import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionTypeResource.*; @@ -63,8 +64,11 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse context.define(currentUser, assumedRoles); validate(requestBody); - final var entityToSave = mapper.map(requestBody, HsOfficeCoopAssetsTransactionEntity.class); + final var entityToSave = mapper.map(requestBody, HsOfficeCoopAssetsTransactionEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); + if (entityToSave.getReverseEntry() != null) { + entityToSave.getReverseEntry().setReverseEntry(entityToSave); + } final var saved = coopAssetsTransactionRepo.save(entityToSave); final var uri = @@ -131,4 +135,10 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse } } -} + final BiConsumer RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { + if ( resource.getReverseEntryUuid() != null ) { + entity.setReverseEntry(coopAssetsTransactionRepo.findByUuid(resource.getReverseEntryUuid()) + .orElseThrow(() -> new EntityNotFoundException("ERROR: [400] reverseEntityUuid %s not found".formatted(resource.getReverseEntryUuid())))); + } + }; +}; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java index 8959c962..18e1f886 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java @@ -95,7 +95,7 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO private String comment; /** - * Optionally, the corresponding transaction for an adjustment transaction, + * Optionally, the UUID of the corresponding transaction for an adjustment transaction, * linked in both directions. */ @OneToOne 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 7a2cb1ec..51df906f 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 @@ -26,6 +26,7 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; +import jakarta.persistence.Version; import jakarta.validation.constraints.Pattern; import java.io.IOException; import java.util.UUID; diff --git a/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml b/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml index adfcc9e8..a11e11ef 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml @@ -32,6 +32,30 @@ components: type: string comment: type: string + reverseEntry: + $ref: '#/components/schemas/HsOfficeCoopAssetsTransactionReverse' + + HsOfficeCoopAssetsTransactionReverse: + description: + Similar to `HsOfficeCoopAssetsTransaction` but without the `reverseEntry`, + otherwise the JSON would be recursive. + type: object + properties: + uuid: + type: string + format: uuid + transactionType: + $ref: '#/components/schemas/HsOfficeCoopAssetsTransactionType' + assetValue: + type: number + format: currency + valueDate: + type: string + format: date + reference: + type: string + comment: + type: string HsOfficeCoopAssetsTransactionInsert: type: object @@ -54,6 +78,9 @@ components: maxLength: 48 comment: type: string + reverseEntryUuid: + type: string + format: uuid required: - membershipUuid - transactionType diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql index 29ac74f8..587eb641 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql @@ -24,7 +24,7 @@ create table if not exists hs_office_coopassetstransaction valueDate date not null, assetValue money not null, reference varchar(48) not null, - reverseEntryUuid uuid references hs_office_coopassetstransaction (uuid), + reverseEntryUuid uuid references hs_office_coopassetstransaction (uuid) deferrable , comment varchar(512) ); --// diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql index d54e77ca..7bedc7c5 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql @@ -14,11 +14,14 @@ create or replace procedure createHsOfficeCoopAssetsTransactionTestData( ) language plpgsql as $$ declare - currentTask varchar; - membership hs_office_membership; + currentTask varchar; + membership hs_office_membership; + lossEntryUuid uuid; + adjustmentEntryUuid uuid; begin currentTask = 'creating coopAssetsTransaction test-data ' || givenPartnerNumber || givenMemberNumberSuffix; execute format('set local hsadminng.currentTask to %L', currentTask); + SET CONSTRAINTS ALL DEFERRED; call defineContext(currentTask); select m.uuid @@ -29,12 +32,15 @@ begin into membership; raise notice 'creating test coopAssetsTransaction: %', givenPartnerNumber || givenMemberNumberSuffix; + lossEntryUuid := uuid_generate_v4(); + adjustmentEntryUuid := uuid_generate_v4(); insert - into hs_office_coopassetstransaction(uuid, membershipuuid, transactiontype, valuedate, assetvalue, reference, comment) + into hs_office_coopassetstransaction(uuid, membershipuuid, transactiontype, valuedate, assetvalue, reference, comment, reverseEntryUuid) values - (uuid_generate_v4(), membership.uuid, 'DEPOSIT', '2010-03-15', 320.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-1', 'initial deposit'), - (uuid_generate_v4(), membership.uuid, 'DISBURSAL', '2021-09-01', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-2', 'partial disbursal'), - (uuid_generate_v4(), membership.uuid, 'ADJUSTMENT', '2022-10-20', 128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some adjustment'); + (uuid_generate_v4(), membership.uuid, 'DEPOSIT', '2010-03-15', 320.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-1', 'initial deposit', null), + (uuid_generate_v4(), membership.uuid, 'DISBURSAL', '2021-09-01', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-2', 'partial disbursal', null), + (lossEntryUuid, membership.uuid, 'LOSS', '2022-10-20', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some loss', adjustmentEntryUuid), + (adjustmentEntryUuid, membership.uuid, 'ADJUSTMENT', '2022-10-21', 128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some adjustment', lossEntryUuid); end; $$; --// diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java index 2c9a811d..64393d52 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java @@ -22,6 +22,7 @@ import jakarta.persistence.PersistenceContext; import java.time.LocalDate; import java.util.UUID; +import static net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType.LOSS; import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid; import static net.hostsharing.test.JsonMatcher.lenientlyEquals; import static org.assertj.core.api.Assertions.assertThat; @@ -69,7 +70,7 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased .then().log().all().assertThat() .statusCode(200) .contentType("application/json") - .body("", hasSize(9)); // @formatter:on + .body("", hasSize(12)); // @formatter:on } @Test @@ -103,10 +104,17 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased "reference": "ref 1000202-2", "comment": "partial disbursal" }, + { + "transactionType": "LOSS", + "assetValue": -128.00, + "valueDate": "2022-10-20", + "reference": "ref 1000202-3", + "comment": "some loss" + }, { "transactionType": "ADJUSTMENT", "assetValue": 128.00, - "valueDate": "2022-10-20", + "valueDate": "2022-10-21", "reference": "ref 1000202-3", "comment": "some adjustment" } @@ -193,6 +201,66 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased assertThat(newUserUuid).isNotNull(); } + @Test + void globalAdmin_canAddCoopAssetsAdjustmentTransaction() { + + context.define("superuser-alex@hostsharing.net"); + final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101); + final var givenTransaction = coopAssetsTransactionRepo.findCoopAssetsTransactionByOptionalMembershipUuidAndDateRange(givenMembership.getUuid(), null, null) + .stream().filter(at -> at.getTransactionType() == LOSS) + .findFirst() + .orElseThrow(); + + final var location = RestAssured // @formatter:off + .given() + .header("current-user", "superuser-alex@hostsharing.net") + .contentType(ContentType.JSON) + .body(""" + { + "membershipUuid": "%s", + "transactionType": "ADJUSTMENT", + "assetValue": %s, + "valueDate": "2022-10-30", + "reference": "temp ref A", + "comment": "some coop assets adjustment transaction", + "reverseEntryUuid": "%s" + } + """.formatted( + givenMembership.getUuid(), + givenTransaction.getAssetValue().negate().toString(), + givenTransaction.getUuid())) + .port(port) + .when() + .post("http://localhost/api/hs/office/coopassetstransactions") + .then().log().all().assertThat() + .statusCode(201) + .contentType(ContentType.JSON) + .body("uuid", isUuidValid()) + .body("", lenientlyEquals(""" + { + "transactionType": "ADJUSTMENT", + "assetValue": 128.00, + "valueDate": "2022-10-30", + "reference": "temp ref A", + "comment": "some coop assets adjustment transaction", + "reverseEntry": { + "transactionType": "LOSS", + "assetValue": -128.00, + "valueDate": "2022-10-20", + "reference": "ref 1000101-3", + "comment": "some loss" + } + } + """.formatted(givenTransaction.getUuid()))) + .header("Location", startsWith("http://localhost")) + .extract().header("Location"); // @formatter:on + + // finally, the new coopAssetsTransaction can be accessed under the generated UUID + final var newUserUuid = UUID.fromString( + location.substring(location.lastIndexOf('/') + 1)); + assertThat(newUserUuid).isNotNull(); + } + @Test void globalAdmin_canNotCancelMoreAssetsThanCurrentlySubscribed() { diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java index 978e2081..4f8c20b5 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java @@ -127,7 +127,7 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase class FindAllCoopAssetsTransactions { @Test - public void globalAdmin_anViewAllCoopAssetsTransactions() { + public void globalAdmin_canViewAllCoopAssetsTransactions() { // given context("superuser-alex@hostsharing.net"); @@ -138,19 +138,22 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase null); // then - allTheseCoopAssetsTransactionsAreReturned( + exactlyTheseCoopAssetsTransactionsAreReturned( result, "CoopAssetsTransaction(M-1000101: 2010-03-15, DEPOSIT, 320.00, ref 1000101-1, initial deposit)", "CoopAssetsTransaction(M-1000101: 2021-09-01, DISBURSAL, -128.00, ref 1000101-2, partial disbursal)", - "CoopAssetsTransaction(M-1000101: 2022-10-20, ADJUSTMENT, 128.00, ref 1000101-3, some adjustment)", + "CoopAssetsTransaction(M-1000101: 2022-10-20, LOSS, -128.00, ref 1000101-3, some loss, M-1000101:+128.00)", + "CoopAssetsTransaction(M-1000101: 2022-10-21, ADJUSTMENT, 128.00, ref 1000101-3, some adjustment, M-1000101:-128.00)", "CoopAssetsTransaction(M-1000202: 2010-03-15, DEPOSIT, 320.00, ref 1000202-1, initial deposit)", "CoopAssetsTransaction(M-1000202: 2021-09-01, DISBURSAL, -128.00, ref 1000202-2, partial disbursal)", - "CoopAssetsTransaction(M-1000202: 2022-10-20, ADJUSTMENT, 128.00, ref 1000202-3, some adjustment)", + "CoopAssetsTransaction(M-1000202: 2022-10-20, LOSS, -128.00, ref 1000202-3, some loss, M-1000202:+128.00)", + "CoopAssetsTransaction(M-1000202: 2022-10-21, ADJUSTMENT, 128.00, ref 1000202-3, some adjustment, M-1000202:-128.00)", "CoopAssetsTransaction(M-1000303: 2010-03-15, DEPOSIT, 320.00, ref 1000303-1, initial deposit)", "CoopAssetsTransaction(M-1000303: 2021-09-01, DISBURSAL, -128.00, ref 1000303-2, partial disbursal)", - "CoopAssetsTransaction(M-1000303: 2022-10-20, ADJUSTMENT, 128.00, ref 1000303-3, some adjustment)"); + "CoopAssetsTransaction(M-1000303: 2022-10-20, LOSS, -128.00, ref 1000303-3, some loss, M-1000303:+128.00)", + "CoopAssetsTransaction(M-1000303: 2022-10-21, ADJUSTMENT, 128.00, ref 1000303-3, some adjustment, M-1000303:-128.00)"); } @Test @@ -166,11 +169,12 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase null); // then - allTheseCoopAssetsTransactionsAreReturned( + exactlyTheseCoopAssetsTransactionsAreReturned( result, "CoopAssetsTransaction(M-1000202: 2010-03-15, DEPOSIT, 320.00, ref 1000202-1, initial deposit)", "CoopAssetsTransaction(M-1000202: 2021-09-01, DISBURSAL, -128.00, ref 1000202-2, partial disbursal)", - "CoopAssetsTransaction(M-1000202: 2022-10-20, ADJUSTMENT, 128.00, ref 1000202-3, some adjustment)"); + "CoopAssetsTransaction(M-1000202: 2022-10-20, LOSS, -128.00, ref 1000202-3, some loss, M-1000202:+128.00)", + "CoopAssetsTransaction(M-1000202: 2022-10-21, ADJUSTMENT, 128.00, ref 1000202-3, some adjustment, M-1000202:-128.00)"); } @Test @@ -207,7 +211,8 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase result, "CoopAssetsTransaction(M-1000101: 2010-03-15, DEPOSIT, 320.00, ref 1000101-1, initial deposit)", "CoopAssetsTransaction(M-1000101: 2021-09-01, DISBURSAL, -128.00, ref 1000101-2, partial disbursal)", - "CoopAssetsTransaction(M-1000101: 2022-10-20, ADJUSTMENT, 128.00, ref 1000101-3, some adjustment)"); + "CoopAssetsTransaction(M-1000101: 2022-10-20, LOSS, -128.00, ref 1000101-3, some loss, M-1000101:+128.00)", + "CoopAssetsTransaction(M-1000101: 2022-10-21, ADJUSTMENT, 128.00, ref 1000101-3, some adjustment, M-1000101:-128.00)"); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java index fb51e8c8..47e5a6ba 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java @@ -387,16 +387,16 @@ public class ImportOfficeData extends ContextBasedTest { assertThat(toFormattedString(coopAssets)).isEqualToIgnoringWhitespace(""" { - 30000=CoopAssetsTransaction(M-1001700: 2000-12-06, DEPOSIT, 1280.00, for subscription A), - 31000=CoopAssetsTransaction(M-1002000: 2000-12-06, DEPOSIT, 128.00, for subscription B), - 32000=CoopAssetsTransaction(M-1001700: 2005-01-10, DEPOSIT, 2560.00, for subscription C), - 33001=CoopAssetsTransaction(M-1001700: 2005-01-10, TRANSFER, -512.00, for transfer to 10), - 33002=CoopAssetsTransaction(M-1002000: 2005-01-10, ADOPTION, 512.00, for transfer from 7), - 34001=CoopAssetsTransaction(M-1002000: 2016-12-31, CLEARING, -8.00, for cancellation D), - 34002=CoopAssetsTransaction(M-1002000: 2016-12-31, DISBURSAL, -100.00, for cancellation D), - 34003=CoopAssetsTransaction(M-1002000: 2016-12-31, LOSS, -20.00, for cancellation D), - 35001=CoopAssetsTransaction(M-1909000: 2024-01-15, DEPOSIT, 128.00, for subscription E), - 35002=CoopAssetsTransaction(M-1909000: 2024-01-20, ADJUSTMENT, -128.00, chargeback for subscription E) + 30000=CoopAssetsTransaction(M-1001700: 2000-12-06, DEPOSIT, 1280.00, legacy data import, for subscription A), + 31000=CoopAssetsTransaction(M-1002000: 2000-12-06, DEPOSIT, 128.00, legacy data import, for subscription B), + 32000=CoopAssetsTransaction(M-1001700: 2005-01-10, DEPOSIT, 2560.00, legacy data import, for subscription C), + 33001=CoopAssetsTransaction(M-1001700: 2005-01-10, TRANSFER, -512.00, legacy data import, for transfer to 10), + 33002=CoopAssetsTransaction(M-1002000: 2005-01-10, ADOPTION, 512.00, legacy data import, for transfer from 7), + 34001=CoopAssetsTransaction(M-1002000: 2016-12-31, CLEARING, -8.00, legacy data import, for cancellation D), + 34002=CoopAssetsTransaction(M-1002000: 2016-12-31, DISBURSAL, -100.00, legacy data import, for cancellation D), + 34003=CoopAssetsTransaction(M-1002000: 2016-12-31, LOSS, -20.00, legacy data import, for cancellation D), + 35001=CoopAssetsTransaction(M-1909000: 2024-01-15, DEPOSIT, 128.00, legacy data import, for subscription E), + 35002=CoopAssetsTransaction(M-1909000: 2024-01-20, ADJUSTMENT, -128.00, legacy data import, chargeback for subscription E) } """); } @@ -849,6 +849,7 @@ public class ImportOfficeData extends ContextBasedTest { .transactionType(assetTypeMapping.get(rec.getString("action"))) .assetValue(rec.getBigDecimal("amount")) .comment(rec.getString("comment")) + .reference("legacy data import") // TODO.spec: or use value from comment column? .build(); coopAssets.put(rec.getInteger("member_asset_id"), assetTransaction); -- 2.39.5 From abba21688e5c1c1d2f01118d3c433490ab80c7e4 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 9 Apr 2024 12:22:43 +0200 Subject: [PATCH 4/7] determine reverse entry for asset transaction adjustment in legacy data import --- .../hs/office/migration/ImportOfficeData.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java index 47e5a6ba..6e2f23f7 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java @@ -395,8 +395,8 @@ public class ImportOfficeData extends ContextBasedTest { 34001=CoopAssetsTransaction(M-1002000: 2016-12-31, CLEARING, -8.00, legacy data import, for cancellation D), 34002=CoopAssetsTransaction(M-1002000: 2016-12-31, DISBURSAL, -100.00, legacy data import, for cancellation D), 34003=CoopAssetsTransaction(M-1002000: 2016-12-31, LOSS, -20.00, legacy data import, for cancellation D), - 35001=CoopAssetsTransaction(M-1909000: 2024-01-15, DEPOSIT, 128.00, legacy data import, for subscription E), - 35002=CoopAssetsTransaction(M-1909000: 2024-01-20, ADJUSTMENT, -128.00, legacy data import, chargeback for subscription E) + 35001=CoopAssetsTransaction(M-1909000: 2024-01-15, DEPOSIT, 128.00, legacy data import, for subscription E, M-1909000:-128.00), + 35002=CoopAssetsTransaction(M-1909000: 2024-01-20, ADJUSTMENT, -128.00, legacy data import, chargeback for subscription E, M-1909000:+128.00) } """); } @@ -852,6 +852,18 @@ public class ImportOfficeData extends ContextBasedTest { .reference("legacy data import") // TODO.spec: or use value from comment column? .build(); + if (assetTransaction.getTransactionType() == HsOfficeCoopAssetsTransactionType.ADJUSTMENT) { + final var negativeValue = assetTransaction.getAssetValue().negate(); + final var reverseEntry = coopAssets.values().stream().filter(a -> + a.getTransactionType() != HsOfficeCoopAssetsTransactionType.ADJUSTMENT && + a.getMembership() == assetTransaction.getMembership() && + a.getAssetValue().equals(negativeValue)) + .findAny() + .orElseThrow(() -> new IllegalStateException("cannot determine asset reverse entry for adjustment " + assetTransaction)); + reverseEntry.setReverseEntry(assetTransaction); + assetTransaction.setReverseEntry(reverseEntry); + } + coopAssets.put(rec.getInteger("member_asset_id"), assetTransaction); }); } -- 2.39.5 From 2d32437c052b7079a8a6b99dac7fd3e6aa37019a Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 9 Apr 2024 13:28:01 +0200 Subject: [PATCH 5/7] constraint to verify reverseEntryUuid with other asset transaction entry --- .../5120-hs-office-coopassets.sql | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql index 587eb641..ff0b5cf6 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql @@ -27,6 +27,31 @@ create table if not exists hs_office_coopassetstransaction reverseEntryUuid uuid references hs_office_coopassetstransaction (uuid) deferrable , comment varchar(512) ); + +alter table hs_office_coopassetstransaction + add constraint hs_office_coopassetstransaction_reverse_entry_missing + check ( transactionType != 'ADJUSTMENT' or reverseEntryUuid is not null); + +CREATE OR REPLACE FUNCTION hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tf() + RETURNS TRIGGER + LANGUAGE plpgsql AS $$ +BEGIN + IF NEW.reverseEntryUuid IS NULL + OR NEW.uuid IN (SELECT other.reverseEntryUuid + FROM hs_office_coopassetstransaction other + WHERE other.uuid = NEW.reverseEntryUuid + AND NEW.membershipUuid = other.membershipUuid) + THEN + RETURN NEW; + END IF; + + RAISE EXCEPTION 'reverseEntryUuid must refer to a row that has a reference back to this row and belongs to the same membership'; +END; $$; + +-- FIXME: why does this not work? +-- CREATE TRIGGER hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tg +-- AFTER INSERT OR UPDATE ON hs_office_coopassetstransaction +-- FOR EACH ROW EXECUTE FUNCTION hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tf(); --// -- ============================================================================ -- 2.39.5 From 6365d344abdcd9f1837b0d1dfaf311a867682887 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 9 Apr 2024 13:28:04 +0200 Subject: [PATCH 6/7] determine reverse entry for asset transaction adjustment in legacy data import --- src/test/resources/application.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 40ae85bb..f80620c7 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -6,7 +6,7 @@ spring: datasource: url-tc: jdbc:tc:postgresql:15.5-bookworm:///spring_boot_testcontainers url-local: jdbc:postgresql://localhost:5432/postgres - url: ${spring.datasource.url-tc} + url: ${spring.datasource.url-local} username: postgres password: password -- 2.39.5 From 519b3a3894985d5d1bd44878da564b6e94f8c8bc Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 10 Apr 2024 10:25:30 +0200 Subject: [PATCH 7/7] add constraint hs_office_coopassetstransaction_reverse_entry_missing --- ...OfficeCoopAssetsTransactionController.java | 6 +-- .../HsOfficeCoopAssetsTransactionEntity.java | 9 ++-- .../hs-office-coopassets-schemas.yaml | 6 +-- .../5120-hs-office-coopassets.sql | 40 +++++---------- .../5128-hs-office-coopassets-test-data.sql | 8 ++- ...tsTransactionControllerAcceptanceTest.java | 51 +++++++++++-------- ...ceCoopAssetsTransactionEntityUnitTest.java | 4 +- ...sOfficeCoopAssetsTransactionRawEntity.java | 18 +++++++ ...sTransactionRepositoryIntegrationTest.java | 20 ++++---- .../hs/office/migration/ImportOfficeData.java | 7 ++- .../test/ContextBasedTestWithCleanup.java | 14 ++--- src/test/resources/application.yml | 2 +- 12 files changed, 96 insertions(+), 89 deletions(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRawEntity.java diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java index f72a855d..a22065c0 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionController.java @@ -65,10 +65,6 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse validate(requestBody); final var entityToSave = mapper.map(requestBody, HsOfficeCoopAssetsTransactionEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); - - if (entityToSave.getReverseEntry() != null) { - entityToSave.getReverseEntry().setReverseEntry(entityToSave); - } final var saved = coopAssetsTransactionRepo.save(entityToSave); final var uri = @@ -137,7 +133,7 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse final BiConsumer RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { if ( resource.getReverseEntryUuid() != null ) { - entity.setReverseEntry(coopAssetsTransactionRepo.findByUuid(resource.getReverseEntryUuid()) + entity.setAdjustedAssetTx(coopAssetsTransactionRepo.findByUuid(resource.getReverseEntryUuid()) .orElseThrow(() -> new EntityNotFoundException("ERROR: [400] reverseEntityUuid %s not found".formatted(resource.getReverseEntryUuid())))); } }; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java index 18e1f886..223852b5 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java @@ -50,7 +50,7 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO .withProp(HsOfficeCoopAssetsTransactionEntity::getAssetValue) .withProp(HsOfficeCoopAssetsTransactionEntity::getReference) .withProp(HsOfficeCoopAssetsTransactionEntity::getComment) - .withProp(at -> ofNullable(at.getReverseEntry()).map(HsOfficeCoopAssetsTransactionEntity::toShortString).orElse(null)) + .withProp(at -> ofNullable(at.getAdjustedAssetTx()).map(HsOfficeCoopAssetsTransactionEntity::toShortString).orElse(null)) .quotedValues(false); @Id @@ -95,12 +95,11 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO private String comment; /** - * Optionally, the UUID of the corresponding transaction for an adjustment transaction, - * linked in both directions. + * Optionally, the UUID of the corresponding transaction for an adjustment transaction. */ @OneToOne - @JoinColumn(name = "reverseentryuuid") - private HsOfficeCoopAssetsTransactionEntity reverseEntry; + @JoinColumn(name = "adjustedassettxuuid") + private HsOfficeCoopAssetsTransactionEntity adjustedAssetTx; public String getTaggedMemberNumber() { return ofNullable(membership).map(HsOfficeMembershipEntity::toShortString).orElse("M-?????"); diff --git a/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml b/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml index a11e11ef..8f31e062 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-coopassets-schemas.yaml @@ -32,10 +32,10 @@ components: type: string comment: type: string - reverseEntry: - $ref: '#/components/schemas/HsOfficeCoopAssetsTransactionReverse' + adjustedAssetTx: + $ref: '#/components/schemas/HsOfficeAdjustedCoopAssetsTransaction' - HsOfficeCoopAssetsTransactionReverse: + HsOfficeAdjustedCoopAssetsTransaction: description: Similar to `HsOfficeCoopAssetsTransaction` but without the `reverseEntry`, otherwise the JSON would be recursive. diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql index ff0b5cf6..289d5c2e 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql @@ -24,34 +24,20 @@ create table if not exists hs_office_coopassetstransaction valueDate date not null, assetValue money not null, reference varchar(48) not null, - reverseEntryUuid uuid references hs_office_coopassetstransaction (uuid) deferrable , + adjustedAssetTxUuid uuid unique REFERENCES hs_office_coopassetstransaction(uuid) DEFERRABLE INITIALLY DEFERRED, comment varchar(512) ); +--// + + +-- ============================================================================ +--changeset hs-office-coopassets-BUSINESS-RULES:1 endDelimiter:--// +-- ---------------------------------------------------------------------------- alter table hs_office_coopassetstransaction add constraint hs_office_coopassetstransaction_reverse_entry_missing - check ( transactionType != 'ADJUSTMENT' or reverseEntryUuid is not null); - -CREATE OR REPLACE FUNCTION hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tf() - RETURNS TRIGGER - LANGUAGE plpgsql AS $$ -BEGIN - IF NEW.reverseEntryUuid IS NULL - OR NEW.uuid IN (SELECT other.reverseEntryUuid - FROM hs_office_coopassetstransaction other - WHERE other.uuid = NEW.reverseEntryUuid - AND NEW.membershipUuid = other.membershipUuid) - THEN - RETURN NEW; - END IF; - - RAISE EXCEPTION 'reverseEntryUuid must refer to a row that has a reference back to this row and belongs to the same membership'; -END; $$; - --- FIXME: why does this not work? --- CREATE TRIGGER hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tg --- AFTER INSERT OR UPDATE ON hs_office_coopassetstransaction --- FOR EACH ROW EXECUTE FUNCTION hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tf(); + check ( transactionType = 'ADJUSTMENT' and adjustedAssetTxUuid is not null + or transactionType <> 'ADJUSTMENT' and adjustedAssetTxUuid is null); --// -- ============================================================================ @@ -66,9 +52,9 @@ declare totalAssetValue money; begin select sum(cat.assetValue) - from hs_office_coopassetstransaction cat - where cat.membershipUuid = forMembershipUuid - into currentAssetValue; + from hs_office_coopassetstransaction cat + where cat.membershipUuid = forMembershipUuid + into currentAssetValue; totalAssetValue := currentAssetValue + newAssetValue; if totalAssetValue::numeric < 0 then raise exception '[400] coop assets transaction would result in a negative balance of assets'; @@ -79,9 +65,9 @@ end; $$; alter table hs_office_coopassetstransaction add constraint hs_office_coopassets_positive check ( checkAssetsByMembershipUuid(membershipUuid, assetValue) ); - --// + -- ============================================================================ --changeset hs-office-coopassets-MAIN-TABLE-JOURNAL:1 endDelimiter:--// -- ---------------------------------------------------------------------------- diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql index 7bedc7c5..1eda1de6 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql @@ -17,7 +17,6 @@ declare currentTask varchar; membership hs_office_membership; lossEntryUuid uuid; - adjustmentEntryUuid uuid; begin currentTask = 'creating coopAssetsTransaction test-data ' || givenPartnerNumber || givenMemberNumberSuffix; execute format('set local hsadminng.currentTask to %L', currentTask); @@ -33,14 +32,13 @@ begin raise notice 'creating test coopAssetsTransaction: %', givenPartnerNumber || givenMemberNumberSuffix; lossEntryUuid := uuid_generate_v4(); - adjustmentEntryUuid := uuid_generate_v4(); insert - into hs_office_coopassetstransaction(uuid, membershipuuid, transactiontype, valuedate, assetvalue, reference, comment, reverseEntryUuid) + into hs_office_coopassetstransaction(uuid, membershipuuid, transactiontype, valuedate, assetvalue, reference, comment, adjustedAssetTxUuid) values (uuid_generate_v4(), membership.uuid, 'DEPOSIT', '2010-03-15', 320.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-1', 'initial deposit', null), (uuid_generate_v4(), membership.uuid, 'DISBURSAL', '2021-09-01', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-2', 'partial disbursal', null), - (lossEntryUuid, membership.uuid, 'LOSS', '2022-10-20', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some loss', adjustmentEntryUuid), - (adjustmentEntryUuid, membership.uuid, 'ADJUSTMENT', '2022-10-21', 128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some adjustment', lossEntryUuid); + (lossEntryUuid, membership.uuid, 'DEPOSIT', '2022-10-20', 128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some loss', null), + (uuid_generate_v4(), membership.uuid, 'ADJUSTMENT', '2022-10-21', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some adjustment', lossEntryUuid); end; $$; --// diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java index 64393d52..1d9bebbe 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java @@ -19,10 +19,11 @@ import org.springframework.transaction.annotation.Transactional; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import java.math.BigDecimal; import java.time.LocalDate; import java.util.UUID; -import static net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType.LOSS; +import static net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType.DEPOSIT; import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid; import static net.hostsharing.test.JsonMatcher.lenientlyEquals; import static org.assertj.core.api.Assertions.assertThat; @@ -105,15 +106,15 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased "comment": "partial disbursal" }, { - "transactionType": "LOSS", - "assetValue": -128.00, + "transactionType": "DEPOSIT", + "assetValue": 128.00, "valueDate": "2022-10-20", "reference": "ref 1000202-3", "comment": "some loss" }, { "transactionType": "ADJUSTMENT", - "assetValue": 128.00, + "assetValue": -128.00, "valueDate": "2022-10-21", "reference": "ref 1000202-3", "comment": "some adjustment" @@ -196,9 +197,9 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased .extract().header("Location"); // @formatter:on // finally, the new coopAssetsTransaction can be accessed under the generated UUID - final var newUserUuid = UUID.fromString( + final var newAssetTxUuid = UUID.fromString( location.substring(location.lastIndexOf('/') + 1)); - assertThat(newUserUuid).isNotNull(); + assertThat(newAssetTxUuid).isNotNull(); } @Test @@ -206,10 +207,18 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased context.define("superuser-alex@hostsharing.net"); final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101); - final var givenTransaction = coopAssetsTransactionRepo.findCoopAssetsTransactionByOptionalMembershipUuidAndDateRange(givenMembership.getUuid(), null, null) - .stream().filter(at -> at.getTransactionType() == LOSS) - .findFirst() - .orElseThrow(); + final var givenTransaction = jpaAttempt.transacted(() -> { + // TODO.impl: introduce something like transactedAsSuperuser / transactedAs("...", ...) + context.define("superuser-alex@hostsharing.net"); + return coopAssetsTransactionRepo.save(HsOfficeCoopAssetsTransactionEntity.builder() + .transactionType(DEPOSIT) + .valueDate(LocalDate.of(2022, 10, 20)) + .membership(givenMembership) + .assetValue(new BigDecimal("256.00")) + .reference("test ref") + .build()); + }).assertSuccessful().assertNotNull().returnedValue(); + toCleanup(HsOfficeCoopAssetsTransactionRawEntity.class, givenTransaction.getUuid()); final var location = RestAssured // @formatter:off .given() @@ -221,7 +230,7 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased "transactionType": "ADJUSTMENT", "assetValue": %s, "valueDate": "2022-10-30", - "reference": "temp ref A", + "reference": "test ref adjustment", "comment": "some coop assets adjustment transaction", "reverseEntryUuid": "%s" } @@ -239,16 +248,15 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased .body("", lenientlyEquals(""" { "transactionType": "ADJUSTMENT", - "assetValue": 128.00, + "assetValue": -256.00, "valueDate": "2022-10-30", - "reference": "temp ref A", + "reference": "test ref adjustment", "comment": "some coop assets adjustment transaction", - "reverseEntry": { - "transactionType": "LOSS", - "assetValue": -128.00, + "adjustedAssetTx": { + "transactionType": "DEPOSIT", + "assetValue": 256.00, "valueDate": "2022-10-20", - "reference": "ref 1000101-3", - "comment": "some loss" + "reference": "test ref" } } """.formatted(givenTransaction.getUuid()))) @@ -256,9 +264,10 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased .extract().header("Location"); // @formatter:on // finally, the new coopAssetsTransaction can be accessed under the generated UUID - final var newUserUuid = UUID.fromString( + final var newAssetTxUuid = UUID.fromString( location.substring(location.lastIndexOf('/') + 1)); - assertThat(newUserUuid).isNotNull(); + assertThat(newAssetTxUuid).isNotNull(); + toCleanup(HsOfficeCoopAssetsTransactionRawEntity.class, newAssetTxUuid); } @Test @@ -267,7 +276,7 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest extends ContextBased context.define("superuser-alex@hostsharing.net"); final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101); - final var location = RestAssured // @formatter:off + RestAssured // @formatter:off .given() .header("current-user", "superuser-alex@hostsharing.net") .contentType(ContentType.JSON) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java index c7a0690c..3a9d07d7 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntityUnitTest.java @@ -27,7 +27,7 @@ class HsOfficeCoopAssetsTransactionEntityUnitTest { .transactionType(HsOfficeCoopAssetsTransactionType.ADJUSTMENT) .assetValue(new BigDecimal("-128.00")) .comment("some comment") - .reverseEntry(givenCoopAssetTransaction) + .adjustedAssetTx(givenCoopAssetTransaction) .build(); final HsOfficeCoopAssetsTransactionEntity givenEmptyCoopAssetsTransaction = HsOfficeCoopAssetsTransactionEntity.builder().build(); @@ -41,7 +41,7 @@ class HsOfficeCoopAssetsTransactionEntityUnitTest { @Test void toStringWithReverseEntryContainsReverseEntry() { - givenCoopAssetTransaction.setReverseEntry(givenCoopAssetAdjustmentTransaction); + givenCoopAssetTransaction.setAdjustedAssetTx(givenCoopAssetAdjustmentTransaction); final var result = givenCoopAssetTransaction.toString(); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRawEntity.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRawEntity.java new file mode 100644 index 00000000..dc7852e8 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRawEntity.java @@ -0,0 +1,18 @@ + +package net.hostsharing.hsadminng.hs.office.coopassets; + +import lombok.NoArgsConstructor; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.util.UUID; + +@Entity +@Table(name = "hs_office_coopassetstransaction") +@NoArgsConstructor +public class HsOfficeCoopAssetsTransactionRawEntity { + + @Id + private UUID uuid; +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java index 4f8c20b5..44adc58b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java @@ -142,18 +142,18 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase result, "CoopAssetsTransaction(M-1000101: 2010-03-15, DEPOSIT, 320.00, ref 1000101-1, initial deposit)", "CoopAssetsTransaction(M-1000101: 2021-09-01, DISBURSAL, -128.00, ref 1000101-2, partial disbursal)", - "CoopAssetsTransaction(M-1000101: 2022-10-20, LOSS, -128.00, ref 1000101-3, some loss, M-1000101:+128.00)", - "CoopAssetsTransaction(M-1000101: 2022-10-21, ADJUSTMENT, 128.00, ref 1000101-3, some adjustment, M-1000101:-128.00)", + "CoopAssetsTransaction(M-1000101: 2022-10-20, DEPOSIT, 128.00, ref 1000101-3, some loss)", + "CoopAssetsTransaction(M-1000101: 2022-10-21, ADJUSTMENT, -128.00, ref 1000101-3, some adjustment, M-1000101:+128.00)", "CoopAssetsTransaction(M-1000202: 2010-03-15, DEPOSIT, 320.00, ref 1000202-1, initial deposit)", "CoopAssetsTransaction(M-1000202: 2021-09-01, DISBURSAL, -128.00, ref 1000202-2, partial disbursal)", - "CoopAssetsTransaction(M-1000202: 2022-10-20, LOSS, -128.00, ref 1000202-3, some loss, M-1000202:+128.00)", - "CoopAssetsTransaction(M-1000202: 2022-10-21, ADJUSTMENT, 128.00, ref 1000202-3, some adjustment, M-1000202:-128.00)", + "CoopAssetsTransaction(M-1000202: 2022-10-20, DEPOSIT, 128.00, ref 1000202-3, some loss)", + "CoopAssetsTransaction(M-1000202: 2022-10-21, ADJUSTMENT, -128.00, ref 1000202-3, some adjustment, M-1000202:+128.00)", "CoopAssetsTransaction(M-1000303: 2010-03-15, DEPOSIT, 320.00, ref 1000303-1, initial deposit)", "CoopAssetsTransaction(M-1000303: 2021-09-01, DISBURSAL, -128.00, ref 1000303-2, partial disbursal)", - "CoopAssetsTransaction(M-1000303: 2022-10-20, LOSS, -128.00, ref 1000303-3, some loss, M-1000303:+128.00)", - "CoopAssetsTransaction(M-1000303: 2022-10-21, ADJUSTMENT, 128.00, ref 1000303-3, some adjustment, M-1000303:-128.00)"); + "CoopAssetsTransaction(M-1000303: 2022-10-20, DEPOSIT, 128.00, ref 1000303-3, some loss)", + "CoopAssetsTransaction(M-1000303: 2022-10-21, ADJUSTMENT, -128.00, ref 1000303-3, some adjustment, M-1000303:+128.00)"); } @Test @@ -173,8 +173,8 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase result, "CoopAssetsTransaction(M-1000202: 2010-03-15, DEPOSIT, 320.00, ref 1000202-1, initial deposit)", "CoopAssetsTransaction(M-1000202: 2021-09-01, DISBURSAL, -128.00, ref 1000202-2, partial disbursal)", - "CoopAssetsTransaction(M-1000202: 2022-10-20, LOSS, -128.00, ref 1000202-3, some loss, M-1000202:+128.00)", - "CoopAssetsTransaction(M-1000202: 2022-10-21, ADJUSTMENT, 128.00, ref 1000202-3, some adjustment, M-1000202:-128.00)"); + "CoopAssetsTransaction(M-1000202: 2022-10-20, DEPOSIT, 128.00, ref 1000202-3, some loss)", + "CoopAssetsTransaction(M-1000202: 2022-10-21, ADJUSTMENT, -128.00, ref 1000202-3, some adjustment, M-1000202:+128.00)"); } @Test @@ -211,8 +211,8 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase result, "CoopAssetsTransaction(M-1000101: 2010-03-15, DEPOSIT, 320.00, ref 1000101-1, initial deposit)", "CoopAssetsTransaction(M-1000101: 2021-09-01, DISBURSAL, -128.00, ref 1000101-2, partial disbursal)", - "CoopAssetsTransaction(M-1000101: 2022-10-20, LOSS, -128.00, ref 1000101-3, some loss, M-1000101:+128.00)", - "CoopAssetsTransaction(M-1000101: 2022-10-21, ADJUSTMENT, 128.00, ref 1000101-3, some adjustment, M-1000101:-128.00)"); + "CoopAssetsTransaction(M-1000101: 2022-10-20, DEPOSIT, 128.00, ref 1000101-3, some loss)", + "CoopAssetsTransaction(M-1000101: 2022-10-21, ADJUSTMENT, -128.00, ref 1000101-3, some adjustment, M-1000101:+128.00)"); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java index 6e2f23f7..71a1f2fc 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportOfficeData.java @@ -395,7 +395,7 @@ public class ImportOfficeData extends ContextBasedTest { 34001=CoopAssetsTransaction(M-1002000: 2016-12-31, CLEARING, -8.00, legacy data import, for cancellation D), 34002=CoopAssetsTransaction(M-1002000: 2016-12-31, DISBURSAL, -100.00, legacy data import, for cancellation D), 34003=CoopAssetsTransaction(M-1002000: 2016-12-31, LOSS, -20.00, legacy data import, for cancellation D), - 35001=CoopAssetsTransaction(M-1909000: 2024-01-15, DEPOSIT, 128.00, legacy data import, for subscription E, M-1909000:-128.00), + 35001=CoopAssetsTransaction(M-1909000: 2024-01-15, DEPOSIT, 128.00, legacy data import, for subscription E), 35002=CoopAssetsTransaction(M-1909000: 2024-01-20, ADJUSTMENT, -128.00, legacy data import, chargeback for subscription E, M-1909000:+128.00) } """); @@ -854,14 +854,13 @@ public class ImportOfficeData extends ContextBasedTest { if (assetTransaction.getTransactionType() == HsOfficeCoopAssetsTransactionType.ADJUSTMENT) { final var negativeValue = assetTransaction.getAssetValue().negate(); - final var reverseEntry = coopAssets.values().stream().filter(a -> + final var adjustedAssetTx = coopAssets.values().stream().filter(a -> a.getTransactionType() != HsOfficeCoopAssetsTransactionType.ADJUSTMENT && a.getMembership() == assetTransaction.getMembership() && a.getAssetValue().equals(negativeValue)) .findAny() .orElseThrow(() -> new IllegalStateException("cannot determine asset reverse entry for adjustment " + assetTransaction)); - reverseEntry.setReverseEntry(assetTransaction); - assetTransaction.setReverseEntry(reverseEntry); + assetTransaction.setAdjustedAssetTx(adjustedAssetTx); } coopAssets.put(rec.getInteger("member_asset_id"), assetTransaction); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java b/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java index 64feda26..3fe3bd91 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/test/ContextBasedTestWithCleanup.java @@ -64,8 +64,10 @@ public abstract class ContextBasedTestWithCleanup extends ContextBasedTest { return merged; } - public UUID toCleanup(final Class entityClass, final UUID uuidToCleanup) { - out.println("toCleanup(" + entityClass.getSimpleName() + ", " + uuidToCleanup); + // TODO.test: back to `Class entityClass` but delete on raw table + // remove HsOfficeCoopAssetsTransactionRawEntity, which is not needed anymore after this change + public UUID toCleanup(final Class entityClass, final UUID uuidToCleanup) { + out.println("toCleanup(" + entityClass.getSimpleName() + ", " + uuidToCleanup + ")"); entitiesToCleanup.put(uuidToCleanup, entityClass); return uuidToCleanup; } @@ -120,7 +122,7 @@ public abstract class ContextBasedTestWithCleanup extends ContextBasedTest { } if (initialRbacObjects != null){ - assertNoNewRbackObjectsRolesAndGrantsLeaked(); + assertNoNewRbacObjectsRolesAndGrantsLeaked(); } initialTestDataValidated = false; @@ -170,7 +172,7 @@ public abstract class ContextBasedTestWithCleanup extends ContextBasedTest { out.println(ContextBasedTestWithCleanup.class.getSimpleName() + ".cleanupAndCheckCleanup"); cleanupTemporaryTestData(); deleteLeakedRbacObjects(); - long rbacObjectCount = assertNoNewRbackObjectsRolesAndGrantsLeaked(); + long rbacObjectCount = assertNoNewRbacObjectsRolesAndGrantsLeaked(); out.println("TOTAL OBJECT COUNT (after): " + rbacObjectCount); } @@ -180,7 +182,7 @@ public abstract class ContextBasedTestWithCleanup extends ContextBasedTest { final var caughtException = jpaAttempt.transacted(() -> { context.define("superuser-alex@hostsharing.net", null); em.remove(em.getReference(entityClass, uuid)); - out.println("DELETING temporary " + entityClass.getSimpleName() + "#" + uuid + " successful"); + out.println("DELETING temporary " + entityClass.getSimpleName() + "#" + uuid + " generated"); }).caughtException(); if (caughtException != null) { out.println("DELETING temporary " + entityClass.getSimpleName() + "#" + uuid + " failed: " + caughtException); @@ -188,7 +190,7 @@ public abstract class ContextBasedTestWithCleanup extends ContextBasedTest { }); } - private long assertNoNewRbackObjectsRolesAndGrantsLeaked() { + private long assertNoNewRbacObjectsRolesAndGrantsLeaked() { return jpaAttempt.transacted(() -> { context.define("superuser-alex@hostsharing.net"); assertEqual(initialRbacObjects, allRbacObjects()); diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index f80620c7..40ae85bb 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -6,7 +6,7 @@ spring: datasource: url-tc: jdbc:tc:postgresql:15.5-bookworm:///spring_boot_testcontainers url-local: jdbc:postgresql://localhost:5432/postgres - url: ${spring.datasource.url-local} + url: ${spring.datasource.url-tc} username: postgres password: password -- 2.39.5