-
-
Notifications
You must be signed in to change notification settings - Fork 825
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
CRM-18082 - Allow case API create to work with custom data #10728
CRM-18082 - Allow case API create to work with custom data #10728
Conversation
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.
@mattwire Left some inline comments. Besides that, it would be great if the PR description had details about the changes you did and their reasons. I tried to compare them to the suggestions made on the ticket's comments, but it seems you did a bit more than what is suggested there. Those details also helper reviewers to better understand what was the intent.
Finally, do you think you can add tests to this?
api/v3/Case.php
Outdated
throw new API_Exception('Invalid case_type_id. No such case type exists.'); | ||
if (empty($params['id'])) { | ||
// Creating a new case, so make sure we have the necessary parameters | ||
civicrm_api3_verify_mandatory($params, 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.
Since PHP 5.3 is still supported, we can't use short array syntax yet
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.
Fixed
api/v3/Case.php
Outdated
throw new API_Exception(ts('You cannot update creator id')); | ||
} | ||
|
||
$mCaseId = $origContactIds = array(); |
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.
What does the m
mean here?
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.
Not my code :-) But agreed, it means merged so I've renamed to $mergedCaseId
api/v3/Case.php
Outdated
'end_date' => CRM_Utils_Array::value('end_date', $params), | ||
'subject' => $params['subject'], | ||
); | ||
// return error if modifying creator id |
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 think the code is quite self-explanatory, so there's no need to this comment
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.
Removed comment
api/v3/Case.php
Outdated
@@ -60,37 +60,67 @@ | |||
* api result array | |||
*/ | |||
function civicrm_api3_case_create($params) { | |||
// Process params |
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.
Instead of adding this comment here, I think it would be better to extract the logic to a function with a descriptive name
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.
Moved to _civicrm_api_case_format_params()
api/v3/Case.php
Outdated
// get original contact id and creator id of case | ||
if (!empty($params['contact_id'])) { | ||
$origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']); | ||
$origContactId = $origContactIds[1]; |
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 1
and not 0
or 2
?
Also, what is the difference between origContactId
and origContactIds
?
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've added a comment, again not my code, but copied from the update function. That function returns a 1-based array, as it's used elsewhere it's not in scope for me to refactor right now! But I've added a FIXME comment there explaining. origContactId
is an integer, origContactIds
an array of contactIds, again copied from update function.
api/v3/Case.php
Outdated
@@ -101,31 +131,46 @@ function civicrm_api3_case_create($params) { | |||
CRM_Case_BAO_CaseContact::create($contactParams); | |||
} | |||
|
|||
if (!isset($params['id'])) { | |||
// Creating a new case |
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.
Is this comment correct? Are we really creating a case here? If not, please use the comment to explain why do we need this
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.
We are, I don't really understand the xml stuff very well, except that cases are defined in xml files, and when creating a new one you have to read the xml file to get case params/setup the case. I've added a comment to that effect - the code in _civicrm_api3_case_create_xmlProcessor is simply extracted code from the original api_v3_case_create function.
api/v3/Case.php
Outdated
return civicrm_api3_create_success($values, $params, 'Case', 'create', $caseBAO); | ||
} | ||
|
||
function _civicrm_api3_case_create_xmlProcessor($params, $caseBAO) { |
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.
What about a docblock for this function?
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.
Done
api/v3/Case.php
Outdated
'caseID' => CRM_Utils_Array::value('id',$params), | ||
'subject' => CRM_Utils_Array::value('subject',$params), | ||
'location' => CRM_Utils_Array::value('location',$params), | ||
'activity_date_time' => CRM_Utils_Array::value('start_date',$params), |
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.
There should be a space between the params of every CRM_Utils_Array::value()
call here
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.
Done
api/v3/Case.php
Outdated
throw new API_Exception('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced'); | ||
} | ||
$origContactId = $params['orig_contact_id']; | ||
} |
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 feel like this logic to get the original contact ID should be extracted to a separate function of even to a BAO method. What do you think?
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.
Agreed. I've added a FIXME comment to that effect
api/v3/Case.php
Outdated
$params['case_type'] = $caseTypes[$params['case_type_id']]; | ||
} | ||
} | ||
elseif (empty($params['case_type'])) { |
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.
Early returns would improve this method's readability and considerably reduce the indentation level in this method
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.
Rearranged a little
Pending unit test and response to @davialexandre (many of your comments are relating to existing code that I didn't modify, but you make some good observations, I'll review and update the PR appropriately). |
Unit tests now added |
@davejenx Could you do a quick review as it's in use on one of your sites :-) |
api/v3/Case.php
Outdated
); | ||
_civicrm_api3_case_format_params($params); | ||
if (array_key_exists('creator_id', $params)) { | ||
throw new API_Exception(ts('You cannot update creator id')); |
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.
Tiny picky point: I believe ts() isn't generally used for API exceptions. Discussed at https://chat.civicrm.org/civicrm/pl/qiotz5x6p7fxdegbiece7xhhmc . ts() isn't used for the other exceptions in this file, apart from an identical instance in civicrm_api3_case_update() which presumably this was copied from.
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.
Thanks, that's now fixed
@mattwire Haven't spotted any other issues - as I understand it, the change largely consists of incorporating the relevant parts of civicrm_api3_case_update() into civicrm_api3_case_create() so that it can serve both purposes, so much of it is existing code. If the existing & new unit tests are all passing, there's nothing else that jumps out at me. |
api/v3/Case.php
Outdated
// Add custom data | ||
if (isset($params['custom'])) { | ||
_civicrm_api3_custom_data_get($values[$caseBAO->id], TRUE, 'case', $caseBAO->id); | ||
} |
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.
This is an extra database hit, which we normally don't do for create operations. There is an api option called something like "force_reload" which takes care of this type of thing.
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.
@colemanw Ok, I've had a good look through the code for various other APIs and I can't find any reference to this. Agreed that it's not ideal calling get again, but not sure what else I can do here to return back the customdata if it was passed in on create.
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 is that necessary?
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 it's not :-) Removed it now, all tests still passing
api/v3/Case.php
Outdated
// FIXME: CRM_Case_BAO_Case::retrieveContactIdsByCaseId returns a 1-based array (1 is the first element) | ||
// It should really return a 0-based array for consistency. | ||
$origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']); | ||
$origContactId = $origContactIds[1]; |
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.
You could use CRM_Utils_Array::first()
so you don't have to worry about how the array is indexed.
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.
Sounds good
@colemanw Think this is now good to merge? |
This PR adds support for custom data for Case.Create API in line with other *.Create API functions. It refactors Case.Create API to support updating as the Case.Update API function is deprecated. This means a single codepath is now used for creation and updating, inline with other parts of CiviCRM API.