2026-04-15 08:12:47 -04:00
class UpdateMemberJob < ApplicationJob
queue_as :default
def perform ( pb_entity_key , employer_id , has_divisions = false , has_dental = false , vw_mb_member = { } )
2026-04-17 15:35:10 -04:00
unless vw_mb_member . present?
vw_mb_member = Vhcs :: VwmbMember . find_by ( enrollee_type_value_id : 1 , pb_entity_key : pb_entity_key ) . attributes . with_indifferent_access . slice ( :mb_member_key , :pb_entity_key , :pl_plan_key , :family_id , :full_name_last_name_first , :social_security_number )
end
2026-04-15 08:12:47 -04:00
2026-04-17 15:35:10 -04:00
pb_products = Vhcs :: PbProduct . joins ( '
2026-04-15 08:12:47 -04:00
INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductKey" = "PBProduct"."PBProductKey"
INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductAvailabilityKey" = "PBProductAvailability"."PBProductAvailabilityKey"
INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
' ) . where ( '
"PBCoveredEntities"."PBEntityKey" = ?
AND "PBProductParticipation"."InEffect" <= ?
AND "PBProductParticipation"."OutOfEffect" > ?' ,
2026-06-17 23:23:36 -04:00
vw_mb_member [ :pb_entity_key ] , 3 . month . from_now , 1 . day . ago
2026-04-15 08:12:47 -04:00
)
if pb_products . present? && vw_mb_member [ :social_security_number ] . present?
2026-05-06 13:28:16 -04:00
member = Member . find_or_create_by ( pb_entity_key : vw_mb_member [ :pb_entity_key ] , employer_id : employer_id )
2026-04-15 08:12:47 -04:00
member . name = vw_mb_member [ :full_name_last_name_first ] . titleize
member . mb_member_key = vw_mb_member [ :mb_member_key ]
member . family_id = vw_mb_member [ :family_id ]
member . pl_plan_key = vw_mb_member [ :pl_plan_key ]
card_display_name = Vhcs :: PbEntity . find_by ( pb_entity_key : member . pb_entity_key ) . full_name
member . id_card_display_name = card_display_name
if has_divisions
division = Vhcs :: PbEntity . joins ( '
INNER JOIN "PBAffiliation" ON "PBAffiliation"."ParentPBEntityKey" = "PBEntity"."PBEntityKey"
INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBAffiliationKey" = "PBAffiliation"."PBAffiliationKey"
INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
' ) . find_by ( '
"PBCoveredEntities"."PBEntityKey" = ?
AND "PBProductParticipation"."OutOfEffect" > ?' ,
member . pb_entity_key , 1 . day . ago
) . last_name
member . division = division
end
if has_dental
2026-04-17 15:35:10 -04:00
medical_pb_product_key = pb_products . where ( " ShortDescription LIKE ? " , " %Medical% " ) & . first & . pb_product_key
2026-04-15 08:12:47 -04:00
dental_pb_product_key = pb_products . where ( " ShortDescription LIKE ? " , " %Dental% " ) & . first & . pb_product_key
# dental_pb_product_key = pb_products.where(short_description: "Dental")&.first&.pb_product_key
if dental_pb_product_key
member . dental_plan_key = dental_pb_product_key
coverage_class = Vhcs :: GenLookupTables . joins ( '
INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."CoverageTypeCode" = "GEN_LookupTables"."RecordID"
INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductAvailabilityKey" = "PBProductParticipation"."PBProductAvailabilityKey"
INNER JOIN "PBProduct" ON "PBProduct"."PBProductKey" = "PBProductAvailability"."PBProductKey"
2026-06-17 23:23:36 -04:00
' ) . select ( '
"GEN_LookupTables".*, "PBProductParticipation"."InEffect" AS EffDate
' ) . where ( '
2026-04-15 08:12:47 -04:00
"PBCoveredEntities"."PBEntityKey" = ?
AND "PBProductParticipation"."InEffect" <= ?
AND "PBProductParticipation"."OutOfEffect" > ?
AND "PBProduct"."PBProductKey" = ?' ,
member . pb_entity_key , 1 . month . from_now , 1 . day . ago , dental_pb_product_key
2026-06-17 23:23:36 -04:00
) . order ( 'EffDate DESC' ) . first . short_desc
2026-04-15 08:12:47 -04:00
member . coverage_class = coverage_class
2026-06-17 23:23:36 -04:00
else
member . coverage_class = " NONE "
2026-04-15 08:12:47 -04:00
end
else
medical_pb_product_key = pb_products . first . pb_product_key
end
if medical_pb_product_key && plan = IdCard :: Plan . find_by ( pb_product_key : medical_pb_product_key )
member . id_card_plan = plan
end
member . dependents = get_dependent_names_by_sequence ( member )
# if employer_members_update
# member
# else
# end
2026-05-06 13:28:16 -04:00
member . save!
2026-04-17 15:35:10 -04:00
puts " ---- #{ member . name } "
2026-04-15 08:12:47 -04:00
end
member . presence
end
private
def get_dependent_names_by_sequence ( member )
dependents = Vhcs :: VwmbMember . joins ( '
INNER JOIN "PBCoveredEntities" ON "PBCoveredEntities"."PBEntityKey" = "vwMBMember"."PBEntityKey"
INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductAvailabilityKey" = "PBProductParticipation"."PBProductAvailabilityKey"
INNER JOIN "PBProduct" ON "PBProduct"."PBProductKey" = "PBProductAvailability"."PBProductKey"
' ) . where ( '
"EnrolleeTypeKey" != 1044 AND "PLPlanKey" = ? AND "PBProductParticipation"."OutOfEffect" >= ? AND "FamilyID" = ?' , member . pl_plan_key , Date . today . strftime ( " %m/%d/%Y " ) , member . family_id
)
if member . pl_plan_key == 3
dependents = dependents . where ( '"PBProduct"."PBProductKey" != 1024' )
elsif member . pl_plan_key == 2
dependents = dependents . where ( '"PBProduct"."PBProductKey" != 1019' )
end
dependent_names = dependents . order ( :sequence_number ) . map { | dep | dep . first_name + ' ' + dep . last_name } . uniq
dependent_names
end
end