Skip to content

Commit

Permalink
feat: add anonymize users task (#338)
Browse files Browse the repository at this point in the history
* Anonimize users

* fix: suggestions
  • Loading branch information
ruioliveira02 authored Aug 27, 2023
1 parent bb36fd3 commit 75f5d2a
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/mix/tasks/anonymize_users.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule Mix.Tasks.Anonymize.Users do
@moduledoc """
Task to anonymize users
"""
use Mix.Task
import Ecto.Query, warn: false

alias Safira.Accounts.{Attendee, User}
alias Safira.Repo

def run(_args) do
Mix.Task.run("app.start")
users = Enum.shuffle(Repo.all(User))

for {user, index} <- Enum.with_index(users) do
anonymize_user(user, index + 1)
end
end

defp anonymize_user(%User{} = user, index) do
attendee =
Attendee
|> where([a], a.user_id == ^user.id)
|> Repo.one()

if not is_nil(attendee) do
attendee
|> Attendee.changeset(%{
name: "Attendee #{index}",
nickname: "attendee#{index}",
avatar: nil,
cv: nil
})
|> Repo.update!()
end

user
|> User.changeset(%{
email: "user#{index}@seium.org",
password: "password1234",
password_confirmation: "password1234"
})
|> Repo.update!()
end
end

0 comments on commit 75f5d2a

Please sign in to comment.