Automatically Create Audit Log Items Weekly
Walk through the process of adding calls to the Audit Log model to create log entries each week for each employee.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

We have one final feature left, and that's creating an audit log for every text message that is sent out. This is what we're going to do in this video.

Open notification.rake and you'll see that a notification is being sent every Sunday to all the employees. Here, we are storing information about all the employees in a variable, iterating over that variable and sending each one the message. What we can do is create an audit log right before the message is sent. We just need to pass the employee id, as we have already set default values for our status and start date.

# lib/tasks/notification.rake

task sms: :environment do
  if Time.now.sunday?
    employees = Employee.all
    notification_message = "Please log into the overtime management dashboard to request overtime or confirm your hours for last week: https://wlp-overtime.herokuapp.com"

    employees.each do |employee|
      AuditLog.create!(user_id: employee.id)
      SmsTool.send_sms(number: employee.phone, message: notification_message)
    end
  end
end

To test, I'm going to turn off the conditional. Then, run the task.

I got the SMS, and to check the audit logs, open the rails console. When you type Auditlog.last, you can see the last log that was created. So, that's good!

large

Let's also quickly run rspec just to make sure nothing is broken. That's all fine.

Resources