aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stif/codif_line_synchronization.rb
blob: 9a8a92bceb6f40b62d507734f6400dd114325d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module Stif
  module CodifLineSynchronization
    class << self
      # Don't check last synchronizations date and lines last update if first_sync
      def synchronize first_sync = false
        # Check last synchronization and synchronization interval
        date = DateTime.now.to_date - LineReferential.first.sync_interval.days
        last_sync = LineReferential.first.line_referential_sync.line_sync_operations.last.try(:created_at)
        return if (last_sync.nil? || last_sync.to_date > date) && !first_sync
        
        # TODO Check exceptions and status messages
        begin
          # Fetch Codifline operators and lines
          client = Codifligne::API.new
          operators = client.operators
          lines = client.lines

          operators.map{ |o| create_or_update_company(o) }
          lines.map{ |l| create_or_update_line(l) }
          
          delete_deprecated_companies(operators)
          delete_deprecated_lines(lines)

          LineReferential.first.line_referential_sync.record_status "OK"
        rescue Exception => e
          ap e.message
          LineReferential.first.line_referential_sync.record_status "Error"
        end
      end

      def create_or_update_company(api_operator)
        params = {
          name: api_operator.name,
          objectid: api_operator.stif_id
        }
        company = save_or_update(params, Chouette::Company)
      end

      def create_or_update_line(api_line)
        params = {
          name: api_line.name,
          objectid: api_line.stif_id,
          number: api_line.short_name,
          deactivated: (api_line.status == "inactive" ? true : false)
        }
        
        # Find Company
        # TODO Check behavior when operator_codes count is 0 or > 1
        if api_line.operator_codes.any?
          company_id = "STIF:CODIFLIGNE:Operator:" + api_line.operator_codes.first
          params[:company] = Chouette::Company.find_by(objectid: company_id)
        end

        save_or_update(params, Chouette::Line)
      end

      def delete_deprecated_companies(operators)
        ids = operators.map{ |o| o.stif_id }.to_a
        Chouette::Company.where.not(objectid: ids).destroy_all
      end

      def delete_deprecated_lines(lines)
        ids = lines.map{ |l| l.stif_id }.to_a
        Chouette::Line.where.not(objectid: ids).map { |l| l.deactivated = true ; l.save }
      end

      def save_or_update(params, klass)
        params[:line_referential] = LineReferential.first
        object = klass.where(objectid: params[:objectid]).first
        if object
          object.assign_attributes(params)
          object.save if object.changed?
        else
          object = klass.new(params)
          object.save if object.valid?
        end
        object
      end
    end
  end
end