Newer
Older
dmpopidor / app / services / org / create_last_month_joined_user_service.rb
@Sam Rust Sam Rust on 17 Mar 2020 759 bytes Issue/2345 (#2427)
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatJoinedUser
import StatJoinedUser::CreateOrUpdate
import User

class Org

  class CreateLastMonthJoinedUserService

    class << self

      def call(org = nil, threads: 0)
        orgs = org.nil? ? ::Org.all : [org]

        Parallel.each(orgs, in_threads: threads) do |org|
          months = OrgDateRangeable.split_months_from_creation(org)
          last = months.last
          if last.present?
            StatJoinedUser::CreateOrUpdate.do(
              start_date: last[:start_date],
              end_date: last[:end_date],
              org: org
            )
          end
        end
      end

    end

  end

end