contactorama/app/controllers/birthdays_controller.rb

37 lines
1.3 KiB
Ruby

# -*- coding: utf-8 -*-
class BirthdaysController < ApplicationController
before_filter :authenticate_user!, :except => [:do_mailing]
def index
@birthdays = get_next_weeks_birthdays
end
# curl --verbose --header "Accept: application/html" --header "Content-type: application/html" --request POST --data "" http://contactorama.dev/birthdays/do_mailing
# wget --post-data '' http://contact-o-rama.de/birthdays/do_mailing
# crontab:
# 30 3 * * * wget --post-data '' http://contact-o-rama.de/birthdays/do_mailing
def do_mailing
birthdays = get_next_weeks_birthdays
birthdays.each do |bday|
logger.info "............< do_mailing: " + bday.firstname + " " + bday.lastname + " >.............."
UserMailer.next_weeks_birthday_notification(birthdays).deliver
end
end
private
def get_next_weeks_birthdays
n = Date.today
e = Date.today + 7.days
# two cases here: now + 7days iis still the same month. or it's not.
if n.month == e.month
birthdays = Contact.where("birth_month = ? AND birth_day <= ? AND birth_day >= ?",
e.month, e.day, n.day)
else
birthdays = Contact.where("(birth_month = ? AND birth_day <= ?) OR (birth_month = ? AND birth_day >= ?)",
e.month, e.day, n.month, n.day)
end
birthdays
end
end