I have a Coupon class and I want my app to check and see how many counts are left on the coupon and if the date for the coupon has expired. I have the following method in my class to check both of these.
Coupon class
def self.get(code)
where(
:code => (normalize_code(code)),
:$and => [
{
:$or => [
{ :coupon_count.gte => 1 },
{ :coupon_count => nil }
]
}, {
:$or => [
{ :expires_at.gt => Time.now.utc },
{ :expires_at => nil }
]
}
]
).first
end
This works fine in development when I enter a coupon. But in production it does not work. I use my MongoDB shell to create a coupon as follows.
db.Coupon.insert({code:'#COUPONNAME',discount_percent: 10, expires_at: new ISODate("2016-05-18"), coupon_count: 10, "description": '1st cold visit sign-up'})
It seems that the problem is when the Coupon checks the expires_at date. In development it finds the coupon and works but in production it keeps not finding the coupon. Just for good measure here is my controller method for this.
EDIT I thought the issue was with the date but if I remove the date query it still does not work in production. I am confused why this wont work in production. It is using MongoDB 3.0.10 and mongoid 5.1.0 gem
charges_controller
@code = params[:couponCode]
if !@code.blank?
@coupon = Coupon.get(@code)
if @coupon.nil?
flash[:error] = 'Coupon code is not valid or expired.'
redirect_to new_managers_charge_path(id: @reportapproval.id)
return
elsif @coupon.discount_percent == 100
@reportapproval.report_paid = true
@reportapproval.free_coupon_used = true
@reportapproval.save!
@coupon.coupon_count = @coupon.coupon_count - 1
@coupon.save!
redirect_to managers_dashboard_path, :notice => "You have successfully requested a pre-paid report from #{@reportapproval.tenant_last_name} with a 'No-Pay' intro coupon."
return
else
@final_amount = @coupon.apply_discount(@amount.to_i)
@discount_amount = (@amount.to_i - @final_amount.to_i)
end