diff --git a/app/services/statistics/builder_service.rb b/app/services/statistics/builder_service.rb index 53d7a49a5..60fcc57d2 100644 --- a/app/services/statistics/builder_service.rb +++ b/app/services/statistics/builder_service.rb @@ -11,6 +11,7 @@ class Statistics::BuilderService Statistics::Builders::ReservationsBuilderService.build(options) Statistics::Builders::MembersBuilderService.build(options) Statistics::Builders::ProjectsBuilderService.build(options) + Statistics::Builders::StoreOrdersBuilderService.build(options) end private diff --git a/app/services/statistics/builders/store_orders_builder_service.rb b/app/services/statistics/builders/store_orders_builder_service.rb index 414505fa3..d57f4c7f8 100644 --- a/app/services/statistics/builders/store_orders_builder_service.rb +++ b/app/services/statistics/builders/store_orders_builder_service.rb @@ -15,6 +15,8 @@ class Statistics::Builders::StoreOrdersBuilderService ca: o[:ca], products: o[:order_products], categories: o[:order_categories], + orderId: o[:order_id], + orderState: o[:order_state], stat: 1 }.merge(user_info_stat(o))) end end diff --git a/app/services/statistics/fetcher_service.rb b/app/services/statistics/fetcher_service.rb index 6c38a0f7a..d9be86fcb 100644 --- a/app/services/statistics/fetcher_service.rb +++ b/app/services/statistics/fetcher_service.rb @@ -6,6 +6,7 @@ class Statistics::FetcherService include Statistics::Concerns::HelpersConcern include Statistics::Concerns::ComputeConcern include Statistics::Concerns::ProjectsConcern + include Statistics::Concerns::StoreOrdersConcern class << self def subscriptions_list(options = default_options) @@ -185,7 +186,8 @@ class Statistics::FetcherService def store_orders_list(options = default_options) result = [] Order.includes(order_items: [:orderable]) - .where('order_items.orderable_type == "Product"') + .joins(:order_items) + .where("order_items.orderable_type = 'Product'") .where('orders.created_at >= :start_date AND orders.created_at <= :end_date', options) .each do |o| result.push({ date: o.created_at.to_date, ca: calcul_ca(o.invoice) } diff --git a/test/fixtures/order_activities.yml b/test/fixtures/order_activities.yml index 1d9fa5500..707e9d505 100644 --- a/test/fixtures/order_activities.yml +++ b/test/fixtures/order_activities.yml @@ -116,8 +116,8 @@ order_activity_21: operator_profile_id: activity_type: paid note: - created_at: '2022-10-04 14:35:40.554303' - updated_at: '2022-10-04 14:35:40.554303' + created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + updated_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> order_activity_22: id: 22 order_id: 15 @@ -125,5 +125,5 @@ order_activity_22: activity_type: ready note: "

Votre commande est prête, merci de venir la récupérer jeudi après 14 h.

" - created_at: '2022-10-05 10:04:53.113486' - updated_at: '2022-10-05 10:04:53.113486' + created_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> diff --git a/test/fixtures/order_items.yml b/test/fixtures/order_items.yml index 5f1cffd10..4ce80a210 100644 --- a/test/fixtures/order_items.yml +++ b/test/fixtures/order_items.yml @@ -126,5 +126,5 @@ order_item_24: amount: 3000 quantity: 1 is_offered: - created_at: '2022-10-04 14:35:29.003092' - updated_at: '2022-10-04 14:35:29.003092' + created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + updated_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> diff --git a/test/fixtures/orders.yml b/test/fixtures/orders.yml index 15f66308f..2802e9dec 100644 --- a/test/fixtures/orders.yml +++ b/test/fixtures/orders.yml @@ -150,8 +150,8 @@ order_15: reference: '005900-10-22' state: ready total: 3000 - created_at: '2022-10-04 14:32:28.706806' - updated_at: '2022-10-05 10:04:53.142267' + created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> wallet_amount: wallet_transaction_id: payment_method: local diff --git a/test/services/statistic_service_test.rb b/test/services/statistics/reservation_subscription_statistic_service_test.rb similarity index 98% rename from test/services/statistic_service_test.rb rename to test/services/statistics/reservation_subscription_statistic_service_test.rb index e04412be6..ef9afb4e5 100644 --- a/test/services/statistic_service_test.rb +++ b/test/services/statistics/reservation_subscription_statistic_service_test.rb @@ -2,7 +2,7 @@ require 'test_helper' -class StatisticServiceTest < ActionDispatch::IntegrationTest +class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationTest setup do @user = User.members.without_subscription.first @admin = User.with_role(:admin).first diff --git a/test/services/statistics/store_statistic_service_test.rb b/test/services/statistics/store_statistic_service_test.rb new file mode 100644 index 000000000..1233d1326 --- /dev/null +++ b/test/services/statistics/store_statistic_service_test.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'test_helper' + +class StoreStatisticServiceTest < ActionDispatch::IntegrationTest + setup do + @order = Order.find(15) + end + + test 'build stats about orders' do + # Build the stats for the last 3 days, we expect the above invoices (reservations+subscription) to appear in the resulting stats + ::Statistics::BuilderService.generate_statistic({ start_date: DateTime.current.beginning_of_day, + end_date: DateTime.current.end_of_day }) + + Stats::StoreOrder.refresh_index! + + # we should find order id 15 (created today) + stat_order = Stats::StoreOrder.search(query: { bool: { must: [{ term: { date: DateTime.current.to_date.iso8601 } }, + { term: { type: 'order' } }] } }).first + assert_not_nil stat_order + assert_equal @order.id, stat_order['orderId'] + check_statistics_on_user(stat_order) + end + + def check_statistics_on_user(stat) + assert_equal @order.statistic_profile.str_gender, stat['gender'] + assert_equal @order.statistic_profile.age.to_i, stat['age'] + assert_equal @order.statistic_profile.group.slug, stat['group'] + end +end