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
Issue #2763705 by mglaman: Implement a usage API #695
Conversation
f8f3a75
to
6a73de7
Compare
* @covers ::getCouponUsage | ||
* @covers ::addCouponUsage | ||
*/ | ||
public function testMockUsage() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bojanz this method at leasts tests the logic and storage. Order refresh and legitimate implementation will take some time. If you'd like to review the table name, columns, and basic storage logic.
b4009d1
to
a85cbd3
Compare
a85cbd3
to
015f63d
Compare
* @param \Drupal\commerce_order\Entity\OrderInterface $order | ||
* The order. | ||
*/ | ||
public function addPromotionUsage(PromotionInterface $promotion, OrderInterface $order); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not a single addUsage($order, $promotion, $coupon = NULL)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess. Just felt weird since we can get the promotion from the coupon, but a single method for adding works for me
* @return int | ||
* The usage. | ||
*/ | ||
public function getCouponUsage(CouponInterface $coupon); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, getUsage($promotion, $coupon = NULL), and some documentation to clarify?
@@ -24,8 +24,27 @@ public function loadValid(OrderTypeInterface $order_type, StoreInterface $store) | |||
if (empty($result)) { | |||
return []; | |||
} | |||
|
|||
$usages_query = $this->database->select('commerce_promotion_usage', 'cpu'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels wrong to query the DB table outside of the service. We have an API, let's use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API doesn't allow for a bulk look up, and core does this as well within storage to provide a more performant query. Otherwise we'd have to do a SELECT for each promotion, and that's wasteful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it's wasteful, but that means we need a new method on the service then. If we keep it this way people won't be able to easily swap the service to use a different storage, which is something that core encourages with its own examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which reminds me, we should look into the backend_overridable tag for the service.
We need a post_update to drop the current_usage field. |
5f48c6d
to
42af6d7
Compare
*/ | ||
function commerce_promotion_schema() { | ||
$schema['commerce_promotion_usage'] = [ | ||
'description' => 'Stores module data as key/value pairs per user.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Untrue.
'default' => 0, | ||
], | ||
'coupon_id' => [ | ||
'description' => 'Primary key: {commerce_promotion_coupon}.id for coupon.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these are primary keys, right? We might as well drop the phrase.
@@ -84,3 +84,17 @@ function commerce_promotion_post_update_4() { | |||
$promotion->save(); | |||
} | |||
} | |||
|
|||
/** | |||
* Remove promotion current_usage field. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Remove the promotion current_usage field."
People will see the lack of an article and think a Slavic person wrote this :P
public function getUsage(PromotionInterface $promotion, CouponInterface $coupon = NULL); | ||
|
||
/** | ||
* Add a promotion usage record. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Adds a usage record for the given promotion." ?
interface PromotionUsageInterface { | ||
|
||
/** | ||
* Gets the usage for a promotion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Gets the usage for the given promotion."?
* @param \Drupal\commerce_promotion\Entity\PromotionInterface $promotion | ||
* The promotion. | ||
* @param \Drupal\commerce_promotion\Entity\CouponInterface|null $coupon | ||
* The promotion's coupon, optional. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Optional) The used coupon.
$usages_query = $this->database->select('commerce_promotion_usage', 'cpu'); | ||
$usages_query->condition('promotion_id', array_keys($result), 'IN'); | ||
$usages_query->addField('cpu', 'promotion_id'); | ||
$usages_query->addExpression("COUNT(promotion_id)", 'count'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call the PromotionUsage method: getUsageMultiple(array $promotions, array $coupons)
9b8ec8c
to
8820ec5
Compare
Final version committed in 5ab9e14. |
Here's a TDD approach to it. Uncovered a lot of flaws with our processing.