2022-01-18 15:34:21 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test_helper'
|
|
|
|
|
2022-10-11 15:14:53 +02:00
|
|
|
class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationTest
|
2022-01-18 15:34:21 +01:00
|
|
|
setup do
|
|
|
|
@user = User.members.without_subscription.first
|
|
|
|
@admin = User.with_role(:admin).first
|
|
|
|
login_as(@admin, scope: :user)
|
|
|
|
end
|
|
|
|
|
2022-10-05 13:11:17 +02:00
|
|
|
test 'build default stats' do
|
|
|
|
::Statistics::BuilderService.generate_statistic
|
|
|
|
end
|
|
|
|
|
2022-08-29 17:34:09 +02:00
|
|
|
test 'build stats' do
|
2022-10-10 12:20:39 +02:00
|
|
|
# Create a reservation to generate an invoice (2 days ago)
|
2022-01-18 15:34:21 +01:00
|
|
|
machine = Machine.find(1)
|
2023-01-31 17:10:07 +01:00
|
|
|
machine_slot = Availability.find(19).slots.first
|
2022-10-10 12:20:39 +02:00
|
|
|
travel_to(2.days.ago)
|
2022-01-18 15:34:21 +01:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
|
|
|
customer_id: @user.id,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.class.name,
|
2022-07-13 16:28:43 +02:00
|
|
|
slots_reservations_attributes: [
|
2022-01-18 15:34:21 +01:00
|
|
|
{
|
2023-01-31 17:10:07 +01:00
|
|
|
slot_id: machine_slot.id
|
2022-01-18 15:34:21 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}.to_json, headers: default_headers
|
2022-10-10 12:20:39 +02:00
|
|
|
travel_back
|
2022-01-18 15:34:21 +01:00
|
|
|
|
2022-10-10 12:20:39 +02:00
|
|
|
# Create a subscription to generate another invoice (1 day ago)
|
2022-01-18 15:34:21 +01:00
|
|
|
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan')
|
2022-10-10 12:20:39 +02:00
|
|
|
travel_to(1.day.ago)
|
2022-01-18 15:34:21 +01:00
|
|
|
post '/api/local_payment/confirm_payment',
|
|
|
|
params: {
|
|
|
|
customer_id: @user.id,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}.to_json, headers: default_headers
|
2022-10-24 12:12:49 +02:00
|
|
|
|
|
|
|
# Create a training reservation (1 day ago)
|
|
|
|
training = Training.find(1)
|
|
|
|
tr_slot = Availability.find(2).slots.first
|
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
|
|
|
customer_id: @user.id,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: training.id,
|
|
|
|
reservable_type: training.class.name,
|
|
|
|
slots_reservations_attributes: [
|
|
|
|
{
|
|
|
|
slot_id: tr_slot.id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}.to_json, headers: default_headers
|
2022-10-10 12:20:39 +02:00
|
|
|
travel_back
|
2022-01-18 15:34:21 +01:00
|
|
|
|
2023-01-31 17:10:07 +01:00
|
|
|
# Create another machine reservation (today)
|
|
|
|
machine_slot2 = Availability.find(19).slots.last
|
2022-10-10 12:20:39 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
|
|
|
customer_id: @user.id,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.class.name,
|
|
|
|
slots_reservations_attributes: [
|
|
|
|
{
|
2023-01-31 17:10:07 +01:00
|
|
|
slot_id: machine_slot2.id
|
2022-10-10 12:20:39 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}.to_json, headers: default_headers
|
|
|
|
|
2022-11-17 16:59:33 +01:00
|
|
|
Stats::Machine.refresh_index!
|
|
|
|
Stats::Training.refresh_index!
|
|
|
|
Stats::Subscription.refresh_index!
|
|
|
|
|
2022-10-10 12:20:39 +02:00
|
|
|
# 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: 2.days.ago.beginning_of_day,
|
2022-08-29 17:34:09 +02:00
|
|
|
end_date: DateTime.current.end_of_day })
|
|
|
|
|
|
|
|
Stats::Machine.refresh_index!
|
|
|
|
|
2022-10-10 12:20:39 +02:00
|
|
|
# first machine reservation (2 days ago)
|
|
|
|
stat_booking = Stats::Machine.search(query: { bool: { must: [{ term: { date: 2.days.ago.to_date.iso8601 } },
|
|
|
|
{ term: { type: 'booking' } }] } }).first
|
|
|
|
assert_not_nil stat_booking
|
|
|
|
assert_equal machine.friendly_id, stat_booking['subType']
|
2023-01-31 17:10:07 +01:00
|
|
|
assert_equal 1, stat_booking['stat']
|
2022-10-10 12:20:39 +02:00
|
|
|
check_statistics_on_user(stat_booking)
|
|
|
|
|
|
|
|
stat_hour = Stats::Machine.search(query: { bool: { must: [{ term: { date: 2.days.ago.to_date.iso8601 } },
|
|
|
|
{ term: { type: 'hour' } }] } }).first
|
|
|
|
|
|
|
|
assert_not_nil stat_hour
|
|
|
|
assert_equal machine.friendly_id, stat_hour['subType']
|
2023-01-31 17:10:07 +01:00
|
|
|
assert_equal (machine_slot.duration.to_i / 3600.0).to_i, stat_hour['stat']
|
|
|
|
assert_includes stat_hour['machineDates'], machine_slot.start_at.to_date.iso8601
|
2022-10-10 12:20:39 +02:00
|
|
|
check_statistics_on_user(stat_hour)
|
|
|
|
|
|
|
|
# second machine reservation (today)
|
2022-08-29 17:34:09 +02:00
|
|
|
stat_booking = Stats::Machine.search(query: { bool: { must: [{ term: { date: DateTime.current.to_date.iso8601 } },
|
|
|
|
{ term: { type: 'booking' } }] } }).first
|
|
|
|
assert_not_nil stat_booking
|
|
|
|
assert_equal machine.friendly_id, stat_booking['subType']
|
2023-01-31 17:10:07 +01:00
|
|
|
assert_equal 1, stat_booking['stat']
|
2022-08-29 17:34:09 +02:00
|
|
|
check_statistics_on_user(stat_booking)
|
|
|
|
|
|
|
|
stat_hour = Stats::Machine.search(query: { bool: { must: [{ term: { date: DateTime.current.to_date.iso8601 } },
|
|
|
|
{ term: { type: 'hour' } }] } }).first
|
|
|
|
|
|
|
|
assert_not_nil stat_hour
|
|
|
|
assert_equal machine.friendly_id, stat_hour['subType']
|
2023-01-31 17:10:07 +01:00
|
|
|
assert_equal (machine_slot2.duration.to_i / 3600.0).to_i, stat_hour['stat']
|
|
|
|
assert_includes stat_hour['machineDates'], machine_slot2.start_at.to_date.iso8601
|
2022-08-29 17:34:09 +02:00
|
|
|
check_statistics_on_user(stat_hour)
|
|
|
|
|
2022-10-24 12:12:49 +02:00
|
|
|
# training
|
2022-11-17 16:59:33 +01:00
|
|
|
Stats::Training.refresh_index!
|
|
|
|
|
2022-10-24 12:12:49 +02:00
|
|
|
stat_training = Stats::Training.search(query: { bool: { must: [{ term: { date: 1.day.ago.to_date.iso8601 } },
|
|
|
|
{ term: { type: 'booking' } }] } }).first
|
|
|
|
assert_not_nil stat_training
|
|
|
|
assert_equal training.friendly_id, stat_training['subType']
|
2023-01-31 17:10:07 +01:00
|
|
|
assert_equal tr_slot.start_at.to_date.iso8601, stat_training['trainingDate']
|
2022-10-24 12:12:49 +02:00
|
|
|
check_statistics_on_user(stat_training)
|
|
|
|
|
2022-10-10 12:20:39 +02:00
|
|
|
# subscription
|
2022-08-29 17:34:09 +02:00
|
|
|
Stats::Subscription.refresh_index!
|
|
|
|
|
2022-10-10 12:20:39 +02:00
|
|
|
stat_subscription = Stats::Subscription.search(query: { bool: { must: [{ term: { date: 1.day.ago.to_date.iso8601 } },
|
2022-08-29 17:34:09 +02:00
|
|
|
{ term: { type: plan.find_statistic_type.key } }] } }).first
|
|
|
|
|
|
|
|
assert_not_nil stat_subscription
|
|
|
|
assert_equal plan.find_statistic_type.key, stat_subscription['type']
|
|
|
|
assert_equal plan.slug, stat_subscription['subType']
|
|
|
|
assert_equal plan.id, stat_subscription['planId']
|
|
|
|
assert_equal 1, stat_subscription['stat']
|
|
|
|
check_statistics_on_user(stat_subscription)
|
|
|
|
end
|
2022-01-18 15:34:21 +01:00
|
|
|
|
2022-08-29 17:34:09 +02:00
|
|
|
def check_statistics_on_user(stat)
|
|
|
|
assert_equal @user.statistic_profile.str_gender, stat['gender']
|
|
|
|
assert_equal @user.statistic_profile.age.to_i, stat['age']
|
|
|
|
assert_equal @user.statistic_profile.group.slug, stat['group']
|
2022-01-18 15:34:21 +01:00
|
|
|
end
|
|
|
|
end
|