-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathext.multiaccs.sql
More file actions
149 lines (124 loc) · 4.15 KB
/
ext.multiaccs.sql
File metadata and controls
149 lines (124 loc) · 4.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
--
-- multi-accounts by user
-- if the user has bonus, credit and other accounts
-- required special format accounts.key for multi-accounts by user
-- required key format (or similar)
-- 'v1.u2.t3.some.fields'
-- v1 - version
-- u2 - userID
-- t3 - type account (main, credit, bonus, etc)
-- some.fields - some values
CREATE OR REPLACE FUNCTION acca.ma_get_user_id(key ltree, OUT v varchar)
RETURNS varchar AS $$
begin
IF NOT 'ma' @> key THEN
RAISE EXCEPTION 'Invalid key of account - want prefix "ma", got %', key::text;
END IF;
v := cast(subltree(key,1, 2) as varchar);
end;
$$ language plpgsql;
-- return account type
-- if not exists
-- helper function
CREATE OR REPLACE FUNCTION acca.ma_get_type(key ltree, OUT v varchar)
RETURNS varchar AS $$
begin
IF NOT 'ma' @> key THEN
RAISE EXCEPTION 'Invalid key of account - want prefix "ma", got %', key::text;
END IF;
v := cast(subltree(key,2, 3) as varchar);
end;
$$ language plpgsql;
CREATE OR REPLACE VIEW acca.ma_accounts AS
SELECT
acca.ma_get_user_id(key) AS user_id,
array_agg(acc_id) AS acc_ids,
array_to_json(array_agg(json_build_object('id', acc_id, 'b',balance, 't', acca.ma_get_type(key), 'balance_accepted', balance_accepted)))::jsonb as ma_balances
FROM acca.accounts
WHERE 'ma' @> key
GROUP BY user_id;
ALTER TABLE acca.balance_changes ADD COLUMN ma_balance jsonb;
CREATE FUNCTION ma_update_balance() RETURNS trigger AS $$
DECLARE
_ma_balances jsonb;
_key ltree;
BEGIN
SELECT key INTO _key FROM acca.accounts WHERE acc_id = NEW.acc_id;
IF NOT 'ma' @> _key THEN
RETURN NEW;
END IF;
select array_to_json(array_agg(json_build_object('id', acc_id, 'b',balance, 't', acca.ma_get_type(key), 'balance_accepted', balance_accepted)))::jsonb INTO _ma_balances
FROM acca.accounts
WHERE subltree(_key, 0, 2) @> key;
UPDATE acca.balance_changes SET ma_balance = _ma_balances WHERE ch_id = NEW.ch_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_ma_balance_by_bc_trigger AFTER INSERT ON acca.balance_changes
FOR EACH ROW EXECUTE PROCEDURE ma_update_balance();
-- REPLACE exists view
DROP VIEW acca.recent_activity;
CREATE OR REPLACE VIEW acca.recent_activity AS
SELECT
bc.ch_id as id,
bc.oper_id as oper_id,
bc.acc_id as acc_id,
bc.amount as amount,
bc.balance as balance,
bc.ma_balance as ma_balances,
-- operations
o.tx_id as tx_id,
o.src_acc_id as src_acc_id,
o.dst_acc_id as dst_acc_id,
-- o.type as type,
o.reason as reason,
-- o.meta as meta,
-- o.hold as hold,
-- o.hold_acc_id as hold_acc_id,
o.status as oper_status,
-- transactions
t.reason AS tx_reason,
t.status AS tx_status,
-- accounts
a.key AS acc_key,
-- currency
c.curr_id AS acc_curr_id,
c.key AS acc_curr_key
FROM acca.balance_changes bc
LEFT JOIN acca.operations o USING(oper_id)
LEFT JOIN acca.transactions t USING(tx_id)
LEFT JOIN acca.accounts a USING(acc_id)
LEFT JOIN acca.currencies c USING(curr_id)
ORDER BY id DESC;
CREATE OR REPLACE VIEW acca.journal_activity AS
SELECT
bc.ch_id as id,
bc.oper_id as oper_id,
bc.acc_id as acc_id,
bc.amount as amount,
bc.balance as balance,
bc.ma_balance as ma_balances,
-- operations
o.tx_id as tx_id,
o.src_acc_id as src_acc_id,
o.dst_acc_id as dst_acc_id,
-- o.type as type,
o.reason as reason,
-- o.meta as meta,
-- o.hold as hold,
-- o.hold_acc_id as hold_acc_id,
o.status as oper_status,
-- transactions
t.reason AS tx_reason,
t.status AS tx_status,
-- accounts
a.key AS acc_key,
-- currency
c.curr_id AS acc_curr_id,
c.key AS acc_curr_key
FROM acca.balance_changes bc
LEFT JOIN acca.operations o USING(oper_id)
LEFT JOIN acca.transactions t USING(tx_id)
LEFT JOIN acca.accounts a USING(acc_id)
LEFT JOIN acca.currencies c USING(curr_id)
ORDER BY id;