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

PT-7293: Add a new Liquid 'where' filter to enable filtering collections #149

Merged
merged 3 commits into from
Dec 6, 2023

Conversation

cgenesoni01
Copy link
Contributor

@cgenesoni01 cgenesoni01 commented Nov 24, 2023

Add a new Liquid 'where' filter to enable filtering collections in notification templates.

This new functionality allow the users filter arrays by conditions.

Example:
We have the following json


{
    "property1" : "value1",
    "users": [
     {
 	    "userId":"1",
 	    "name": "Admin",
 	    "age": 36
     },
     {
 	    "userId":"2",
 	    "name": "Carlos",
 	    "age": 40
     },
     {
 	    "userId":"3",
 	    "name": "Juan",
 	    "age": 20
     }]
 }

For example, if we wanted to filter the 'users' collection using the 'userId' and retrieve the first element from the list.
The sentence to make this is :

{{ var1 = users | where: 'userId' '==' '1' | array.first }} {{ var1.name }}

We filter the user collection based on the 'userId' property. When the 'userId' is 1, we retrieve the first element from the filtered results. Subsequently, we assign this result to a new variable, 'var1'. Following this, we write the user's name into the template.

Description

References

QA-test:

Jira-link:

https://virtocommerce.atlassian.net/browse/PT-7293

Artifact URL:

@CLAassistant
Copy link

CLAassistant commented Nov 24, 2023

CLA assistant check
All committers have signed the CLA.

@OlegoO OlegoO changed the title Add a new Liquid 'where' filter to enable filtering collections in no… PT-7293: Add a new Liquid 'where' filter to enable filtering collections Nov 27, 2023
Copy link
Contributor

@artem-dudarev artem-dudarev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Render_GetCurrentYear test is broken in this PR

{
/// <summary>
/// Filter the elements of an array by a given condition
/// {% assign sorted = pages | where:"propName","==","value" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change sorted to filtered

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -152,6 +153,51 @@ public async Task RenderAsync_ContextHasLayoutId_LayoutRendered()
Assert.Equal("header test_content footer", result);
}

[Fact]
public async Task RenderAsync_ContextHasLayoutId_ArrayContent_LayoutRendered()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to RenderAsync_FilterArray

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

public async Task RenderAsync_ContextHasLayoutId_ArrayContent_LayoutRendered()
{
var layoutId = Guid.NewGuid().ToString();
var content = @"{% capture content %} Order Number: {{customer_order.number }}, Items Catalog Name: {{ var1 = customer_order.items | where: 'CatalogId' '==' '1' | array.first }} {{ var1.name }} {% endcapture %}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant code. (capture, order number) We need to test only filtering.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

};

var result = await _liquidTemplateRenderer.RenderAsync(context);
Assert.Equal("header Order Number: 123456, Items Catalog Name: Item1 footer", result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests for operations other than ==.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, I added a new test using greater than

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fixed, there are more operations

@artem-dudarev artem-dudarev merged commit 1c1adac into VirtoCommerce:dev Dec 6, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants