coop-assets-transaction-reverse-entry (#37)
Co-authored-by: Michael Hoennig <michael@hoennig.de> Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/37 Reviewed-by: Timotheus Pokorra <timotheus.pokorra@hostsharing.net>
This commit is contained in:
@ -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,7 @@ 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);
|
||||
final var saved = coopAssetsTransactionRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
@ -131,4 +131,10 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
final BiConsumer<HsOfficeCoopAssetsTransactionInsertResource, HsOfficeCoopAssetsTransactionEntity> RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
|
||||
if ( resource.getReverseEntryUuid() != null ) {
|
||||
entity.setAdjustedAssetTx(coopAssetsTransactionRepo.findByUuid(resource.getReverseEntryUuid())
|
||||
.orElseThrow(() -> new EntityNotFoundException("ERROR: [400] reverseEntityUuid %s not found".formatted(resource.getReverseEntryUuid()))));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -50,6 +50,7 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO
|
||||
.withProp(HsOfficeCoopAssetsTransactionEntity::getAssetValue)
|
||||
.withProp(HsOfficeCoopAssetsTransactionEntity::getReference)
|
||||
.withProp(HsOfficeCoopAssetsTransactionEntity::getComment)
|
||||
.withProp(at -> ofNullable(at.getAdjustedAssetTx()).map(HsOfficeCoopAssetsTransactionEntity::toShortString).orElse(null))
|
||||
.quotedValues(false);
|
||||
|
||||
@Id
|
||||
@ -93,6 +94,12 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, RbacO
|
||||
@Column(name = "comment")
|
||||
private String comment;
|
||||
|
||||
/**
|
||||
* Optionally, the UUID of the corresponding transaction for an adjustment transaction.
|
||||
*/
|
||||
@OneToOne
|
||||
@JoinColumn(name = "adjustedassettxuuid")
|
||||
private HsOfficeCoopAssetsTransactionEntity adjustedAssetTx;
|
||||
|
||||
public String getTaggedMemberNumber() {
|
||||
return ofNullable(membership).map(HsOfficeMembershipEntity::toShortString).orElse("M-?????");
|
||||
|
@ -19,7 +19,13 @@ import org.hibernate.annotations.JoinFormula;
|
||||
import org.hibernate.annotations.NotFound;
|
||||
import org.hibernate.annotations.NotFoundAction;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
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.persistence.Version;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import java.io.IOException;
|
||||
|
@ -32,6 +32,30 @@ components:
|
||||
type: string
|
||||
comment:
|
||||
type: string
|
||||
adjustedAssetTx:
|
||||
$ref: '#/components/schemas/HsOfficeAdjustedCoopAssetsTransaction'
|
||||
|
||||
HsOfficeAdjustedCoopAssetsTransaction:
|
||||
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
|
||||
|
@ -17,17 +17,29 @@ 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,
|
||||
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' and adjustedAssetTxUuid is not null
|
||||
or transactionType <> 'ADJUSTMENT' and adjustedAssetTxUuid is null);
|
||||
--//
|
||||
|
||||
-- ============================================================================
|
||||
--changeset hs-office-coopassets-ASSET-VALUE-CONSTRAINT:1 endDelimiter:--//
|
||||
-- ----------------------------------------------------------------------------
|
||||
@ -40,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';
|
||||
@ -53,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:--//
|
||||
-- ----------------------------------------------------------------------------
|
||||
|
@ -14,11 +14,13 @@ create or replace procedure createHsOfficeCoopAssetsTransactionTestData(
|
||||
)
|
||||
language plpgsql as $$
|
||||
declare
|
||||
currentTask varchar;
|
||||
membership hs_office_membership;
|
||||
currentTask varchar;
|
||||
membership hs_office_membership;
|
||||
lossEntryUuid 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 +31,14 @@ begin
|
||||
into membership;
|
||||
|
||||
raise notice 'creating test coopAssetsTransaction: %', givenPartnerNumber || givenMemberNumberSuffix;
|
||||
lossEntryUuid := 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, adjustedAssetTxUuid)
|
||||
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, '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; $$;
|
||||
--//
|
||||
|
||||
|
Reference in New Issue
Block a user