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

Fix missing customer_uuid when creating a note #88

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning].
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [4.1.1] - 2023-12-21
- Fix missing customer_uuid when creating a note from a customer

## [4.1.0] - 2023-12-20
- Support customer notes

Expand Down
2 changes: 1 addition & 1 deletion chartmogul/api/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ def make(self, data, **kwargs):
"create", "post", "/customers/{uuid}/contacts", useCallerClass=True
)
Customer.notes = CustomerNote._method("all", "get", "/customer_notes?customer_uuid={uuid}", useCallerClass=True)
Customer.createNote = CustomerNote._method("create", "post", "/customer_notes", useCallerClass=True)
Customer.createNote = CustomerNote._method("create", "post", "/customer_notes", useCallerClass=True, useUUIDFor="customer_uuid")
6 changes: 5 additions & 1 deletion chartmogul/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _validate_arguments(cls, method, kwargs):
)

@classmethod
def _method(callerClass, method, http_verb, path=None, useCallerClass=False):
def _method(callerClass, method, http_verb, path=None, useCallerClass=False, useUUIDFor=None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _method has 5 arguments (exceeds 4 allowed). Consider refactoring.

Copy link
Contributor Author

@SoeunSona SoeunSona Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to improve this line of code. If there is a better way, please feel free to comment on it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SoeunSona The useUUIDFor being used only for this endpoint seems like a code smell to me. There should be a better way to do this. And perhaps doing what I suggested before (https://github.com/chartmogul/platform/pull/19166#issuecomment-1853150940), regarding supporting both /customers/:customer_uuid/notes and /customer_notes for both listing all notes and creating a note should help this.

The end result would be something like this:

Customer.notes = CustomerNote._method("all", "get", "/customers/{uuid}/notes", useCallerClass=True)
Customer.createNote = CustomerNote._method("create", "post", "/customers/{uuid}/notes", useCallerClass=True)

Just like how I did it for contacts:

Customer.contacts = Contact._method("all", "get", "/customers/{uuid}/contacts", useCallerClass=True)
Customer.createContact = Contact._method(
"create", "post", "/customers/{uuid}/contacts", useCallerClass=True
)

This means we have to modify the core API code to support both endpoints as such. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@briwa Thank you for suggesting this idea. But It seems overkill because we need to change not only the core api but also change all the libraries. And it also requires communication cost with product team to update. I think it would be better to release without this

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this isn't great, and @briwa solution is a good one, but I think this is a compromise we can live with, specially because even if we then change the underlying API we can keep this around for backwards compatibility.

@classmethod
def fc(calleeClass, config, **kwargs):
if config is None or not isinstance(config, Config):
Expand All @@ -208,6 +208,10 @@ def fc(calleeClass, config, **kwargs):
cls._validate_arguments(method, kwargs)

pathTemp = Resource._expandPath(pathTemp, kwargs)

if useUUIDFor is not None and 'data' in kwargs.keys():
kwargs["data"][useUUIDFor] = kwargs["uuid"]

# UUID is always path parameter only.
if "uuid" in kwargs:
del kwargs["uuid"]
Expand Down
2 changes: 1 addition & 1 deletion chartmogul/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.1.0"
__version__ = "4.1.1"
3 changes: 1 addition & 2 deletions test/api/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@
}

createNote = {
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"type": "note",
"text": "This is a note",
"author_email": "john@xample.com"
Expand Down Expand Up @@ -547,7 +546,7 @@ def test_createNote(self, mock_requests):

config = Config("token")
expected = Customer.createNote(
config, data=createNote
config, uuid="cus_00000000-0000-0000-0000-000000000000", data=createNote
).get()

self.assertEqual(mock_requests.call_count, 1, "expected call")
Expand Down
Loading