Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pull Vouchers into CSV

user_not_founds = []
new_csv = []
CSV.foreach('/tmp/compensation.csv', headers: true) do |row|
  name = row['Comp Method'].to_s
  if new_csv.blank?
    headers = row.headers
    headers.push('voucher ids')
    new_csv.push headers.to_csv
  end

  next unless name.include?('GF')

  email = row['email']

  user = User.find_by email: email
  if user.blank?
    user_not_founds.push row
    next
  end

  body = row.to_h.values

  vouchers = Voucher.where('id >= ?', 484000).where(user_id: user.id).where('name LIKE ?', 'GF%')
  if vouchers.present?
    body.push vouchers.pluck(:id).map(&:to_s).join(', ')
  end
  new_csv.push body.to_csv
end

File.open('/tmp/new_comp.csv', 'w') do |f|
  f.write new_csv.join('')
  f.close
end

File.open('/tmp/not_found.csv', 'w') do |f|
  data = user_not_founds.map do |row|
    row.to_h.values.to_csv
  end.join('')
  f.write data
  f.close
end