Class: Admin::IaReportConversionsController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/ia_report_conversions_controller.rb

Defined Under Namespace

Classes: InvalidStatus

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Method Summary collapse

Methods inherited from BaseController

#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from AdminHelper

#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?

Methods included from UpdateLocaleConcern

#setup_locale

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#createObject



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
70
71
72
73
# File 'app/controllers/admin/ia_report_conversions_controller.rb', line 37

def create
  file = params.require(:ia_report_conversion)[:file]&.path
  filename = "Involve-Asia-Validation-#{Time.current_time.to_i}#{rand(10_000)}.xlsx"
  filepath = Rails.root.join('tmp', filename).to_s

  data = Roo::Spreadsheet.open(file)
  header = data.row(1)

  Xlsxtream::Workbook.open(filepath) do |xlsx|
    xlsx.write_worksheet 'Reservation Report' do |sheet|
      sheet.add_row header

      data.each_with_index do |row, _idx|
        if row == header
          row[15] = 'Reason'
          next
        end

        # we were using row[5] # column F that contain reservation ID
        # but now it moved to row[11] # column L
        reservation = Reservation.find_by(id: row[11])
        status, reason = valid?(reservation)
        row[14] = status
        row[15] = reason
        sheet.add_row row
      end
    end
  end

  report = IaReportConversion.new file: File.open(filepath)
  if report.save
    flash[:notice] = 'Validating success'
  else
    flash[:error] = 'Validating failed'
  end
  redirect_to admin_ia_report_conversions_path
end

#destroyObject



16
17
18
19
20
21
22
23
# File 'app/controllers/admin/ia_report_conversions_controller.rb', line 16

def destroy
  if @ia_report_conversion.destroy
    flash[:notice] = 'file deleted successfully'
  else
    flash[:error] = 'failed delete file'
  end
  redirect_to admin_ia_report_conversions_path
end

#indexObject



4
5
6
7
8
9
10
# File 'app/controllers/admin/ia_report_conversions_controller.rb', line 4

def index
  set_meta_tags title: 'Involve asia validation'
  @grid = ::Admin::IaReportConversionsGrid.new(params[:admin_ia_report_conversions_grid]) do |scope|
    scope.page(params[:page]).per(50)
  end
  fresh_when @grid.assets.cache_key
end

#newObject



12
13
14
# File 'app/controllers/admin/ia_report_conversions_controller.rb', line 12

def new
  @ia_report_conversion = IaReportConversion.new
end

#send_emailObject



25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/admin/ia_report_conversions_controller.rb', line 25

def send_email
  download_link = @ia_report_conversion.file_url.dup
  download_link.gsub!(Figaro.env.CDN_URL, Figaro.env.HH_HOST_URL)
  download_link = "#{Figaro.env.HH_HOST_URL!}#{download_link}" unless download_link.include? 'http'
  receivers = [AdminSetting.involve_asia_staff_email]
  StaffMailer.send_ia_staff_validation_report(download_link, receivers).deliver_later!

  @ia_report_conversion.send_email_at = Time.thai_time
  @ia_report_conversion.save
  redirect_to admin_ia_report_conversions_path, notice: 'Email successfully sent to involve asia staff'
end