From 75bc1a11932be380384f8bbc6daf3239025108ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Mon, 18 Sep 2017 18:44:09 +0100 Subject: [PATCH 01/14] Create Woo checkpoint when creating products in Woo --- app/classes/woo/products.rb | 2 ++ app/models/keyword.rb | 1 + app/models/keyword/woo_checkpoint.rb | 5 +++++ test/classes/woo/products_test.rb | 10 ++++++++++ 4 files changed, 18 insertions(+) create mode 100644 app/models/keyword/woo_checkpoint.rb diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index dd852108d..688b2dfcc 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -16,6 +16,8 @@ def create created_count += create_product(product) end + Keyword::WooCheckpoint.create!(selite: Time.current) + logger.info "Lisättiin #{created_count} tuotetta verkkokauppaan" end diff --git a/app/models/keyword.rb b/app/models/keyword.rb index 41dce0194..36ac7fd0d 100644 --- a/app/models/keyword.rb +++ b/app/models/keyword.rb @@ -140,6 +140,7 @@ def self.child_class_names VARASTOLUOKKA: Keyword, VARASTORYHMA: Keyword, VARTOIMTULOSTIN: Keyword, + WOO_CHECKPOINT: Keyword::WooCheckpoint, WOO_FIELD: Keyword::WooField, Y: Keyword, }.stringify_keys diff --git a/app/models/keyword/woo_checkpoint.rb b/app/models/keyword/woo_checkpoint.rb new file mode 100644 index 000000000..8b4210cee --- /dev/null +++ b/app/models/keyword/woo_checkpoint.rb @@ -0,0 +1,5 @@ +class Keyword::WooCheckpoint < Keyword + def self.sti_name + :WOO_CHECKPOINT + end +end diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 12fb296a1..6dd4acfbb 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -103,4 +103,14 @@ class Woo::ProductsTest < ActiveSupport::TestCase product = @woocommerce.send(:products).first assert_equal response, @woocommerce.send(:product_hash, product) end + + test 'checkpoint is created after first run' do + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, 'id' => 1 do + assert_difference 'Keyword::WooCheckpoint.count' do + @woocommerce.create + end + end + end + end end From 17269c356c31351f6fe2e69c9ccc4d07e804a64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Mon, 18 Sep 2017 19:18:09 +0100 Subject: [PATCH 02/14] Improve woo checkpoint structure --- app/classes/woo/products.rb | 2 +- app/models/keyword/woo_checkpoint.rb | 12 ++++++++++++ test/classes/woo/products_test.rb | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index 688b2dfcc..8a09e60fa 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -16,7 +16,7 @@ def create created_count += create_product(product) end - Keyword::WooCheckpoint.create!(selite: Time.current) + Keyword::WooCheckpoint.update_timestamp(:create) logger.info "Lisättiin #{created_count} tuotetta verkkokauppaan" end diff --git a/app/models/keyword/woo_checkpoint.rb b/app/models/keyword/woo_checkpoint.rb index 8b4210cee..ea8ebf776 100644 --- a/app/models/keyword/woo_checkpoint.rb +++ b/app/models/keyword/woo_checkpoint.rb @@ -2,4 +2,16 @@ class Keyword::WooCheckpoint < Keyword def self.sti_name :WOO_CHECKPOINT end + + def self.update_timestamp(run_type) + checkpoint = last || new + timestamps = checkpoint.selite.present? ? JSON.parse(checkpoint.selite, symbolize_names: true) : {} + timestamps[run_type.to_sym] = Time.current + checkpoint.selite = timestamps.to_json + checkpoint.save! + end + + def self.last_run_at(run_type) + Time.zone.parse(JSON.parse(last.selite, symbolize_names: true)[run_type.to_sym]) + end end diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 6dd4acfbb..5ed7a3caa 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -104,7 +104,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_equal response, @woocommerce.send(:product_hash, product) end - test 'checkpoint is created after first run' do + test 'checkpoint is created after first create run' do @woocommerce.stub :get_sku, nil do @woocommerce.stub :woo_post, 'id' => 1 do assert_difference 'Keyword::WooCheckpoint.count' do @@ -112,5 +112,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase end end end + + assert_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 end end From d0d8c125170cb9e2570f1aafc34eaf5cf02d9cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Mon, 18 Sep 2017 19:21:40 +0100 Subject: [PATCH 03/14] Test that checkpoint is updated if already present --- test/classes/woo/products_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 5ed7a3caa..10b41a18a 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -115,4 +115,16 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 end + + test 'checkpoint is updated after if already present when creating products' do + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, 'id' => 1 do + assert_difference 'Keyword::WooCheckpoint.count' do + 2.times { @woocommerce.create } + end + end + end + + assert_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 + end end From 349810dbd7ca29238fa51bf3d5e9f5f250092c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Mon, 18 Sep 2017 19:24:22 +0100 Subject: [PATCH 04/14] Update checkpoint when updating stocks --- app/classes/woo/products.rb | 2 ++ test/classes/woo/products_test.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index 8a09e60fa..6bcdbdb80 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -37,6 +37,8 @@ def update updated_count += update_stock(woo_product['id'], product) end + Keyword::WooCheckpoint.update_timestamp(:update) + logger.info "Päivitettiin #{updated_count} tuotteen saldot" end diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 10b41a18a..3f6a36cbc 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -127,4 +127,16 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 end + + test 'checkpoint is updated after updating stocks' do + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, 'id' => 1 do + assert_difference 'Keyword::WooCheckpoint.count' do + 2.times { @woocommerce.update } + end + end + end + + assert_in_delta Keyword::WooCheckpoint.last_run_at(:update), Time.current, 10 + end end From 32eab521f5534075a26fed75f527d77c41e7cb91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Mon, 18 Sep 2017 19:32:04 +0100 Subject: [PATCH 05/14] Test that different timestamps work independently --- test/classes/woo/products_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 3f6a36cbc..8129faad7 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -139,4 +139,18 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_in_delta Keyword::WooCheckpoint.last_run_at(:update), Time.current, 10 end + + test 'different timestamps in checkpoint work independently' do + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, 'id' => 1 do + assert_difference 'Keyword::WooCheckpoint.count' do + travel(-1.day) { @woocommerce.create } + @woocommerce.update + end + end + end + + refute_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 + assert_in_delta Keyword::WooCheckpoint.last_run_at(:update), Time.current, 10 + end end From c3b862da6eb24c44fcb6fc8781126a302bc41af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Tue, 19 Sep 2017 17:34:13 +0100 Subject: [PATCH 06/14] Only create new products by default --- app/classes/woo/products.rb | 10 ++++--- app/models/keyword/woo_checkpoint.rb | 4 ++- test/classes/woo/products_test.rb | 40 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index 6bcdbdb80..10fd7b2c0 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -4,9 +4,11 @@ class Woo::Products < Woo::Base # Create products to woocommerce def create + timestamp = Keyword::WooCheckpoint.last_run_at(:create) + created_count = 0 - products.each do |product| + products(timestamp).each do |product| if get_sku(product.tuoteno) logger.info "Tuote #{product.tuoteno} on jo verkkokaupassa" @@ -44,10 +46,12 @@ def update private - def products + def products(timestamp = nil) # Näkyviin tuotteet A ja P statuksella, mutta vain ne tuotteet joissa Hinnastoon valinnoissa # verkkokauppa näkyvyys päällä - Product.where(status: %w(A P)).where(hinnastoon: 'W') + products = Product.where(status: %w(A P)).where(hinnastoon: 'W') + timestamp && products = products.where('luontiaika >= ?', timestamp) + products end def product_hash(product) diff --git a/app/models/keyword/woo_checkpoint.rb b/app/models/keyword/woo_checkpoint.rb index ea8ebf776..52b0f649a 100644 --- a/app/models/keyword/woo_checkpoint.rb +++ b/app/models/keyword/woo_checkpoint.rb @@ -12,6 +12,8 @@ def self.update_timestamp(run_type) end def self.last_run_at(run_type) - Time.zone.parse(JSON.parse(last.selite, symbolize_names: true)[run_type.to_sym]) + checkpoint = last + return unless checkpoint + Time.zone.parse(JSON.parse(checkpoint.selite, symbolize_names: true)[run_type.to_sym]) end end diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 8129faad7..ac0f016d4 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -153,4 +153,44 @@ class Woo::ProductsTest < ActiveSupport::TestCase refute_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 assert_in_delta Keyword::WooCheckpoint.last_run_at(:update), Time.current, 10 end + + test 'all products are created if no create timestamp in checkpoint' do + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, block do + @woocommerce.create + end + end + + assert_equal 1, counter + end + + test 'only new products are created when create timestamp is found' do + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, 'id' => 1 do + @woocommerce.create + end + end + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, block do + @woocommerce.create + end + end + + assert_equal 0, counter + end end From 62a266305963b4d521dcae91f3244d20f1d22722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Tue, 19 Sep 2017 17:38:39 +0100 Subject: [PATCH 07/14] Allow creating all products by passing option --- app/classes/woo/products.rb | 4 ++-- test/classes/woo/products_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index 10fd7b2c0..8a63b6039 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -3,8 +3,8 @@ class Woo::Products < Woo::Base # 2. update stock of products in webstore # Create products to woocommerce - def create - timestamp = Keyword::WooCheckpoint.last_run_at(:create) + def create(all: false) + timestamp = all ? nil : Keyword::WooCheckpoint.last_run_at(:create) created_count = 0 diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index ac0f016d4..e176d1f59 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -193,4 +193,27 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_equal 0, counter end + + test 'all products can be created by passign option even when timestamp is found' do + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, 'id' => 1 do + @woocommerce.create + end + end + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, nil do + @woocommerce.stub :woo_post, block do + @woocommerce.create(all: true) + end + end + + assert_equal 1, counter + end end From 1d198339a9416f723a0da8430d58c3cb0cd491df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Thu, 21 Sep 2017 19:38:27 +0100 Subject: [PATCH 08/14] Take rows into account when updating stocks --- app/classes/woo/products.rb | 28 ++++++--- app/models/keyword/woo_checkpoint.rb | 6 +- app/models/product.rb | 1 + test/classes/woo/products_test.rb | 87 +++++++++++++++++++++++++++- 4 files changed, 112 insertions(+), 10 deletions(-) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index 8a63b6039..4a20e24f9 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -8,7 +8,7 @@ def create(all: false) created_count = 0 - products(timestamp).each do |product| + products_to_create(timestamp).each do |product| if get_sku(product.tuoteno) logger.info "Tuote #{product.tuoteno} on jo verkkokaupassa" @@ -24,10 +24,12 @@ def create(all: false) end # Update product stock quantity - def update + def update(all: false) + timestamp = all ? nil : Keyword::WooCheckpoint.last_run_at(:update) + updated_count = 0 - products.each do |product| + products_to_update(timestamp).each do |product| woo_product = get_sku(product.tuoteno) unless woo_product @@ -46,12 +48,24 @@ def update private - def products(timestamp = nil) + def products # Näkyviin tuotteet A ja P statuksella, mutta vain ne tuotteet joissa Hinnastoon valinnoissa # verkkokauppa näkyvyys päällä - products = Product.where(status: %w(A P)).where(hinnastoon: 'W') - timestamp && products = products.where('luontiaika >= ?', timestamp) - products + Product.where(status: %w(A P)).where(hinnastoon: 'W') + end + + def products_to_create(timestamp) + return products unless timestamp + + products.where('luontiaika >= ?', timestamp) + end + + def products_to_update(timestamp) + return products unless timestamp + + products.select do |product| + product.rows.where('laadittu >= ?', timestamp).any? + end end def product_hash(product) diff --git a/app/models/keyword/woo_checkpoint.rb b/app/models/keyword/woo_checkpoint.rb index 52b0f649a..a84087d5c 100644 --- a/app/models/keyword/woo_checkpoint.rb +++ b/app/models/keyword/woo_checkpoint.rb @@ -6,7 +6,7 @@ def self.sti_name def self.update_timestamp(run_type) checkpoint = last || new timestamps = checkpoint.selite.present? ? JSON.parse(checkpoint.selite, symbolize_names: true) : {} - timestamps[run_type.to_sym] = Time.current + timestamps[run_type.to_sym] = Time.current.change(usec: 0) checkpoint.selite = timestamps.to_json checkpoint.save! end @@ -14,6 +14,8 @@ def self.update_timestamp(run_type) def self.last_run_at(run_type) checkpoint = last return unless checkpoint - Time.zone.parse(JSON.parse(checkpoint.selite, symbolize_names: true)[run_type.to_sym]) + timestamp = JSON.parse(checkpoint.selite, symbolize_names: true)[run_type.to_sym] + return unless timestamp + Time.zone.parse(timestamp) end end diff --git a/app/models/product.rb b/app/models/product.rb index 2c452506f..d0e7ac2d5 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -32,6 +32,7 @@ class Product < BaseModel o.has_many :manufacture_rows, class_name: 'ManufactureOrder::Row' o.has_many :product_suppliers, class_name: 'Product::Supplier' o.has_many :purchase_order_rows, class_name: 'PurchaseOrder::Row' + o.has_many :rows, class_name: 'Row' o.has_many :sales_order_rows, class_name: 'SalesOrder::Row' o.has_many :shelf_locations o.has_many :stock_transfer_rows, class_name: 'StockTransfer::Row' diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index e176d1f59..d732bc084 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -4,6 +4,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase fixtures %w( products + sales_order/rows ) setup do @@ -16,6 +17,8 @@ class Woo::ProductsTest < ActiveSupport::TestCase Product.update_all(hinnastoon: '') products(:ski).update(hinnastoon: 'w') + + Keyword::WooCheckpoint.delete_all end test 'should initialize' do @@ -178,6 +181,8 @@ class Woo::ProductsTest < ActiveSupport::TestCase end end + products(:ski).update!(luontiaika: 10.seconds.ago) + counter = 0 block = proc do @@ -194,7 +199,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_equal 0, counter end - test 'all products can be created by passign option even when timestamp is found' do + test 'all products can be created by passing option even when timestamp is found' do @woocommerce.stub :get_sku, nil do @woocommerce.stub :woo_post, 'id' => 1 do @woocommerce.create @@ -216,4 +221,84 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_equal 1, counter end + + test 'all products are updated if no update timestamp in checkpoint' do + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, block do + @woocommerce.update + end + end + + assert_equal 1, counter + end + + test 'only products with new rows are updated when update timestamp is found' do + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, 'id' => 1 do + @woocommerce.update + end + end + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, block do + @woocommerce.update + end + end + + assert_equal 0, counter + + products(:ski).sales_order_rows.create!(laadittu: Time.current) + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, block do + @woocommerce.update + end + end + + assert_equal 1, counter + end + + test 'all products can be updated by passing option even when timestamp is found' do + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, 'id' => 1 do + @woocommerce.update + end + end + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, block do + @woocommerce.update(all: true) + end + end + + assert_equal 1, counter + end end From c638231e6c104d78946e5245c519e6f24425bf30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Thu, 21 Sep 2017 19:43:49 +0100 Subject: [PATCH 09/14] Fix typo --- test/classes/woo/products_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index d732bc084..5cd020383 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -119,7 +119,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase assert_in_delta Keyword::WooCheckpoint.last_run_at(:create), Time.current, 10 end - test 'checkpoint is updated after if already present when creating products' do + test 'checkpoint is updated if already present when creating products' do @woocommerce.stub :get_sku, nil do @woocommerce.stub :woo_post, 'id' => 1 do assert_difference 'Keyword::WooCheckpoint.count' do From 792d102bc19e01331c44615d2cdc747772e419a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Sun, 24 Sep 2017 12:42:07 +0100 Subject: [PATCH 10/14] Add transaction model --- app/models/product/transaction.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 app/models/product/transaction.rb diff --git a/app/models/product/transaction.rb b/app/models/product/transaction.rb new file mode 100644 index 000000000..e08da74ad --- /dev/null +++ b/app/models/product/transaction.rb @@ -0,0 +1,7 @@ +class Product::Transaction < BaseModel + belongs_to :product, foreign_key: :tuoteno, primary_key: :tuoteno + + self.table_name = :tapahtuma + self.primary_key = :tunnus + self.record_timestamps = false +end From 96a8c8671fb1fa34ca19c52c0f7b65ead2194e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Sun, 24 Sep 2017 12:42:29 +0100 Subject: [PATCH 11/14] Add transactions associsation to Product --- app/models/product.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/product.rb b/app/models/product.rb index d0e7ac2d5..0793fe5da 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -36,6 +36,7 @@ class Product < BaseModel o.has_many :sales_order_rows, class_name: 'SalesOrder::Row' o.has_many :shelf_locations o.has_many :stock_transfer_rows, class_name: 'StockTransfer::Row' + o.has_many :transactions, class_name: 'Product::Transaction' end has_many :warehouses, through: :shelf_locations From ad8bf8e764991463de621b3382ef0b0d8627f6b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Sun, 24 Sep 2017 14:36:34 +0100 Subject: [PATCH 12/14] Check whether product has any new transactions --- app/classes/woo/products.rb | 3 ++- test/classes/woo/products_test.rb | 36 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index 4a20e24f9..a0f441893 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -64,7 +64,8 @@ def products_to_update(timestamp) return products unless timestamp products.select do |product| - product.rows.where('laadittu >= ?', timestamp).any? + product.rows.where('laadittu >= ?', timestamp).any? || + product.transactions.where('laadittu >= ?', timestamp).any? end end diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index 5cd020383..fc2f9e861 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -263,6 +263,42 @@ class Woo::ProductsTest < ActiveSupport::TestCase products(:ski).sales_order_rows.create!(laadittu: Time.current) + sleep 1 + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, block do + @woocommerce.update + end + end + + assert_equal 1, counter + + counter = 0 + + block = proc do + counter += 1 + { 'id' => 1 } + end + + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, block do + @woocommerce.update + end + end + + assert_equal 0, counter + + products(:ski).transactions.create!(laadittu: Time.current) + + sleep 1 + counter = 0 block = proc do From d72c97c60f1fb1ad99233559a25dd045794f0dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Sun, 24 Sep 2017 14:56:12 +0100 Subject: [PATCH 13/14] Refactor tests --- test/classes/woo/products_test.rb | 60 ++++++++++--------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/test/classes/woo/products_test.rb b/test/classes/woo/products_test.rb index fc2f9e861..1237388e1 100644 --- a/test/classes/woo/products_test.rb +++ b/test/classes/woo/products_test.rb @@ -19,6 +19,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase products(:ski).update(hinnastoon: 'w') Keyword::WooCheckpoint.delete_all + Product::Transaction.delete_all end test 'should initialize' do @@ -132,8 +133,8 @@ class Woo::ProductsTest < ActiveSupport::TestCase end test 'checkpoint is updated after updating stocks' do - @woocommerce.stub :get_sku, nil do - @woocommerce.stub :woo_post, 'id' => 1 do + @woocommerce.stub :get_sku, {} do + @woocommerce.stub :woo_put, 'id' => 1 do assert_difference 'Keyword::WooCheckpoint.count' do 2.times { @woocommerce.update } end @@ -175,13 +176,9 @@ class Woo::ProductsTest < ActiveSupport::TestCase end test 'only new products are created when create timestamp is found' do - @woocommerce.stub :get_sku, nil do - @woocommerce.stub :woo_post, 'id' => 1 do - @woocommerce.create - end - end + products(:ski).update!(luontiaika: 1.second.ago) - products(:ski).update!(luontiaika: 10.seconds.ago) + Keyword::WooCheckpoint.update_timestamp(:create) counter = 0 @@ -200,11 +197,9 @@ class Woo::ProductsTest < ActiveSupport::TestCase end test 'all products can be created by passing option even when timestamp is found' do - @woocommerce.stub :get_sku, nil do - @woocommerce.stub :woo_post, 'id' => 1 do - @woocommerce.create - end - end + products(:ski).update!(luontiaika: 1.second.ago) + + Keyword::WooCheckpoint.update_timestamp(:create) counter = 0 @@ -240,11 +235,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase end test 'only products with new rows are updated when update timestamp is found' do - @woocommerce.stub :get_sku, {} do - @woocommerce.stub :woo_put, 'id' => 1 do - @woocommerce.update - end - end + Keyword::WooCheckpoint.update_timestamp(:update) counter = 0 @@ -260,10 +251,12 @@ class Woo::ProductsTest < ActiveSupport::TestCase end assert_equal 0, counter + end - products(:ski).sales_order_rows.create!(laadittu: Time.current) + test 'products with new rows are updated when timestamp is found' do + Keyword::WooCheckpoint.update_timestamp(:update) - sleep 1 + products(:ski).sales_order_rows.create!(laadittu: 1.second.from_now) counter = 0 @@ -279,25 +272,12 @@ class Woo::ProductsTest < ActiveSupport::TestCase end assert_equal 1, counter + end - counter = 0 - - block = proc do - counter += 1 - { 'id' => 1 } - end - - @woocommerce.stub :get_sku, {} do - @woocommerce.stub :woo_put, block do - @woocommerce.update - end - end - - assert_equal 0, counter - - products(:ski).transactions.create!(laadittu: Time.current) + test 'products with new transactions are updated when timestamp is found' do + Keyword::WooCheckpoint.update_timestamp(:update) - sleep 1 + products(:ski).transactions.create!(laadittu: 1.second.from_now) counter = 0 @@ -316,11 +296,7 @@ class Woo::ProductsTest < ActiveSupport::TestCase end test 'all products can be updated by passing option even when timestamp is found' do - @woocommerce.stub :get_sku, {} do - @woocommerce.stub :woo_put, 'id' => 1 do - @woocommerce.update - end - end + Keyword::WooCheckpoint.update_timestamp(:update) counter = 0 From 871365bb06b22d79e55634d3012cdc6028fe748a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20Ja=CC=88rvinen?= Date: Sun, 24 Sep 2017 15:03:56 +0100 Subject: [PATCH 14/14] Refactor --- app/classes/woo/products.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/classes/woo/products.rb b/app/classes/woo/products.rb index a0f441893..37d9f8046 100644 --- a/app/classes/woo/products.rb +++ b/app/classes/woo/products.rb @@ -4,11 +4,9 @@ class Woo::Products < Woo::Base # Create products to woocommerce def create(all: false) - timestamp = all ? nil : Keyword::WooCheckpoint.last_run_at(:create) - created_count = 0 - products_to_create(timestamp).each do |product| + products_to_create(all: all).each do |product| if get_sku(product.tuoteno) logger.info "Tuote #{product.tuoteno} on jo verkkokaupassa" @@ -25,11 +23,9 @@ def create(all: false) # Update product stock quantity def update(all: false) - timestamp = all ? nil : Keyword::WooCheckpoint.last_run_at(:update) - updated_count = 0 - products_to_update(timestamp).each do |product| + products_to_update(all: all).each do |product| woo_product = get_sku(product.tuoteno) unless woo_product @@ -54,13 +50,17 @@ def products Product.where(status: %w(A P)).where(hinnastoon: 'W') end - def products_to_create(timestamp) + def products_to_create(all: false) + timestamp = all ? nil : Keyword::WooCheckpoint.last_run_at(:create) + return products unless timestamp products.where('luontiaika >= ?', timestamp) end - def products_to_update(timestamp) + def products_to_update(all: false) + timestamp = all ? nil : Keyword::WooCheckpoint.last_run_at(:update) + return products unless timestamp products.select do |product|