Sha256: 9b6f9f34dbe7d8e15e91c2b2d455173b58a6ef5abd734c51ff628420493f782c

Contents?: true

Size: 1.26 KB

Versions: 17

Compression:

Stored size: 1.26 KB

Contents

module Ecom
  module Core
    class SiteCrewService
      def update(params)
        id = params[:site_id]
        crew = params[:crew]

        # Process new additions
        new_crew = crew.select { |c| c[:status] == 'checked' }
        removed = crew.select { |c| c[:status] == 'unchecked' }

        crew_ids = new_crew.map { |c| c[:crew_id] }

        site_crew = Ecom::Core::Crew.where(id: crew_ids)

        new_entries = []
        new_crew.each do |nc|
          new_entries << Ecom::Core::SiteCrew.new(
            site_id: id, crew_id: nc[:crew_id], start_date: Date.today,
            site_crew_type: 'Temporary site crew',
            status: 'Active'
          )
        end

        Ecom::Core::SiteCrew.transaction do
          new_entries.each(&:save)
        end

        removed_ids = removed.map { |r| r[:crew_id] }
        if removed_ids.count.positive?
          removed_crews = Ecom::Core::SiteCrew.where(site_id: id, crew_id: removed_ids, status: 'Active')
          Ecom::Core::SiteCrew.transaction do
            # removed_crew.map(&:delete)
            removed_crews.each do |removed_crew|
              removed_crew.update(status: 'Inactive')
            end
          end
        end

        { success: true, data: site_crew }
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.13 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.12 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.11 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.10 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.9 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.8 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.7 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.6 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.5 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.4 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.3 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.2 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.1 app/services/ecom/core/site_crew_service.rb
ecom_core-1.3.0 app/services/ecom/core/site_crew_service.rb
ecom_core-1.2.36 app/services/ecom/core/site_crew_service.rb
ecom_core-1.2.35 app/services/ecom/core/site_crew_service.rb