Skip to content

Commit

Permalink
Add a function for deleting unconfirmed people
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed May 23, 2024
1 parent e7918a9 commit 031cd6f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/changelog/oban_workers/bouncer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ defmodule Changelog.ObanWorkers.Bouncer do
This module defines the Oban worker for getting hard bounces/spam from CM and
deleting the associated people records from the system
"""
require Logger

alias Changelog.{EventLog, ListKit, Person, Repo}
alias Changelog.{EventLog, ListKit, Newsletters, Person, Repo}
alias ChangelogWeb.PersonView
alias Craisin.Client

use Oban.Worker, queue: :scheduled
Expand Down Expand Up @@ -43,6 +43,28 @@ defmodule Changelog.ObanWorkers.Bouncer do
log("Deleted #{deleted_count} people from #{length(spam)} emails marked as spam.")
end

def delete_unconfirmed do
older_than = Timex.shift(Timex.today(), weeks: -1)
people = Person.older_than(older_than) |> Person.needs_confirmation() |> Repo.all()

results = Enum.map(people, fn person ->
person = Person.preload_subscriptions(person)

if PersonView.is_subscribed(person, Newsletters.nightly()) do
false
else
log("Deleted #{person.id} #{person.name} (#{person.email})")
Repo.delete!(person)
true
end
end)

true_count = results |> Enum.filter(&(&1)) |> length()
false_count = results |> Enum.filter(&(!&1)) |> length()

log("Deleted #{true_count} unconfirmed people older than #{older_than}. #{false_count} skipped.")
end

defp log(message), do: EventLog.insert(message, "Bouncer")

defp is_hard_bounce(bounce) do
Expand Down

0 comments on commit 031cd6f

Please sign in to comment.