@@ -70,6 +70,8 @@ def sync_clients(ids = nil)
7070 context = Client . includes ( :work_item )
7171 context = context . where ( id : ids ) if ids . present?
7272
73+ clean_crm_keys ( context )
74+
7375 sync_crm_entities ( context ) do |client |
7476 company = with_prefetch ( :companies , client . crm_key . to_i ) { ::Crm ::Odoo ::Company . find ( _1 ) }
7577 item = client . work_item
@@ -84,11 +86,15 @@ def sync_orders(ids = nil)
8486 context = Order . includes ( :work_item , :additional_crm_orders )
8587 context = context . where ( id : ids ) if ids . present?
8688
89+ clean_crm_keys ( context )
90+
8791 sync_crm_entities ( context ) do |order |
8892 lead = with_prefetch ( :leads , order . crm_key . to_i ) { ::Crm ::Odoo ::Lead . find ( _1 ) }
8993 item = order . work_item
9094
9195 order . additional_crm_orders . each do |additional |
96+ next if additional . crm_key . blank?
97+
9298 sync_additional_order ( additional )
9399 end
94100
@@ -117,6 +123,8 @@ def sync_contacts(ids = nil)
117123 context = Contact
118124 context = context . where ( id : ids ) if ids . present?
119125
126+ clean_crm_keys ( context )
127+
120128 sync_crm_entities ( context ) do |contact |
121129 partner = with_prefetch ( :partners , contact . crm_key . to_i ) { ::Crm ::Odoo ::Partner . find ( _1 ) }
122130 next if partner . blank?
@@ -132,6 +140,8 @@ def import_client_contacts(ids = nil)
132140 context = Client
133141 context = context . where ( id : ids ) if ids . present?
134142
143+ clean_crm_keys ( context )
144+
135145 sync_crm_entities ( context ) do |client |
136146 partners = with_prefetch ( :company_partners , client . crm_key . to_i ) { ::Crm ::Odoo ::Company . partners_for ( _1 ) }
137147 existing = existing_contact_crm_keys ( client , partners . map ( &:id ) )
@@ -149,6 +159,14 @@ def restrict_local? = true
149159
150160 private
151161
162+ def clean_crm_keys ( context )
163+ return unless context . has_attribute? :crm_key
164+
165+ Rails . logger . info "Cleaning crm keys for #{ context . name } "
166+
167+ context . where ( crm_key : '' ) . update_all ( crm_key : nil )
168+ end
169+
152170 def prefetch_resources ( type = :all )
153171 @prefetched ||= { }
154172
0 commit comments