| 64 | {{{ |
| 65 | create or replace function after_transaction_trigger() |
| 66 | returns trigger as $$ |
| 67 | begin |
| 68 | |
| 69 | if new.type = 'Deposit' then |
| 70 | insert into Deposit(account_id,amount, timestamp) |
| 71 | values (new.account_id, new.amount, new.timestamp); |
| 72 | |
| 73 | elsif new.type = 'Withdrawal' then |
| 74 | insert into Withdraw(account_id,amount, timestamp) |
| 75 | values (new.account_id, new.amount, new.timestamp); |
| 76 | |
| 77 | elsif new.type like '(MKD) Transfer Out to%' then |
| 78 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 79 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 80 | new.id, 'MKD'; |
| 81 | |
| 82 | elsif new.type like '(USD) Transfer Out to%' then |
| 83 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 84 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 85 | new.id, 'USD'; |
| 86 | |
| 87 | elsif new.type like '(EUR) Transfer Out to%' then |
| 88 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 89 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 90 | new.id, 'EUR'; |
| 91 | |
| 92 | elsif new.type like '(GBP) Transfer Out to%' then |
| 93 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 94 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 95 | new.id, 'GBP'; |
| 96 | |
| 97 | end if; |
| 98 | return new; |
| 99 | end; |
| 100 | $$ language plpgsql; |
| 101 | |
| 102 | create trigger after_transaction |
| 103 | after insert on transaction |
| 104 | for each row |
| 105 | execute function after_transaction_trigger() |
| 106 | |
| 107 | }}} |