I have a setup where I would like to send automatic reminders for users if they haven't made a monthly rental payment. The landlord has the opinion to define the day of the month on which the tenant should make the payment(due_date
). What I would like to achieve is that if the payment hasn't been made in 2 days since the due_date
the tenant would be sent an email. Currently, I have the following task for this.
namespace :rent_due_reminder do
desc "Sends monthly reminders to tenants who haven't paid their rent."
task :send => :environment do
Offer.all.each do |offer|
if offer.status == "accepted" && offer.due_date.present?
landlord = User.find_by(id: offer.landlord_id)
tenant = User.find_by(id: offer.user_id)
stripe_account = Stripe::Account.retrieve(landlord.merchant_id)
payments = Stripe::Charge.list(
{
limit: 100,
expand: ['data.source_transfer.source_transaction.dispute', 'data.application_fee'],
source: {object: 'all'}
},
{ stripe_account: landlord.merchant_id }
)
due_date = "#{Date.today.year}-#{Date.today.month}-#{offer.due_date}".to_datetime
last_payment_date = DateTime.strptime(payments.data.last.created.to_s,'%s')
if (SOME CONDITION)
OfferMailer.with(offer: offer, tenant: tenant, landlord: landlord).rent_due.deliver
end
end
end
end
end
So what I'm doing is that I'm picking the last payment and getting the date of that and this is what I should use to condition the mailer. Initially, I thought that I could just look and see whether last_payment_date < due_date + 2
, but this didn't seem to work. I might be getting confused about this since if I have multiple landlords and some of them have their due date for example 30th of every month and some of them have 1st of every month, should I still be running the task monthly or how should I set this up? Apologies for the reasonably bad explanation, I myself am getting very confused with this also.