2026-03-19 00:42:27 -04:00
|
|
|
module EmployerAutomation
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
2026-04-15 08:12:47 -04:00
|
|
|
included do
|
2026-04-20 12:12:52 -04:00
|
|
|
|
|
|
|
|
scope :uninitialized, -> {
|
|
|
|
|
where(initialized: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :initialized, -> {
|
|
|
|
|
where(initialized: true)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 15:35:10 -04:00
|
|
|
scope :not_automation_ready, -> {
|
2026-04-20 12:12:52 -04:00
|
|
|
where(group_number: [nil, ''])
|
2026-04-15 08:12:47 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 15:35:10 -04:00
|
|
|
scope :automation_ready, -> {
|
2026-04-20 12:12:52 -04:00
|
|
|
where.not(group_number: [nil, ''])
|
2026-04-15 08:12:47 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 12:12:52 -04:00
|
|
|
scope :in_automation_initilization, -> {
|
2026-05-06 13:28:16 -04:00
|
|
|
left_outer_joins(:id_card_setup)
|
2026-04-20 12:12:52 -04:00
|
|
|
.where(initialized: false)
|
|
|
|
|
.or(
|
|
|
|
|
where(id_card_setup: {initialized: false})
|
2026-05-06 13:28:16 -04:00
|
|
|
).distinct
|
2026-04-17 15:35:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :missing_keychain_initialization, -> {
|
2026-04-20 12:12:52 -04:00
|
|
|
uninitialized.automation_ready
|
2026-04-17 15:35:10 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 12:12:52 -04:00
|
|
|
scope :uninitialized_id_card_setup, -> {
|
2026-05-06 13:28:16 -04:00
|
|
|
left_outer_joins(:id_card_setup)
|
2026-04-20 12:12:52 -04:00
|
|
|
.where(id_card_setup: {initialized: false})
|
2026-04-17 15:35:10 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 12:12:52 -04:00
|
|
|
scope :missing_plans, -> {
|
2026-05-06 13:28:16 -04:00
|
|
|
left_outer_joins(:plans)
|
|
|
|
|
.where(id_card_plans: { pb_product_key: [nil, ""] })
|
|
|
|
|
.distinct
|
2026-04-17 15:35:10 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 12:12:52 -04:00
|
|
|
scope :has_plans, -> {
|
2026-05-06 13:28:16 -04:00
|
|
|
left_outer_joins(:plans)
|
|
|
|
|
.where("id_card_plans.pb_product_key IS NOT NULL AND id_card_plans.pb_product_key != ''")
|
|
|
|
|
.distinct
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :missing_members, -> {
|
|
|
|
|
left_outer_joins(:members)
|
|
|
|
|
.where(members: { id: nil })
|
|
|
|
|
.distinct
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :has_members, -> {
|
|
|
|
|
left_outer_joins(:members)
|
|
|
|
|
.where.not(members: { id: nil })
|
|
|
|
|
.distinct
|
2026-04-17 15:35:10 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 12:12:52 -04:00
|
|
|
scope :missing_plans_initialization, -> {
|
2026-05-06 13:28:16 -04:00
|
|
|
initialized.uninitialized_id_card_setup.missing_plans
|
2026-04-20 12:12:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :missing_members_initialization, -> {
|
2026-05-06 13:28:16 -04:00
|
|
|
initialized.uninitialized_id_card_setup.has_plans.missing_members
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :ready_for_id_card_activation, -> {
|
|
|
|
|
initialized.uninitialized_id_card_setup.has_members
|
2026-04-20 12:12:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :with_active_id_card_setup, -> {
|
2026-04-17 15:35:10 -04:00
|
|
|
active.left_outer_joins(:id_card_setup)
|
|
|
|
|
.where(id_card_setup: {active: true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :deactivated, -> {
|
2026-04-20 12:12:52 -04:00
|
|
|
inactive.initialized
|
2026-04-17 15:35:10 -04:00
|
|
|
}
|
2026-04-15 08:12:47 -04:00
|
|
|
end
|
2026-03-19 00:42:27 -04:00
|
|
|
|
2026-04-15 08:12:47 -04:00
|
|
|
def sync_members_with_vhcs
|
2026-04-17 15:35:10 -04:00
|
|
|
AutomationService::EmployerMembersUpdate.new(pl_plan_key).call
|
2026-03-19 00:42:27 -04:00
|
|
|
end
|
|
|
|
|
|
2026-04-17 15:35:10 -04:00
|
|
|
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
|
2026-04-15 08:12:47 -04:00
|
|
|
|
2026-03-19 00:42:27 -04:00
|
|
|
def sync_with_vhcs
|
2026-04-17 15:35:10 -04:00
|
|
|
employer_identifier = automation_identifier
|
|
|
|
|
AutomationService::EmployerUpdate.new(employer_identifier).call
|
2026-03-19 00:42:27 -04:00
|
|
|
end
|
|
|
|
|
end
|