|
| 1 | +require "prawn/measurement_extensions" |
| 2 | + |
| 3 | +namespace :oltrino do |
| 4 | + |
| 5 | + desc """ |
| 6 | + Generates :num customers with wallets of :amount Oltrino in the given group |
| 7 | + and generates the related PDF and CSV files |
| 8 | + """ |
| 9 | + task :generate_customers, [:group_id, :num, :amount] => :environment do |t, args| |
| 10 | + group = Group.find_by(id: args[:group_id]) # improve this |
| 11 | + abort 'Invalid group_id' if group.nil? |
| 12 | + num = args[:num].to_i || 10 |
| 13 | + amount = args[:amount].to_i || 50 |
| 14 | + currency = group.currency |
| 15 | + wallets = [] |
| 16 | + num.times do |
| 17 | + commoner = Commoner.find_or_create_by name: generate_commoner_name do |commoner| |
| 18 | + user_attributes_for(commoner) |
| 19 | + end |
| 20 | + Membership.create(commoner: commoner, group: group) |
| 21 | + wallet = Wallet.find_by(currency: currency, walletable: commoner) |
| 22 | + transfer_oltrino_to_wallet(wallet, amount) |
| 23 | + wallets << wallet |
| 24 | + end |
| 25 | + group.wallet.refresh_balance |
| 26 | + generate_pdf_for_wallets(wallets, amount) |
| 27 | + generate_csv_for_wallets(wallets, amount) |
| 28 | + end |
| 29 | + |
| 30 | + desc "Destroy and recreate all the wallets in the given group" |
| 31 | + task :reset_group_wallets, [:group_id] => :environment do |t, args| |
| 32 | + group = Group.find_by(id: args[:group_id]) # improve this |
| 33 | + abort 'Invalid group_id' if group.nil? |
| 34 | + currency = group.currency |
| 35 | + # reset group wallet |
| 36 | + group.wallet.transactions.destroy_all |
| 37 | + group.wallet.destroy |
| 38 | + Wallet.create(walletable: group, |
| 39 | + address: '', |
| 40 | + currency: currency) |
| 41 | + # reset members' wallets |
| 42 | + group.members.find_each do |member| |
| 43 | + wallet = Wallet.find_by(currency: currency, walletable: member) |
| 44 | + wallet.transactions.destroy_all |
| 45 | + wallet.destroy |
| 46 | + new_wallet = Wallet.create(walletable: member, |
| 47 | + address: Digest::SHA2.hexdigest(member.email + Time.now.to_s), |
| 48 | + currency: currency) |
| 49 | + transfer_oltrino_to_wallet(new_wallet, 10) |
| 50 | + end |
| 51 | + group.wallet.refresh_balance |
| 52 | + end |
| 53 | + |
| 54 | + desc "Generates a PDF with the QR codes of all the wallets in the given group" |
| 55 | + task :generate_qrcode_pdf, [:group_id] => :environment do |t, args| |
| 56 | + group = Group.find_by(id: args[:group_id]) # improve this |
| 57 | + abort 'Invalid group_id' if group.nil? |
| 58 | + currency = group.currency |
| 59 | + Prawn::Document.generate("#{host_tmp_path}/#{timestamp}_oltrino_qrcodes.pdf", page_options) do |
| 60 | + group.members.find_each do |member| |
| 61 | + wallet = Wallet.find_by(currency: currency, walletable: member) |
| 62 | + # see http://www.qrcode.com/en/about/version.html for versions |
| 63 | + # 7 -> 45x45 modules |
| 64 | + # 8 -> 49x49 modules |
| 65 | + qr = RQRCode::QRCode.new( |
| 66 | + commoner_wallet_url(member, wallet), |
| 67 | + size: 7) |
| 68 | + qr_svg = qr.as_svg(offset: 0, |
| 69 | + color: '000', |
| 70 | + shape_rendering: 'crispEdges', |
| 71 | + module_size: 5.5) # in pixels |
| 72 | + svg qr_svg, position: :center, width: 179 |
| 73 | + move_down 2 |
| 74 | + text "WalletID #{wallet.id}", align: :center |
| 75 | + start_new_page |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + desc "Generates a CSV with the URLs of all the wallets in the given group" |
| 81 | + task :generate_wallet_urls_csv, [:group_id] => :environment do |t, args| |
| 82 | + group = Group.find_by(id: args[:group_id]) # improve this |
| 83 | + abort 'Invalid group_id' if group.nil? |
| 84 | + currency = group.currency |
| 85 | + output_file = File.join(host_tmp_path, ("#{timestamp}_oltrino_wallet_urls.csv")) |
| 86 | + CSV.open(output_file, 'wb') do |csv| |
| 87 | + csv << %w(Wallet_URL Wallet_ID) |
| 88 | + group.members.find_each do |member| |
| 89 | + wallet = Wallet.find_by(currency: currency, walletable: member) |
| 90 | + wallet_url = commoner_wallet_url(member, wallet) |
| 91 | + csv << [wallet_url, wallet.id] |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + task :import_sellers, [:group_id] => :environment do |t, args| |
| 97 | + group = Group.find_by(id: args[:group_id]) |
| 98 | + abort 'Invalid group_id' if group.nil? |
| 99 | + puts group.name |
| 100 | + currency = group.currency |
| 101 | + wallets = [] |
| 102 | + # out_csv = CSV.new(File.join(host_tmp_path, 'sf_sellers_out.csv'), headers: true) |
| 103 | + CSV.open(File.join(host_tmp_path, 'sf_sellers_out.csv'), 'wb') do |out_csv| |
| 104 | + out_csv << %w(Name Email Password) |
| 105 | + CSV.foreach(File.join(host_tmp_path, 'sf_sellers.csv'), headers: true, header_converters: :symbol) do |row| |
| 106 | + hash_row = row.to_hash |
| 107 | + name = hash_row[:name] |
| 108 | + email = hash_row[:email] |
| 109 | + password = [('a'..'z').to_a.shuffle[0,8].join, rand(100..999)].join |
| 110 | + puts "#{hash_row[:name]} #{hash_row[:email]}" |
| 111 | + out_csv << [name, email, password] |
| 112 | + commoner = Commoner.find_or_create_by name: name do |commoner| |
| 113 | + seller_user_attributes_for(commoner, email, password) |
| 114 | + end |
| 115 | + Membership.find_or_create_by(commoner: commoner, group: group, role: 'editor') |
| 116 | + wallet = Wallet.find_by(currency: currency, walletable: commoner) |
| 117 | + wallets << wallet |
| 118 | + end |
| 119 | + end |
| 120 | + group.wallet.refresh_balance |
| 121 | + generate_pdf_for_wallets(wallets, 0) |
| 122 | + generate_csv_for_wallets(wallets, 0) |
| 123 | + end |
| 124 | + |
| 125 | + def page_options |
| 126 | + { |
| 127 | + margin: 1.mm, |
| 128 | + page_size: [70.mm, 70.mm] |
| 129 | + } |
| 130 | + end |
| 131 | + |
| 132 | + def host_tmp_path |
| 133 | + "/host_tmp" # A volume defined in the proper docker-compose file |
| 134 | + end |
| 135 | + |
| 136 | + def timestamp |
| 137 | + I18n.l Time.zone.now, format: '%Y%m%d%H%M' |
| 138 | + end |
| 139 | + |
| 140 | + def user_attributes_for(resource) |
| 141 | + resource.user_attributes = { |
| 142 | + email: "#{resource.name.parameterize}@sf.it", |
| 143 | + password: [('a'..'z').to_a.shuffle[0,8].join, rand(100..999)].join |
| 144 | + } |
| 145 | + end |
| 146 | + |
| 147 | + def seller_user_attributes_for(resource, email, password) |
| 148 | + resource.user_attributes = { |
| 149 | + email: email, |
| 150 | + password: password |
| 151 | + } |
| 152 | + end |
| 153 | + |
| 154 | + def transfer_oltrino_to_wallet(wallet, amount) |
| 155 | + client = SocialWallet::Client.new( |
| 156 | + api_endpoint: wallet.endpoint, |
| 157 | + api_key: wallet.api_key |
| 158 | + ) |
| 159 | + resp = client.transactions.new(from_id: '', to_id: wallet.address, amount: amount, tags: ['initial_income', 'new_commoner']) |
| 160 | + wallet.refresh_balance if resp['amount'] == amount |
| 161 | + end |
| 162 | + |
| 163 | + def commoner_wallet_url(commoner, wallet) |
| 164 | + Rails.application.routes.url_helpers.view_commoner_wallet_url( |
| 165 | + host: hostname, |
| 166 | + locale: 'it', |
| 167 | + commoner_id: commoner.id, |
| 168 | + id: wallet.id) |
| 169 | + end |
| 170 | + |
| 171 | + def hostname |
| 172 | + case Rails.env |
| 173 | + when 'production' |
| 174 | + 'https://commonfare.net' |
| 175 | + when 'staging' |
| 176 | + 'https://cf-staging.fbk.eu' |
| 177 | + else |
| 178 | + 'http://localhost:3000' |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + def generate_commoner_name |
| 183 | + name = "SF_#{[('a'..'z').to_a.shuffle[0,5].join, rand(100..999)].join}" |
| 184 | + if Commoner.exists?(["lower(name) = ?", name.downcase]) |
| 185 | + generate_commoner_name |
| 186 | + else |
| 187 | + name |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + def generate_pdf_for_wallets(wallets, amount) |
| 192 | + sorted_ids = wallets.map(&:id).sort |
| 193 | + Prawn::Document.generate("#{host_tmp_path}/#{timestamp}_oltrino_qrcodes_#{amount}sc_#{sorted_ids.first}-#{sorted_ids.last}.pdf", page_options) do |
| 194 | + wallets.each do |wallet| |
| 195 | + # see http://www.qrcode.com/en/about/version.html for versions |
| 196 | + # 7 -> 45x45 modules |
| 197 | + # 8 -> 49x49 modules |
| 198 | + qr = RQRCode::QRCode.new( |
| 199 | + commoner_wallet_url(wallet.walletable, wallet), |
| 200 | + size: 7) |
| 201 | + qr_svg = qr.as_svg(offset: 0, |
| 202 | + color: '000', |
| 203 | + shape_rendering: 'crispEdges', |
| 204 | + module_size: 5.5) # in pixels |
| 205 | + svg qr_svg, position: :center, width: 179 |
| 206 | + move_down 2 |
| 207 | + text "WalletID #{wallet.id}", align: :center |
| 208 | + start_new_page |
| 209 | + end |
| 210 | + end |
| 211 | + end |
| 212 | + |
| 213 | + def generate_csv_for_wallets(wallets, amount) |
| 214 | + sorted_ids = wallets.map(&:id).sort |
| 215 | + output_file = File.join(host_tmp_path, ("#{timestamp}_oltrino_wallet_urls_#{amount}sc_#{sorted_ids.first}-#{sorted_ids.last}.csv")) |
| 216 | + CSV.open(output_file, 'wb') do |csv| |
| 217 | + csv << %w(Wallet_URL Wallet_ID) |
| 218 | + wallets.each do |wallet| |
| 219 | + wallet_url = commoner_wallet_url(wallet.walletable, wallet) |
| 220 | + csv << [wallet_url, wallet.id] |
| 221 | + end |
| 222 | + end |
| 223 | + end |
| 224 | + |
| 225 | +end |
0 commit comments