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

Refactor entityParams in Order.Create API so it is easier to understand/modify #18306

Merged

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Sep 1, 2020

Overview

Partial from #18096 - this is a refactor only.

Before

More compact, less easy to understand/modify code.

After

Less compact, clearer code. Allows for easier changes for individual entities (eg. adding participant statuses per #18096).

Technical Details

Comments

@eileenmcnaughton Could you review/merge? I'll rebase #18096 after

@civibot civibot bot added the master label Sep 1, 2020
@civibot
Copy link

civibot bot commented Sep 1, 2020

(Standard links)

@eileenmcnaughton
Copy link
Contributor

@mattwire I did the same refactor myself and came out with a slightly different outcome (mostly switching on $item['entity_table'] since we know what the entity is from the table & reducing the IFs on $entityParams). However, the only thing of significance / that couldn't equally well be done in a future round is that I think

break 2;

is wrong - as that would take us out of both the switch AND the line item iteration loop - meaning that a contribution line item would block a later membership item being created. I guess we want to check that scenario is tested before making this change

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Sep 3, 2020

hmm - it might be OK - actually - perhaps - just looking at the test config we have

   $orderID = $this->callAPISuccess('Order', 'create', [
      'total_amount' => 400,
      'financial_type_id' => 'Member Dues',
      'contact_id' => $this->_contactID,
      'is_test' => 0,
      'payment_instrument_id' => 'Check',
      'receive_date' => '2019-07-25 07:34:23',
      'line_items' => [
        [
          'params' => [
            'contact_id' => $this->ids['contact'][0],
            'membership_type_id' => $this->ids['membership_type'][0],
            'source' => 'Payment',
          ],
          'line_item' => [
            [
              'label' => 'General',
              'qty' => 1,
              'unit_price' => 200,
              'line_total' => 200,
              'financial_type_id' => 1,
              'entity_table' => 'civicrm_membership',
              'price_field_id' => $priceFieldID,
              'price_field_value_id' => $generalPriceFieldValueID,
            ],
          ],
        ],
        [
          'params' => [
            'contact_id' => $this->ids['contact'][1],
            'membership_type_id' => $this->ids['membership_type'][0],
            'source' => 'Payment',
          ],
          'line_item' => [
            [
              'label' => 'General',
              'qty' => 1,
              'unit_price' => 200,
              'line_total' => 200,
              'financial_type_id' => 1,
              'entity_table' => 'civicrm_membership',
              'price_field_id' => $priceFieldID,
              'price_field_value_id' => $generalPriceFieldValueID,
            ],
          ],
        ],
      ],
    ])['id'];

it's unclear whether more than one line could be in line item

I guess what would make it clearer would be something like

if ($item['entity_table'] === 'civicrm_participant') {
  // Do the whole participant create in here - don't try to share code with membership bit.
}

if ($item['entity_table'] === 'civicrm_participant') {
  // Do the whole membership create in here - don't try to share code with participant bit.
}

@eileenmcnaughton
Copy link
Contributor

Hmmm no the second break is definitely out of

 foreach ($params['line_items'] as $lineItems) {
  • so it should be moderately easy to tweak the setup in CRMTraits_Financial_OrderTrait to have an order where the first line item is purely a contribution item

@eileenmcnaughton
Copy link
Contributor

Just adding needs test because we want to ensure that case were the line items is an array with a contribution line as the first line is covered

jitendrapurohit pushed a commit to jitendrapurohit/civicrm-core that referenced this pull request Oct 7, 2020
mattwire pushed a commit to mattwire/civicrm-core that referenced this pull request Oct 8, 2020
@mattwire mattwire force-pushed the refactororderapientityparams branch from 10b40ef to 6573357 Compare October 8, 2020 13:22
@mattwire
Copy link
Contributor Author

mattwire commented Oct 8, 2020

@eileenmcnaughton This now has a test which should improve the test coverage for the Order API - thanks @jitendrapurohit

@mattwire mattwire force-pushed the refactororderapientityparams branch from 6573357 to 07e6da1 Compare October 11, 2020 09:44
mattwire pushed a commit to mattwire/civicrm-core that referenced this pull request Oct 11, 2020
eileenmcnaughton pushed a commit to eileenmcnaughton/civicrm-core that referenced this pull request Oct 16, 2020
@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Oct 16, 2020

@mattwire @jitendrapurohit thanks for the test - it doesn't actually cover the problem in this PR - but I have updated it so that it passes without this PR (locking in correct behaviour) but fails with this PR #18785

As I suspected the in my comments earlier - the break 2; IS causing subsequent line items to be ignored if the first is a contribution - however the test, as it is in this PR ONLY has one line item rather than more than one where the first is contribution only - so it's not testing the impact of that early exit.

@jitendrapurohit
Copy link
Contributor

Thank you for the update @eileenmcnaughton 👍

mattwire added a commit that referenced this pull request Oct 19, 2020
unit test for #18306 - order create api test for contribution
@mattwire mattwire force-pushed the refactororderapientityparams branch from 07e6da1 to bd94f95 Compare October 19, 2020 09:42
@mattwire mattwire force-pushed the refactororderapientityparams branch from bd94f95 to 33f7914 Compare October 26, 2020 21:00
@mattwire
Copy link
Contributor Author

@eileenmcnaughton I've updated the PR and tests are now passing

@eileenmcnaughton eileenmcnaughton merged commit 6ee6581 into civicrm:master Oct 28, 2020
@eileenmcnaughton
Copy link
Contributor

yep that works

@mattwire mattwire deleted the refactororderapientityparams branch October 28, 2020 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants