Sha256: ec7134f1b28bbeddf0ffdbee8a99668bd5e17e8de9b79a97f600e442fc06f1e0

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

module Ecom
  module Core
    class ProjectCrewService
      def update(params)
        id = params[:project_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] }

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

        new_entries = []
        new_crew.each do |nc|
          new_entries << Ecom::Core::ProjectCrew.new(
            project_id: id, crew_id: nc[:crew_id], start_date: Date.today, status: 'Active'
          )
        end

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

        removed_ids = removed.map { |r| r[:crew_id] }
        if removed_ids.count.positive?
          removed_crew = Ecom::Core::ProjectCrew.where(project_id: id, crew_id: removed_ids)
          Ecom::Core::ProjectCrew.transaction do
            removed_crew.map(&:delete)
          end
        end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ecom_core-1.2.34 app/services/ecom/core/project_crew_service.rb
ecom_core-1.2.33 app/services/ecom/core/project_crew_service.rb
ecom_core-1.2.32 app/services/ecom/core/project_crew_service.rb
ecom_core-1.2.31 app/services/ecom/core/project_crew_service.rb
ecom_core-1.2.30 app/services/ecom/core/project_crew_service.rb
ecom_core-1.2.29 app/services/ecom/core/project_crew_service.rb
ecom_core-1.2.28 app/services/ecom/core/project_crew_service.rb