1
0

hs-office-coopshares: add non-negative validation

This commit is contained in:
Michael Hoennig
2022-10-20 14:08:19 +02:00
parent 22bca20c49
commit dc0835fa25
6 changed files with 95 additions and 33 deletions

View File

@ -18,7 +18,7 @@ components:
format: uuid
transactionType:
$ref: '#/components/schemas/HsOfficeCoopSharesTransactionType'
sharesCount:
shareCount:
type: integer
valueDate:
type: string
@ -37,7 +37,7 @@ components:
nullable: false
transactionType:
$ref: '#/components/schemas/HsOfficeCoopSharesTransactionType'
sharesCount:
shareCount:
type: integer
valueDate:
type: string
@ -51,7 +51,7 @@ components:
required:
- membershipUuid
- transactionType
- sharesCount
- shareCount
- valueDate
- reference
additionalProperties: false

View File

@ -20,6 +20,33 @@ create table if not exists hs_office_coopsharestransaction
);
--//
-- ============================================================================
--changeset hs-office-coopshares-SHARE-COUNT-CONSTRAINT:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
create or replace function checkSharesByMembershipUuid(forMembershipUuid UUID, newShareCount integer)
returns boolean
language plpgsql as $$
declare
currentShareCount integer;
totalShareCount integer;
begin
select sum(cst.shareCount)
from hs_office_coopsharestransaction cst
where cst.membershipUuid = forMembershipUuid
into currentShareCount;
totalShareCount := currentShareCount + newShareCount;
if totalShareCount < 0 then
raise exception '[400] coop shares transaction would result in a negative number of shares';
end if;
return true;
end; $$;
alter table hs_office_coopsharestransaction
add constraint hs_office_coopshares_positive
check ( checkSharesByMembershipUuid(membershipUuid, shareCount) );
--//
-- ============================================================================
--changeset hs-office-coopshares-MAIN-TABLE-JOURNAL:1 endDelimiter:--//