Skip to content

Instantly share code, notes, and snippets.

@hadfieldn
Last active November 1, 2017 16:17
Show Gist options
  • Save hadfieldn/ffc1c13492efadea9d27d1653e3a7a00 to your computer and use it in GitHub Desktop.
Save hadfieldn/ffc1c13492efadea9d27d1653e3a7a00 to your computer and use it in GitHub Desktop.
Set state to `active` for users who are admins and have transactions or active cards
/* Find admins who are in non-active state */
/* Run after `update.sql` to find how many admins still need to be reviewed */
select * from users where state = 'created' or state = 'onboarding' and company_admin = true
update users
set state = 'active'
from
(select
id,
(select count(*) from transactions t
left join user_categories as c on t.user_category_id = c.id
left join user_allocations as a on c.user_allocation_id = a.id
where a.user_id = u.id
) transaction_count
from users u
) as onboarding_users,
(select user_id from cards c
join users as u on c.user_id = u.id
where c.activation_status = 'activated' and c.type = 'physical'
) as active_card_users
where
(
(users.id = onboarding_users.id and onboarding_users.transaction_count > 0)
or users.id = active_card_users.user_id
)
and (users.state = 'created' or users.state = 'onboarding')
and users.company_admin = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment