module Jobs module Cron module WithdrawFiat @page_now = 1 @per_page = 100 @tdata_maps = {} @today = Date.today @persian_today = @today.to_pdate def self.vandar @vandar ||= VandarService.new end def self.process transaction_map return unless @tdata_maps.present? withdraws = Withdraw.where(aasm_state: 'confirming', created_at: (@today - 5).beginning_of_day..@today.end_of_day) withdraws.find_each(batch_size: 500) do |withdraw| withdraw_status = find_vandar_status(withdraw.txid, @tdata_maps) withdraw.success! if withdraw_status.present? && withdraw_status.to_i.in?(VandarService::SUCCESS_WITHDRAW) withdraw.fail! if withdraw_status.present? && withdraw_status.to_i.in?(VandarService::UNSUCCESS_WITHDRAW) end sleep 43200 end def self.find_vandar_status(txid, vhash) txid = txid.to_i return vhash.dig(txid) if vhash.dig(txid).present? @page_now += 1 # all page seen return if @page_now > @last_page transaction_map find_vandar_status(txid, @tdata_maps) end def self.transaction_map transactions = vandar.list_transactions(statusKind: 'settlements', per_page: @per_page, page: @page_now, fromDate: (@persian_today - 5).strftime('%Y%m%d'), toDate: @persian_today.strftime('%Y%m%d')) return unless transactions.dig('status').positive? tdata = transactions&.dig('data') @last_page ||= tdata.dig('last_page') @total ||= tdata.dig('total') return unless tdata.dig('data').present? # @tdata_maps remains old data tdata.dig('data').each_with_object(@tdata_maps) do |item, hash| hash[item.dig('id').to_i] = item.dig('status') end end end end end