-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0116-function.sr.fn-bank-transaction.sql
More file actions
67 lines (62 loc) · 1.89 KB
/
Copy path0116-function.sr.fn-bank-transaction.sql
File metadata and controls
67 lines (62 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- Database: sr
-- Tables: coin_ledger
-- Function: fn_bank_transaction
-- Description: Executes bank transaction and returns sender and receiver balances
DROP FUNCTION IF EXISTS sr.fn_bank_transaction(UUID,UUID,UUID,DOUBLE PRECISION,BIGINT,JSONB);
CREATE OR REPLACE FUNCTION sr.fn_bank_transaction(
new_transaction_uuid UUID,
new_sending_entity_uuid UUID,
new_receiving_entity_uuid UUID,
new_transaction_value DOUBLE PRECISION,
new_transaction_status_id BIGINT,
new_transaction_details JSONB
) RETURNS TABLE (
bank_balances JSON
)
LANGUAGE plpgsql VOLATILE AS
$BODY$
BEGIN
-- Perform the transaction
INSERT
INTO
sr.coin_ledger (
transaction_uuid,
sending_entity_uuid,
receiving_entity_uuid,
transaction_value,
transaction_status_id,
transaction_details
)
VALUES (
UUID(new_transaction_uuid),
UUID(new_sending_entity_uuid),
UUID(new_receiving_entity_uuid),
new_transaction_value,
new_transaction_status_id,
JSONB(new_transaction_details)
);
-- Return resulting balances
RETURN QUERY(
SELECT
row_to_json(balances) AS bank_balances
FROM (
SELECT
scb.balance_value AS sender_coin_balance,
rcb.balance_value AS receiver_coin_balance
FROM
(
SELECT
balance_value
FROM
sr.fn_bank_balance(new_sending_entity_uuid) AS sender_coin_balance
) AS scb,
(
SELECT
balance_value
FROM
sr.fn_bank_balance(new_receiving_entity_uuid) AS receiver_coin_balance
) AS rcb
) AS balances
);
END
$BODY$;