Add regex analyser action handler to execute APIv4 actions#527
Draft
Add regex analyser action handler to execute APIv4 actions#527
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds an regex analyser action handler for the action "api4" to execute APIv4 actions. It is inspired by #513.
To use values from the pattern matches, the bank transaction (
btx), the bank account (ba), or the party bank account (party_ba) the Symfony Expression Language is used. So it is not only possible to access a single value, but to do some mathematics and other things. Strings starting with@=(this is the prefix Symfony uses in config files) are interpreted as expressions. I decided to give expressions also a try in theresult_mapin addition to what was discussed in #513. In expressions in the result map theResultobject is available as variableresult. I'm not sure if we need and want to have two approaches. (This is something to be discussed.) With the expression language the expression@=result.first()['some_field'][0] ?? NULLcould be used instead of thefirstfilter.The Symfony Expression Language can be extended with custom functions so we're not limited to the ones available by default. (We could even consider giving third party extensions the possibility to provide custom functions.)
Adapted example from #513:
{ "comment": "Look for previous contribution with matching bank name and amount", "action": "api4", "api4": { "entity": "Contribution", "action": "get", "params": { "limit": 1, "orderBy": { "receive_date": "DESC" }, "where": [ [ "Donor_Information.Bank_Name", "=", "@=purpose" ], [ "total_amount", "=", "@=btx.amount" ] ] }, "result_map": { "previous_contribution_id": "id", "contact_id": "contact_id" "financial_type_id": "@=result.first()['financial_type_id']", } } },Actually
@=result.first()['financial_type_id']is the same as justfinancial_type_idhere with results limited to one in the API call. It's just meant as an example. In\Civi\Banking\Matcher\RegexAnalyser\ActionHandlers\Api4RegexAnalyserActionHandlerTestyou can find expressions with operations (addition, string concatenation).Note: The
.is the object access operator in expressions which prevents using dots in pattern matches. In case there are values with.in the parsed data ofbtx, array access can be used:btx['foo.bar'].I'd like to hear your opinions @ufundo, @jensschuppe.
Another thing I'm currently unsure about: What is the expected behavior when the APIv4 call returns no result, but a result map is defined. The current implementation doesn't set any value at all, though another option would be to set
NULL. In case of expressions they might be evaluated with the empty result object.systopia-reference: 30273