Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAPT-1863: Rake task to pull message delivery status from Notify API #3321

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/tasks/retrieve_notify_message_status.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
desc "Retrieve recent message delivery status from GOV.UK Notify API"
task :retrieve_notify_message_status, [:number_of_days] => :environment do |t, args|
args.with_defaults(number_of_days: 1)

require "notifications/client"
client = Notifications::Client.new(ENV.fetch("NOTIFY_API_KEY"))

current_message_timestamp = Time.now
oldest_retained_message_timestamp = current_message_timestamp - args.number_of_days.days

messages = []
response = nil

while current_message_timestamp > oldest_retained_message_timestamp
args = {}
args[:older_than] = response.collection.last.id if response.present?

puts "Retrieving messages older than #{current_message_timestamp}"

response = client.get_notifications(args)

messages.push(*response.collection)

current_message_timestamp = response.collection.last.created_at
end

puts "Retrieved #{messages.count} messages"

CSV.open("notifications.csv", "w") do |csv|
csv << ["Email", "Status", "Template ID", "Sent at", "Created at", "Completed at"]

messages.each do |message|
csv << [message.email_address, message.status, message.template["id"], message.sent_at, message.created_at, message.completed_at]
end
end

puts "Written to notifications.csv"
end