-- cpdb_dev.client_current_users source
create or replace algorithm = UNDEFINED view `cpdb_dev`.`client_current_users` as
select `cu`.`client_id` as `client_id`,
  `u`.`email` as `email`,
  'active user' as user_type
from (
    `cpdb_dev`.`client_users` `cu`
    join `cpdb_dev`.`users` `u` on (
      (
        (`cu`.`user_id` = `u`.`id`)
        and isnull(`u`.`deleted_at`)
      )
    )
  )
where (`cu`.`status` = 1)
union
select `cui`.`client_id` as `client_id`,
  `cui`.`email` as `email`,
  'invitee' as user_type
from `cpdb_dev`.`client_user_invites` `cui`
where (
    (`cui`.`status` = 1)
    and isnull(`cui`.`deleted_at`)
    and (`cui`.`expired_at` > now())
  );