Move user and customer updates into a transaction, managed by a module - #501
Conversation
|
We discussed that we're going to need to change this to push out connect customer email changes, but that we won't store the email on our server. |
|
Marked as needs work but still needs code review, too. |
e4758b6 to
2c3bfca
Compare
|
I am marking as low priority at the moment and removing the needs code review for the time being. There is no mechanism right now for a user to update their email through Code Corps, and that's the only change going to Stripe. Once we allow users to change their email – or once we have time to circle back – we can reprioritize this. Let's not let it get out of date. We still need the change mentioned above. |
|
I think we actually need to finish this up. Was wrong. |
3beb43a to
f4e361e
Compare
|
I made some updates to this, so it ought to be ready for review. When a user updates, if the updates contain an e-mail,
Considering the amount of potential overlap with #508, I would prefer to merge this before touching #508 further. |
5a4d168 to
4107099
Compare
| {:error, _failed_operation, _failed_value, _changes_so_far} -> | ||
| {:error, :unhandled} | ||
| other -> | ||
| IO.inspect(other, pretty: true) |
There was a problem hiding this comment.
This needs removed. Is there an other case that needs handled?
|
|
||
| defp do_update(%Changeset{} = changeset) do | ||
| with {:ok, user} <- Repo.update(changeset) do | ||
| {:ok, user, :nothing_to_update, :nothing_to_update} |
There was a problem hiding this comment.
Why not return nil and nil here?
There was a problem hiding this comment.
:nothing_to_update seems overly specific here whereas nil is just as descriptive, IMO.
| |> Repo.update | ||
| |> CodeCorps.Analytics.Segment.track(:updated_profile, conn) | ||
| def handle_update(conn, record, attributes) do | ||
| with {:ok, user, _platform_customer_updates, _connect_customer_updates} <- UserService.update(record, attributes) |
There was a problem hiding this comment.
I would just go unnamed on the nil returns here, straight _. See no harm in that and easier to read.
There was a problem hiding this comment.
It feels like the UserService.update should probably happen in an asynchronous task rather than sync here.
There was a problem hiding this comment.
We can either do it in a transaction, meaning we have to wait for sync, or we ditch the transaction, update the user, respond with that and do the propagation async. I don't think we can do both.
|
|
||
| def update_changeset(struct, params) do | ||
| struct | ||
| |> cast(params, [:email]) |
There was a problem hiding this comment.
Why no validate_required here? as there is with platform?
There was a problem hiding this comment.
That changeset should go away completely, actually. I added it by mistake, before realising there is nothing to update with the local record.
| {:ok, customer} = StripePlatformCustomerService.update(customer, %{email: "mail@mail.com"}) | ||
| assert customer.email == "mail@mail.com" | ||
|
|
||
| # TODO: Figure out testing if stripe API request was made |
There was a problem hiding this comment.
Is this TODO leftover or intentional?
There was a problem hiding this comment.
It's intentional. I'm not sure how to check if a stripe API update was made. Maybe the function should return both the updated local record and the updated stripe record?
I'm starting to think we should avoid "side effects" in our functions as much as we can. I added a response containing updated platform and connect customer records to the user service for basically the same reason.
| Handles CRUD operations for users. | ||
|
|
||
| When operations happen on `CodeCorps.User`, we need to make sure changes | ||
| are propagated to related records, ex., `CodeCorps.StripePlatformCustomer` or |
| """ | ||
|
|
||
| alias CodeCorps.{Repo, StripeConnectCustomer, StripePlatformCustomer, User} | ||
| alias CodeCorps.StripeService.{StripeConnectCustomerService,StripePlatformCustomerService} |
|
@begedin feel free to merge even if there are unresolved questions here, but if so (like the async task), open in another issue and reference this. |
c9d7c6e to
64f568e
Compare
…latform and connect customer updates
64f568e to
631b556
Compare
What's in this PR?
Adds a
UserManagermodule, which, for now, is placed under lib/code_corps/user_manager.ex, but ought to probably moved into a new namespace, together withDonationGoalsmanager.This module, for now, exposes an
updatefunction, which receives a user and a set of attributes.The
updatefunction behaves in the following way:To update the platform customer, I've also implemented
StripeService.StripePlatformCustomer.update/2, which handles updating of both the local and the API record.The scope of the PR was unclear to me, so I implemented the whole process. Based on the issue title alone, the assumption would be we just need to pass in the parameters during customer creation. However, the issue description also mentions keeping it up to date so "we can search by user email".
Questions:
References
Fixes #489