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

👷 Refactors existing Square logic to a new SquareOrder service class #1909

Merged
merged 30 commits into from
Nov 17, 2023

Conversation

rosschapman
Copy link
Contributor

@rosschapman rosschapman commented Oct 26, 2023

Issue

Goals

  1. Add additional test coverage for a Guest checkout

  2. The new class should expose a single public method for executing the workflow to create an order in square. For example, from inside stripe_events_controller.rb or elsewhere

    square_order = SquareOrder.new(order, ???)
    square_order.send_to_square_seller_dashboard

Notes

  • 🙋 What initial data does a SquareOrder instance need to do it's work?
    • order, marketplace, ordered_products
    • just an order should suffice to query all necessary data

@rosschapman rosschapman changed the title Checkpoint Refactors Square logic from StripeEventsController to a new SquareOrder service class Oct 26, 2023
@rosschapman rosschapman changed the title Refactors Square logic from StripeEventsController to a new SquareOrder service class 👷 Refactors Square logic from StripeEventsController to a new SquareOrder service class Oct 26, 2023
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Relocates all Square related behavior to the new Marketplace::SquareOrder class.

Copy link
Contributor Author

@rosschapman rosschapman Oct 31, 2023

Choose a reason for hiding this comment

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

This sketches the use of live sandbox credentials for e2e testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sketches of unit tests for complex internal methods of the new SquareOrder service class. I'm thinking about this class in a way similar to an external library, therefore it feels justified to test complex private methods that are critical to the correct transmission of data.

Copy link
Member

@zspencer zspencer left a comment

Choose a reason for hiding this comment

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

I like where this is headed! Just not approving because it's still in draft.

Can't wait to see the tests!

private def create_square_order_payment
square_create_payment_body = prepare_square_create_order_payment_body

marketplace.square_client.payments.create_payment(body: square_create_payment_body)
Copy link
Member

Choose a reason for hiding this comment

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

Same here, just for shits and giggles.

@rosschapman rosschapman changed the title 👷 Refactors Square logic from StripeEventsController to a new SquareOrder service class 👷 Refactors existing Square logic to a new SquareOrder service class Oct 31, 2023
@rosschapman rosschapman marked this pull request as ready for review November 9, 2023 19:25
Copy link
Member

@zspencer zspencer left a comment

Choose a reason for hiding this comment

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

I've added the credentials for a Zinc Collective LLC Square Account and read through the code.

I think this is a good enough step forward; and left some stylistic feedback around:

  • Using delegate and attr_accessor to reduce the amount of setup on class instantiation
  • Including more context when sending information to the Square POS
  • Storing significant events (Square Order, etc) in the database so that they are visible to the Marketplace Vendor and Distributor; rather than just developers.

# receiving account which are requirements for the order to show up in the
# Seller Dashboard.
def send_order
Rails.logger.info("Start creating Square order")
Copy link
Member

Choose a reason for hiding this comment

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

Loggers are great! But what I really want are events attached to the Order so that we can surface the information to the folks at Piikup:

order.events.create(description: "Payment Received")

Copy link
Contributor Author

@rosschapman rosschapman Nov 13, 2023

Choose a reason for hiding this comment

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

Ah ok. A blend of misinterpreting your guidance and forgetting we had an event system implemented. Let's do it. Although I'm interested in keeping the logs around for our own benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zspencer

What's the thinking around what strings go into the public.marketplace_events.description column since it's a "free" text type? Understandably with a small org code review serves as a guardrail to maintain consistent event descriptions/actions. But would the list of accepted descriptions strings be something we would consider declaring in some kind of enum somewhere so developers could easily consult an existing set?

I already notice we use a typical pattern of Title Case, past tense, and Noun + Verb. What about adding a small style guide comment to the Event model?

Copy link
Member

Choose a reason for hiding this comment

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

I think a style guide is probably better than an enum; but if you want to gather the usages I wouldn't be opposed.

end
end

context "when Square notifications are enabled" do
let(:order) { order_for_square }
let(:marketplace) { create_marketplace_with_square }
Copy link
Member

Choose a reason for hiding this comment

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

Rather than a unique way to create data, it may be better to use FactoryBot's trait system and create(:marketplace, :with_square)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will follow up in #1936

@@ -65,6 +65,14 @@
with_delivery_areas
with_notification_methods
end

trait :with_square do
Copy link
Member

Choose a reason for hiding this comment

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

Is there supposed to be an Application ID? I'm adding secrets to Github and I've added:

  1. MARKETPLACE_VENDOR_SQUARE_ACCESS_TOKEN
  2. MARKETPLACE_VENDOR_SQUARE_LOCATION
  3. SQUARE_ENV
  4. MARKETPLACE_DISTRIBUTOR_APPLICATION_ID

Copy link
Member

Choose a reason for hiding this comment

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

I think the application id is only important if we make it an OAUTH App; which I would love for us to do at some point (so that someone can just add "Delivery by Piikup" to their Square account and magic-pony complete!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not necessary for instantiating a Square Client class.

if marketplace.square_order_notifications_enabled?
order.send_to_square_seller_dashboard
end
marketplace.square_connection&.send_order
Copy link
Member

Choose a reason for hiding this comment

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

Very tiny!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is. Although I'd still prefer a more object-oriented, explicit exposure and/or handling of nil cases. 😅

@rosschapman
Copy link
Contributor Author

@zspencer I'm going to follow up with a separate PR for the tests to reduce noise in this PR. See #1936. Therefore consider what's already here ready for any final review. I suspect it may not be perfect so we can also work out some details on the next PR if required.

@rosschapman rosschapman requested a review from zspencer November 13, 2023 21:08
Copy link
Member

@zspencer zspencer left a comment

Choose a reason for hiding this comment

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

Once you get the tests passing let's shipit!

@rosschapman rosschapman merged commit f728ea5 into main Nov 17, 2023
@rosschapman rosschapman deleted the marketplace-square-order-model branch November 17, 2023 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants