Class: PartnerService::Reservations::Exports::ValidationService
- Inherits:
-
Object
- Object
- PartnerService::Reservations::Exports::ValidationService
- 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
-
#date_filter_type ⇒ Object
readonly
Returns the value of attribute date_filter_type.
-
#date_type ⇒ Object
readonly
Returns the value of attribute date_type.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#restaurant ⇒ Object
readonly
Returns the value of attribute restaurant.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
-
.build_export_params(permitted_params, start_date, end_date) ⇒ Hash
Build export parameters hash for consistent usage across controllers.
Instance Method Summary collapse
-
#initialize(restaurant, export_params) ⇒ ValidationService
constructor
A new instance of ValidationService.
- #validate_data_availability ⇒ Object
-
#validate_export_parameters ⇒ Object
Validate export parameters (same logic as worker).
-
#validate_export_request ⇒ Object
Combined validation - parameters + data availability.
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_type ⇒ Object (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_type ⇒ Object (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_date ⇒ Object (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_time ⇒ Object (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 |
#restaurant ⇒ Object (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_date ⇒ Object (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_time ⇒ Object (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
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_availability ⇒ Object
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., }) { valid: false, message: I18n.t('partner.errors.reservations.validation_failed'), count: 0, } end |
#validate_export_parameters ⇒ Object
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_request ⇒ Object
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 |