module Ecom module Core class CrewContractTransactionService def execute(crew_contract_transaction, crew_contract) Ecom::Core::CrewContractTransaction.transaction do case crew_contract_transaction.transaction_type when Ecom::Core::CrewContractTransaction::TXN_UPDATE_CREW_TYPE create_new_id_card(crew_contract.crew, crew_contract.to) # update crew type crew = crew_contract.crew crew.update(crew_type_id: crew_contract.crew_type_id) crew_contract_transaction.update(status: Ecom::Core::CrewContractTransaction::EXECUTED) when Ecom::Core::CrewContractTransaction::TXN_UPDATE_PLACE_OF_WORK create_new_id_card(crew_contract.crew, crew_contract.to) crew_contract_transaction.update(status: Ecom::Core::CrewContractTransaction::EXECUTED) when Ecom::Core::CrewContractTransaction::TXN_UPDATE_WAGE crew = crew_contract.crew # update wage crew.update(wage: crew_contract.wage, wage_in_words: crew_contract.wage_in_words) crew_contract_transaction.update(status: Ecom::Core::CrewContractTransaction::EXECUTED) when Ecom::Core::CrewContractTransaction::TXN_UPDATE_VALIDITY # make crew active create_new_id_card(crew_contract.crew, crew_contract.to) make_crew_active(crew_contract.crew) activate_contract(crew_contract) crew_contract_transaction.update(status: Ecom::Core::CrewContractTransaction::EXECUTED) when Ecom::Core::CrewContractTransaction::TXN_TERMINATE_CONTRACT # make crew inactive make_crew_inactive(crew_contract.crew) # make ID card and gate pass invalid invalidate_id_card(crew_contract.crew) invalidate_gate_pass(crew_contract.crew) # make the contract void deactivate_contract(crew_contract) crew_contract_transaction.update(status: Ecom::Core::CrewContractTransaction::EXECUTED) end end end def activate_contract(crew_contract) crew_contract.update(status: :in_effect) end def deactivate_contract(crew_contract) crew_contract.update(status: :void) end def make_crew_active(crew) return if crew.active crew.update(active: true) end def make_crew_inactive(crew) return unless crew.active crew.update(active: false) end # invalidate other Id cards and creates a new ID card def create_new_id_card(crew, valid_until) issued_on = Time.now previous_id_card = Ecom::Core::CrewIdCard.where(crew_id: crew.id).last previous_id_card&.update(status: 'Invalid') new_id_card = Ecom::Core::CrewIdCard.new( crew_id: crew.id, issued_on: issued_on, valid_until: valid_until, status: 'Valid' ) raise StandardError, new_id_card.errors.messages unless new_id_card.save end def invalidate_id_card(crew) current_id_card = Ecom::Core::CrewIdCard.where(crew_id: crew.id).last current_id_card.update(status: 'Invalid') end def invalidate_gate_pass(crew) Ecom::Core::SiteCrew.where(crew_id: crew.id).update(status: Ecom::Core::SiteCrew::INACTIVE) end end end end