Class: PartnerService::Reservations::Exports::ValidationService

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/partner_service/reservations/exports/validation_service.rb

Overview

Service to validate export parameters and check data availability before scheduling workers This prevents workers from being scheduled when no data would be exported

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restaurant, export_params) ⇒ ValidationService

Returns a new instance of ValidationService.



13
14
15
16
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 13

def initialize(restaurant, export_params)
  @restaurant = restaurant
  parse_export_parameters(export_params)
end

Instance Attribute Details

#date_filter_typeObject (readonly)

Returns the value of attribute date_filter_type.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def date_filter_type
  @date_filter_type
end

#date_typeObject (readonly)

Returns the value of attribute date_type.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def date_type
  @date_type
end

#end_dateObject (readonly)

Returns the value of attribute end_date.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def end_date
  @end_date
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def end_time
  @end_time
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def restaurant
  @restaurant
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def start_date
  @start_date
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



11
12
13
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 11

def start_time
  @start_time
end

Class Method Details

.build_export_params(permitted_params, start_date, end_date) ⇒ Hash

Build export parameters hash for consistent usage across controllers

Parameters:

  • permitted_params (Hash)

    Controller permitted parameters

  • start_date (Time)

    Parsed start date

  • end_date (Time)

    Parsed end date

Returns:

  • (Hash)

    Standardized export parameters



23
24
25
26
27
28
29
30
31
32
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 23

def self.build_export_params(permitted_params, start_date, end_date)
  {
    start_date: start_date.to_date.to_s,
    end_date: end_date.to_date.to_s,
    date_type: permitted_params[:date_type] || 'date',
    start_time: permitted_params[:start_time],
    end_time: permitted_params[:end_time],
    date_filter_type: permitted_params[:date_filter_type],
  }
end

Instance Method Details

#validate_data_availabilityObject



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
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 37

def validate_data_availability
  # Build optimized query for counting only
  query = build_reservation_query(for_count: true)

  # Count reservations without loading them all into memory
  reservation_count = query.count

  if reservation_count == 0
    {
      valid: false,
      message: I18n.t('partner.errors.reservations.no_reservations_found'),
      count: 0,
    }
  else
    {
      valid: true,
      message: I18n.t('partner.success.reservations.reservations_found', count: reservation_count),
      count: reservation_count,
    }
  end
rescue StandardError => e
  # Remove sensitive export parameters from logs for security
  APMErrorHandler.report('Export validation failed', {
                           restaurant_id: restaurant.id,
                           error: e.message,
                         })

  {
    valid: false,
    message: I18n.t('partner.errors.reservations.validation_failed'),
    count: 0,
  }
end

#validate_export_parametersObject

Validate export parameters (same logic as worker)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 72

def validate_export_parameters
  errors = []

  errors << I18n.t('partner.errors.reservations.start_date_invalid') if start_date.blank?

  if date_filter_type != 'single'
    errors << I18n.t('partner.errors.reservations.end_date_invalid') if end_date.blank?

    if start_date.present? && end_date.present? && start_date > end_date
      errors << I18n.t('partner.errors.reservations.start_date_must_be_before_end_date')
    end
  end

  {
    valid: errors.empty?,
    errors: errors,
  }
end

#validate_export_requestObject

Combined validation - parameters + data availability



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/services/partner_service/reservations/exports/validation_service.rb', line 92

def validate_export_request
  # First validate parameters
  param_validation = validate_export_parameters
  return param_validation unless param_validation[:valid]

  # Then validate data availability
  data_validation = validate_data_availability

  {
    valid: data_validation[:valid],
    message: data_validation[:message],
    count: data_validation[:count],
    errors: data_validation[:valid] ? [] : [data_validation[:message]],
  }
end