-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add anonymize users task (#338)
* Anonimize users * fix: suggestions
- Loading branch information
1 parent
bb36fd3
commit 75f5d2a
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |