简体   繁体   English

如何在Rails出价中实施卖方可以在24小时内取消或接受或交易被取消的出价?

[英]How can I implement in Rails bid offers that sellers can cancel or accept within 24hrs or transaction is cancelled?

I am working on a marketplace in rails that allows buyers to make an offer to the seller as a bid for a listing. 我正在一个铁路市场上工作,该市场允许买方向卖方提出要约以竞标上市。 The seller can cancel or accept the bid but only has a 24 hr window to do this or the bid expires and the transaction is cancelled. 卖方可以取消或接受出价,但只有24小时的时间可以这样做,否则出价将过期并且交易将被取消。 A user can be both a buyer or a seller. 用户可以是买家或卖家。

I have an Order model that represents each transaction. 我有一个代表每个交易的订单模型。

Class Order < ActiveRecord::Base
    belongs_to :seller, class_name: "User"
    belongs_to :buyer, class_name: "User"
    belongs_to :listing
end

On my reservation controller, Class OrdersController < Application Controller before_action :authenticate_user! 在我的预订控制器上,类OrdersController <应用程序控制器before_action:authenticate_user!

def create 
    @order = Order.new(order_params)
    @seller = @order.listing.user
    @order.buyer_id = current_user.id
    @order.seller_id = @seller.id
    @bidding_fee = @order.bidding_fee
end

I want to differentiate between a buyer cancel and seller cancel because if a seller cancels a bid for a listing the buyer pays no bidding fee but if the buyer cancels the order he made he is charged a bidding fee. 我想区分买方取消和卖方取消,因为如果卖方取消了对清单的投标,则买方不支付任何投标费用,但是如果买方取消了他所作的定单,则要向他收取投标费用。 How can the transactions for bids be implemented. 如何执行投标交易。 The hardest part is figuring out creating a timer in rails that will show the seller how much time is left eg 4 hrs 12 min before the bid expires and cancelling the bid after 24 hrs if there's no response especially keeping in mind different client side time zones that may be hours apart from the created_at time of the order. 最难的部分是弄清楚在rails中创建一个计时器,该计时器将向卖方显示还剩多少时间,例如,在出价到期前12分4小时,如果没有响应,则在24小时后取消出价,尤其要牢记不同的客户端时区与订单的created_at时间可能相隔数小时。 I also need to show order status as pending, canceled or accepted which rules out boolean functionality because that creates three possible statuses. 我还需要将订单状态显示为待处理,已取消或已接受,这排除了布尔功能,因为这会创建三种可能的状态。 Any kind of help in any of these problems would be greatly appreciated. 对于这些问题中的任何帮助,将不胜感激。

You might have noticed that in bids table (I am assuming the table name) there is a field called created_at . 您可能已经注意到,在bids表中(我假设是表名),有一个名为created_at You can use this as the start-time indicator. 您可以将其用作start-time指示器。

In bids page where you show all the bids to seller . 在“ bids页面中,您可以向seller显示所有出价。 Either you can 你可以

  • only show the valid bids ( bids.where('bids.created_at > ?', 24.hours.ago) ) 仅显示有效出价( bids.where('bids.created_at > ?', 24.hours.ago)
  • Or, show all the bids but disable the bids created before 24 Hours 或者,显示所有bids但禁用24 Hours之前创建的出价

Better way would be keeping track of the status of the bid with status attribute. 更好的方法是使用status属性跟踪出价的status To invalidate a bid after 24Hours you can use background processors like sidekiq , resque . 要在24小时后使bid无效,您可以使用sidekiqresque等后台处理器。

For example: With Sidekiq 例如:使用Sidekiq

BidInvalidator.perform_in(24.hours, 'mike', 1)
BidInvalidator.perform_at(24.hours.from_now, 'mike', 1)

Update 更新

Do you know how to check the time on the client side based on the timezone of the created_at timestamp on the database if you take into account some users may make an offer before the created_at time if they live 6hrs behind in Alaska? 您是否知道如果考虑到某些用户在阿拉斯加的时间落后6小时,可能会根据数据库上created_at时间戳的时区在客户端上检查时间,如果某些用户可能在created_at时间之前提出要约?

Well, ActiveRecord stores all date-time in UTC time so that developers can converted it to any timezone you like. 好吧,ActiveRecord将所有日期时间存储为UTC时间,以便开发人员可以将其转换为您喜欢的任何时区。

You need to set TimeXone according to user's timezone. 您需要根据用户的时区设置TimeXone You can store timezone offset in browser cookie and get the offset to calculate the timezone in Server side. 您可以将timezone offset存储在浏览器Cookie中,并获取offset以在服务器端计算timezone

$(document).ready(function(){
  if(!($.cookie('time_zone'))) {
    current_time = new Date();
    expired_after_days = 365;
    $.cookie('time_zone', current_time.getTimezoneOffset(), { path: '/', expires: expired_after_days } );
    // Here expired_after_days represents the number of days the cookies will store the info
  }
})

in application_controller you can application_controller您可以

  # setting the time zone with the current time zone offset
  before_filter :set_time_zone
  def set_time_zone
    time_zone_offset = cookies[:time_zone].to_i
    Time.zone = ActiveSupport::TimeZone[-time_zone_offset.minutes]
  end

Note : Time.zone is thread safe. 注意Time.zone是线程安全的。

Now you need to use DateTime.current in place of DateTime.now 现在您需要使用DateTime.current代替DateTime.now

Does it provide functionality to create a worker that checks the status attribute a little more frequently like every ten minutes.If a bid is cancelled Immediately I don't want to have to wait 24hrs before informing a buyer the bid is cancelled 它是否提供创建工作人员的功能,例如每十分钟检查一次状态属性。如果取消了投标,我不想立即等待24小时通知买方取消投标

Well to execute tasks in specific point of time you can create cronjobs using Whenever gem 要在特定时间执行任务,您可以使用Whenever gem创建cronjobs

You do not really need a timer. 您实际上不需要计时器。 You just need the time until the bid is valid. 您只需要时间直到出价有效。 I guess you will have some kind of Bit model. 我想您将拥有某种Bit模型。 When a bit is created by a buyer store when the bid expires in a expires_at column: 当出价在expires_at列中过期时,由买方商店创建了一点:

class Bid < ActiveRecord::Base
  before_create :set_expired_at

  private
  def set_expired_at
    self.expired_at = Time.current + 24.hour
  end
end

This allows you to add an expired? 这样可以添加expired? method to the same class which you could use in the view to decide if you want to still list an offer: 您可以在视图中使用该方法确定是否仍要列出商品的同一类的方法:

def expired?
  expired_at < Time.current
end

Or you could add a scope to only load unexpired bits: 或者,您可以添加范围以仅加载未到期的位:

scope :unexpired, -> { where('expired_at < ?', Time.current) }

If an offer is pending or was excepted could be implemented as a simple accepted boolean in the database. 如果要约待定或被排除,则可以在数据库中将其实现为简单的accepted布尔值。 Which lead to the following status method: 导致以下status方法:

def status
  if accepted?
    'accepted'
  else
    expired? ? 'expired' : 'pending'
  end
end

Since all this methods a depending on the current time, there is no need for an external timer, a cron job or a background job. 由于所有这些方法都取决于当前时间,因此不需要外部计时器,cron作业或后台作业。 The model it self is always able to return the current state. 自身的模型始终能够返回当前状态。

You could use delayed_job , sidekiq or resque or similar tools to schedule your function to be called as soon as the bid is created. 你可以使用delayed_jobsidekiqresque或类似的工具来安排你的函数尽快创建投标调用。 And when 24 hour is up, when the function get's called you can check there if it was accepted or not. 当24小时启动时,调用get函数时,您可以在那里检查该函数是否被接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM