module EmployerAutomation extend ActiveSupport::Concern included do scope :not_automation_ready, -> { where(pl_plan_key: [nil, ''], group_number: [nil, '']) } scope :automation_ready, -> { where.not(pl_plan_key: [nil, '']) .or(where.not(group_number: [nil, ''])) } scope :missing_keychain_values, -> { where(company_pb_entity_key: [nil, '']) .or(where(plan_id: [nil, ''])) .or(where(group_number: [nil, ''])) .or(where(pl_plan_key: [nil, ''])) } scope :missing_keychain_initialization, -> { inactive.automation_ready.missing_keychain_values } scope :building_id_card_setup, -> { active.left_outer_joins(:id_card_setup) .where(id_card_setup: {active: false}) } scope :missing_plans_initialization, -> { building_id_card_setup .where.missing(:plans) .or( where( id: joins(:plans) .where(plans: { pb_product_key: [nil, ''] }) .select(:id) ) ) } scope :missing_members_initialization, -> { building_id_card_setup .where.associated(:plans) .where.not( id: joins(:plans) .where(plans: { pb_product_key: [nil, ''] }) .select(:id) ).distinct } scope :active_with_active_id_card_setup, -> { active.left_outer_joins(:id_card_setup) .where(id_card_setup: {active: true}) } scope :with_keychain_values, -> { where.not( pl_plan_key: [nil, ''], group_number: [nil, ''], company_pb_entity_key: [nil, ''], plan_id: [nil, ''] ) } scope :deactivated, -> { inactive.with_keychain_values } # Employer.joins(:id_card_setup) # .left_outer_joins(:plans) # .where(plans: { pb_product_key: [nil, ''] }) # .group('users.id') # Group by user ID # .having('COUNT(posts.id) = 0') # Employer.left_outer_joins(:plans) # .where(plans: { id: nil }) # .or(Employer.where(plans: { pb_product_key: [nil, ''] })) # Employer.where.missing(:plans) # .or(Employer.joins(:plans).where(plans: { pb_product_key: [nil, ''] })) # Employer.joins(id_card_setup: :plans).where.not("plans.id_card_setup_id = id_card_setup.id").or # (where(plans: { pb_product_key: [nil, ''] })) # scope :with_survey_and_no_questions, -> { # joins(:survey) # join has_one # .left_joins(survey: :questions) # join has_many through has_one # .where(questions: { id: nil }) # filter where has_many is empty # } # scope :missing_initial_members, -> { # new_groups.with_plans # } end # class_methods do # # Methods in this block become class methods of the including class. # def count_all_visible # visible.count # end # end # Any other methods defined here become instance methods automatically. # def up_to_date? # self.pl_plan_key.present? && # self.company_pb_entity_key.present? && # self.plan_id.present? && # self.group_number.present? && # self.effective_date.present? # end def sync_members_with_vhcs AutomationService::EmployerMembersUpdate.new(pl_plan_key).call end def sync_plans_with_vhcs AutomationService::EmployerPlansUpdate.new(pl_plan_key).call end def automation_identifier attributes.with_indifferent_access.slice( pl_plan_key.present? ? :pl_plan_key : :group_number ) end def sync_with_vhcs employer_identifier = automation_identifier AutomationService::EmployerUpdate.new(employer_identifier).call end end