Class: RestaurantGroupService::UpdateMembers

Inherits:
Object
  • Object
show all
Defined in:
app/services/restaurant_group_service/update_members.rb

Overview

Service class for managing and updating members of a restaurant group and staff.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restaurant_group) ⇒ UpdateMembers

Initializes the service with a restaurant group.

Parameters:

  • restaurant_group (RestaurantGroup)

    The restaurant group to be updated.



11
12
13
14
15
# File 'app/services/restaurant_group_service/update_members.rb', line 11

def initialize(restaurant_group)
  @restaurant_group = restaurant_group
  @staff_account = restaurant_group.
  @errors = []
end

Instance Attribute Details

#errorsRestaurantGroup, ... (readonly)

Returns:

  • (RestaurantGroup)

    the restaurant group being updated.

  • (StaffAccount)

    the staff account associated with the restaurant group.

  • (Array<String>)

    list of errors encountered during execution.



7
8
9
# File 'app/services/restaurant_group_service/update_members.rb', line 7

def errors
  @errors
end

#restaurant_groupRestaurantGroup, ... (readonly)

Returns:

  • (RestaurantGroup)

    the restaurant group being updated.

  • (StaffAccount)

    the staff account associated with the restaurant group.

  • (Array<String>)

    list of errors encountered during execution.



7
8
9
# File 'app/services/restaurant_group_service/update_members.rb', line 7

def restaurant_group
  @restaurant_group
end

#staff_accountRestaurantGroup, ... (readonly)

Returns:

  • (RestaurantGroup)

    the restaurant group being updated.

  • (StaffAccount)

    the staff account associated with the restaurant group.

  • (Array<String>)

    list of errors encountered during execution.



7
8
9
# File 'app/services/restaurant_group_service/update_members.rb', line 7

def 
  @staff_account
end

Instance Method Details

#call(restaurant_params, id = nil) ⇒ Boolean

Updates the restaurant group members based on the provided parameters.

Parameters:

  • restaurant_params (Array<Hash>)

    List of restaurant parameters.

  • id (Integer, nil) (defaults to: nil)

    Optional ID to identify specific restaurants for removal.

Returns:

  • (Boolean)

    true if the operation is successful, false otherwise.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'app/services/restaurant_group_service/update_members.rb', line 21

def call(restaurant_params, id = nil)
  ActiveRecord::Base.transaction do
    get_or_create_staff

    # if @restaurant.group.merge_allotment is true and there is params restaurants with no nickname then raise error
    if restaurant_group.merge_allotment? && restaurant_params.any? do |restaurant_param|
         restaurant_param['nick_name'].blank?
       end
      errors << 'Nick name is required for all restaurants when merge allotment is enabled.'
      raise ActiveRecord::Rollback
    end

    new_restaurant_ids = []

    restaurant_params.each do |restaurant_param|
      restaurant_id = restaurant_param['id'].to_i

      if restaurant_param[:_destroy] == 'false'
        handle_add_or_update(restaurant_param)
        new_restaurant_ids << restaurant_id
      elsif id
        handle_remove(restaurant_id)
        new_restaurant_ids.delete restaurant_id
      end
    end

    # Remove restaurants not in the updated list.
    if new_restaurant_ids.present?
      removed_ids = restaurant_group.restaurants.ids - new_restaurant_ids
      removed_ids.each { |id| handle_remove(id) }
    end

    # sync staff restaurants with group restaurants
    all_staffs = restaurant_group.staffs
    all_staffs.each do |staff|
      sync_staff_restaurants(restaurant_group, staff)
    end

    restaurant_group.touch

  rescue ActiveRecord::Rollback => e
    Rails.logger.error("Update Members Error: #{e.message}")
    return false
  end

  true
end