-
-
Notifications
You must be signed in to change notification settings - Fork 813
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
dev/core#5308 : APIv4 - Add Order.validate #30716
base: master
Are you sure you want to change the base?
Conversation
🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷 Introduction for new contributors...
Quick links for reviewers...
|
No issue was found matching the number given in the pull request title. Please check the issue number. |
2afc151
to
cb0dd66
Compare
cb0dd66
to
0c8b80c
Compare
@monishdeb I like the look of this - I think we want to add a unit test to get it merged - perhaps the easiest thing to cover in a unit test would be ensuring (based on price_field_value_id' => $this->ids['PriceFieldValue']['membershprice_field_value_id' => $this->ids['PriceFieldValue']['membership_first'],ip_first'],
|
@monishdeb I took another look at this & I think there is a lot to like here :-) I feel like in terms of the order of how we procede through this project getting this merged, with some tests, will help us structure the rest of the work I want to kick the tires a bit on how it works in practice. Some thoughts
Given that I wonder
How do we accomodate both of these in the way we return the results? Some other thoughts
|
@eileenmcnaughton I forgot to mention the other PR which is on implementing Membership.validate() APIv4 #30710. To answer each question:
Extending pseudocode in your previous comment there should be separate setter functions for setting contribution values and line-item values. And the line-item values should be pass through a formatter function before getting validated by respective entity.validate. To summarize : namespace \Civi\Api4\Order;
class validate {
public function _run(\Civi\Api4\Generic\Result $result) {
// extract and format the contribution parameters before validating the parameters
$contributionValues = $this->format($this->getContributionValues(...), 'Contribution');
$errors['contributions'] = Contribution.validate($contributionValues);
foreach ($this->getLineItems() as $key => $lineItem) {
$entity = getEntityFromEntityTable($lineItem['entity_table']);
// $entity could be in Contribution, Event or Membership
$errors['lineitems'][$key] = $entity.validate($this->format($lineItem, $entity));
}
$result[] = $errors;
}
}
So Order.validate result in case of errors could something like: return [
[
[
'record' => 'contribution values',
'fields' => [
'contact_id',
'receive_date',
],
'name' => 'mandatory_missing',
'message' => 'Mandatory values missing from Api4 Contribution::validate: contact_id, receive_date',
],
[
'lineitems' => [
[
'record' => $lineItemKey,
'fields' => [
'status_id',
],
'name' => 'empty_status_id',
'message' => 'There is no valid Membership Status available for selected membership dates.',
],
...
]
];
Isn't it apt to write the unit tests in this PR #30710 ? To use Membership.validate as in: After #30710 got merged we can work on the Order.validate signature and add a unit tests to validate Order's (contribution) parameters and it's entity's parameter (say Membership using Membership.validate as per the pseudocode mentioned above) |
protected function onValidateValues(ValidateValuesEvent $e) { | ||
foreach ($e->records as $recordKey => $record) { | ||
$unmatched = $this->checkRequiredFields($record); | ||
if ($unmatched) { | ||
$e->addError($recordKey, $unmatched, 'mandatory_missing', "Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched)); | ||
} | ||
} | ||
} |
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.
How about making this into a subscriber to civi.api4.validate
rather than calling directly? If it has a service name and uses IsActiveTrait
it could even be disabled by custom implementations of civi.api4.validate
and would serve as a good in-core example of how to implement your own.
Overview
Add (skeletal) Order.validate APIv4 action. This adds the generic validate action that can be inherited by all entities for validating the parameters of an entity before doing CRUD operation.
Ref - https://lab.civicrm.org/dev/core/-/issues/5308
Comments
ping @JoeMurray @eileenmcnaughton @colemanw @jensschuppe @seamuslee001