Skip to content

Commit

Permalink
Update balance query to show all transactions and to put a hold after…
Browse files Browse the repository at this point in the history
… confirmed amounts until registration is complete or cancels. #6
  • Loading branch information
james committed Apr 15, 2020
1 parent 55ecc0b commit 185ff5f
Showing 1 changed file with 60 additions and 37 deletions.
97 changes: 60 additions & 37 deletions server/db/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,58 @@ async function history(publicKey, total) {

const [result] = await sequelize.query(`
${sumSelect1}
select
ae.created, 'purchase' as type, ae.id as source_id, ap.extern_id,
ap.buy_price as total, 0.00 as pending,
ae.created_by, ae.pay_status as notes, a.address, a.domain
select -- payment
coalesce(ape.extern_time, ape.created) as created,
'payment' as type, ape.id as source_id, extern_id,
coalesce(ape.confirmed_total * -1, 0.00) as total,
coalesce(ape.pending_total * -1, 0.00) as pending,
ape.created_by, coalesce(ape.extern_status, ape.pay_status) as notes,
a.address, a.domain
--, ape.*
from account a
join account_pay ap on ap.account_id = a.id
join account_pay_event ae on ae.id = (
select id from account_pay_event
where account_pay_id = ap.id
order by id asc limit 1 -- 1st / oldest event
)
join account_pay_event ape on ape.account_pay_id = ap.id
where a.owner_key = :publicKey
union all
select -- "hold payment" / offset payment until registration or payment finality
coalesce(ape.extern_time, ape.created) as created,
'payment' as type, ape.id as source_id, extern_id,
coalesce(ape.confirmed_total, 0.00) as total,
coalesce(ape.pending_total, 0.00) as pending,
ape.created_by, 'hold payment' as notes,
a.address, a.domain
--, ape.*
from account a
join account_pay ap on ap.account_id = a.id
join account_pay_event ape on ape.account_pay_id = ap.id
where
a.owner_key = :publicKey and
ape.confirmed_total is not null and
not exists (
select 1
from blockchain_trx t
join blockchain_trx_event e on e.blockchain_trx_id = t.id
join blockchain_trx_event le on le.id = (
select le.id
from blockchain_trx lt
join blockchain_trx_event le on le.blockchain_trx_id = lt.id
where lt.account_id = t.account_id
order by le.id desc limit 1
)
where
t.account_id = a.id and
(e.trx_status = 'success' or le.trx_status = 'cancel')
) and not exists (
select 1
from account_pay ja
join account_pay_event je on je.account_pay_id = ja.id
where ja.account_id = a.id and je.pay_status = 'cancel'
)
union all
select
created, 'adjustment' as type, adj.id as source_id, '' as extern_id,
amount as total, cast(0.00 as numeric) as pending,
Expand All @@ -34,44 +70,31 @@ async function history(publicKey, total) {
union all
select -- payment
coalesce(ape.extern_time, ape.created), 'payment' as type, ape.id as source_id, extern_id,
coalesce(ape.confirmed_total * -1, 0.00) as total,
coalesce(ape.pending_total * -1, 0.00) as pending,
ape.created_by, ape.pay_status,
a.address, a.domain
--, ape.*
from account a
join account_pay ap on ap.account_id = a.id
join account_pay_event ape on ape.id = (
select id from account_pay_event
where account_pay_id = ap.id
order by id desc limit 1
)
where a.owner_key = :publicKey
${total ? '' : `union all
select
coalesce(t.block_time, te.created),
'registration' as type,
te.id as source_id,
trx_id as extern_id,
0.00 as total,
case
when te.trx_status = 'success' then ap.buy_price
when te.trx_status = 'cancel' then 0.00
else 0.00
end as total,
0.00 as pending,
te.created_by, te.trx_status_notes, a.address, a.domain
te.created_by, te.trx_status || coalesce(': ' || te.trx_status_notes, ''),
a.address, a.domain
--, te.*
from account a
join blockchain_trx t on t.id = (
select id from blockchain_trx
where account_id = a.id and t.type = 'register'
join account_pay ap on ap.id = (
select id from account_pay
where account_id = a.id
order by id desc limit 1
)
join blockchain_trx_event te on te.id = (
select id from blockchain_trx_event
where blockchain_trx_id = t.id
order by id desc limit 1
)
where a.owner_key = :publicKey`}
order by 1, 3
join blockchain_trx t on t.account_id = a.id and t.type = 'register'
join blockchain_trx_event te on te.blockchain_trx_id = t.id
where a.owner_key = :publicKey
order by created, source_id, total asc, pending asc
${sumSelect2}`, {replacements: {publicKey}}
)

Expand Down

0 comments on commit 185ff5f

Please sign in to comment.