2022-10-18 14:44:10 +02:00
|
|
|
--liquibase formatted sql
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset michael.hoennig:hs-office-coopshares-MAIN-TABLE endDelimiter:--//
|
2022-10-18 14:44:10 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-11-13 07:08:29 +01:00
|
|
|
CREATE TYPE hs_office.CoopSharesTransactionType AS ENUM ('REVERSAL', 'SUBSCRIPTION', 'CANCELLATION');
|
2022-10-18 14:44:10 +02:00
|
|
|
|
2024-09-23 10:52:37 +02:00
|
|
|
CREATE CAST (character varying as hs_office.CoopSharesTransactionType) WITH INOUT AS IMPLICIT;
|
2022-10-18 14:44:10 +02:00
|
|
|
|
2024-09-23 10:52:37 +02:00
|
|
|
create table if not exists hs_office.coopsharetx
|
2022-10-18 14:44:10 +02:00
|
|
|
(
|
2024-09-16 15:36:37 +02:00
|
|
|
uuid uuid unique references rbac.object (uuid) initially deferred,
|
2024-04-08 11:21:22 +02:00
|
|
|
version int not null default 0,
|
2024-09-18 10:28:21 +02:00
|
|
|
membershipUuid uuid not null references hs_office.membership(uuid),
|
2024-09-23 10:52:37 +02:00
|
|
|
transactionType hs_office.CoopSharesTransactionType not null,
|
2022-10-18 14:44:10 +02:00
|
|
|
valueDate date not null,
|
2024-04-12 11:29:26 +02:00
|
|
|
shareCount integer not null,
|
|
|
|
reference varchar(48) not null,
|
2024-11-13 06:50:55 +01:00
|
|
|
revertedShareTxUuid uuid unique REFERENCES hs_office.coopsharetx(uuid) DEFERRABLE INITIALLY DEFERRED,
|
2022-10-18 14:44:10 +02:00
|
|
|
comment varchar(512)
|
|
|
|
);
|
|
|
|
--//
|
|
|
|
|
2024-04-12 11:29:26 +02:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset michael.hoennig:hs-office-coopshares-BUSINESS-RULES endDelimiter:--//
|
2024-04-12 11:29:26 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-09-23 10:52:37 +02:00
|
|
|
alter table hs_office.coopsharetx
|
2024-09-18 10:28:21 +02:00
|
|
|
add constraint reverse_entry_missing
|
2024-11-13 07:08:29 +01:00
|
|
|
check ( transactionType = 'REVERSAL' and revertedShareTxUuid is not null
|
|
|
|
or transactionType <> 'REVERSAL' and revertedShareTxUuid is null);
|
2024-04-12 11:29:26 +02:00
|
|
|
--//
|
|
|
|
|
2022-10-20 14:08:19 +02:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset michael.hoennig:hs-office-coopshares-SHARE-COUNT-CONSTRAINT endDelimiter:--//
|
2022-10-20 14:08:19 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-09-23 10:52:37 +02:00
|
|
|
create or replace function hs_office.coopsharestx_check_positive_total(forMembershipUuid UUID, newShareCount integer)
|
2022-10-20 14:08:19 +02:00
|
|
|
returns boolean
|
|
|
|
language plpgsql as $$
|
|
|
|
declare
|
|
|
|
currentShareCount integer;
|
|
|
|
totalShareCount integer;
|
|
|
|
begin
|
|
|
|
select sum(cst.shareCount)
|
2024-09-23 10:52:37 +02:00
|
|
|
from hs_office.coopsharetx cst
|
2022-10-20 14:08:19 +02:00
|
|
|
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; $$;
|
|
|
|
|
2024-09-23 10:52:37 +02:00
|
|
|
alter table hs_office.coopsharetx
|
2024-09-18 10:28:21 +02:00
|
|
|
add constraint check_positive_total_shares_count
|
2024-09-23 10:52:37 +02:00
|
|
|
check ( hs_office.coopsharestx_check_positive_total(membershipUuid, shareCount) );
|
2022-10-20 14:08:19 +02:00
|
|
|
|
|
|
|
--//
|
2022-10-18 14:44:10 +02:00
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset michael.hoennig:hs-office-coopshares-MAIN-TABLE-JOURNAL endDelimiter:--//
|
2022-10-18 14:44:10 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-09-23 10:52:37 +02:00
|
|
|
call base.create_journal('hs_office.coopsharetx');
|
2022-10-18 14:44:10 +02:00
|
|
|
--//
|