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

Upsert (Update or Insert) Person Record #53

Closed
1 task done
nelsonic opened this issue Apr 27, 2020 · 2 comments
Closed
1 task done

Upsert (Update or Insert) Person Record #53

nelsonic opened this issue Apr 27, 2020 · 2 comments
Assignees
Labels
enhancement New feature or enhancement of existing functionality starter A beginner-friendly issue that is a good starting point for a new person T1h Time Estimate 1 Hour technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@nelsonic
Copy link
Member

nelsonic commented Apr 27, 2020

At present we are inserting an initial person into people in seeds.exs based on the ADMIN_EMAIL environment variable. This person is the (first) "admin" of the auth app.
They will have full control over the auth app. The issue is that we don't want to hard-code too much data into the seed.exs and we don't want to have 5 environment variables for defining the Admin person.
So when the Admin logs into the App for the first time, they see this:
image

When it should show this:
image

We need to update the person record for the Admin when they successfully login with their Google Account.

With that in mind we need an upsert_person/1 function.
This will be useful for anyone who updates their Google/GitHub profile e.g: their profile picture and wants that change to flow through to their @dwyl profile.

# Todo

  • Create an upsert_person/1 function that accepts a person record and either creates a new record if one does not already exist or updates the existing record with the new values.
@nelsonic nelsonic added enhancement New feature or enhancement of existing functionality T1h Time Estimate 1 Hour starter A beginner-friendly issue that is a good starting point for a new person technical A technical issue that requires understanding of the code, infrastructure or dependencies labels Apr 27, 2020
@nelsonic nelsonic self-assigned this Apr 27, 2020
@nelsonic
Copy link
Member Author

nelsonic commented May 4, 2020

This is what I've got:

  def upsert_person(person) do
    case get_person_by_email(person.email) do
      nil ->
        create_person(person)

      ep -> # existing person
        merged = Map.merge(AuthPlug.Helpers.strip_struct_metadata(ep), person)
        {:ok, person} = changeset(%Person{id: ep.id}, merged)
        |> Repo.update()

        person
    end
  end

Does it seem reasonable to you?

@nelsonic
Copy link
Member Author

nelsonic commented May 5, 2020

The upsert_person/1 function works to update the "Admin" profile on first login:
image

This is included/used in #43

@nelsonic nelsonic closed this as completed May 5, 2020
@nelsonic nelsonic mentioned this issue May 5, 2020
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement of existing functionality starter A beginner-friendly issue that is a good starting point for a new person T1h Time Estimate 1 Hour technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

1 participant