Last active
November 1, 2017 16:17
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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