improve support to meili
This commit is contained in:
62
http-api/app/meili.py
Normal file
62
http-api/app/meili.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import re
|
||||
|
||||
OPERATORS = [
|
||||
'!=',
|
||||
'>=',
|
||||
'<=',
|
||||
'=',
|
||||
'>',
|
||||
'<',
|
||||
'TO',
|
||||
'EXISTS',
|
||||
'IN',
|
||||
'NOT IN',
|
||||
]
|
||||
|
||||
|
||||
def remove_quotes(value: str) -> str:
|
||||
if (value.startswith('"') and value.endswith('"')) or (
|
||||
value.startswith("'") and value.endswith("'")
|
||||
):
|
||||
return value[1:-1]
|
||||
return value
|
||||
|
||||
|
||||
def parse_condition(condition: str) -> dict[str, str] | None:
|
||||
for op in OPERATORS:
|
||||
parts = condition.split(op)
|
||||
|
||||
if len(parts) == 2:
|
||||
attr, value = parts
|
||||
attr = attr.strip()
|
||||
value = remove_quotes(value.strip())
|
||||
|
||||
return {
|
||||
'attr': attr,
|
||||
'op': op,
|
||||
'value': value,
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def parse(s: str) -> list[dict]:
|
||||
filter_expr = re.sub(r'\s+', ' ', s).strip()
|
||||
|
||||
if filter_expr == '':
|
||||
return []
|
||||
|
||||
parts = re.split(r'\b(?:AND|OR)\b', filter_expr)
|
||||
conditions = []
|
||||
|
||||
for part in parts:
|
||||
condition = parse_condition(part.strip())
|
||||
|
||||
if condition:
|
||||
conditions.append(condition)
|
||||
|
||||
return conditions
|
||||
|
||||
|
||||
def encode(conditions: list[dict]):
|
||||
return ' AND '.join(f'{c["attr"]} {c["op"]} {c["value"]}' for c in conditions if c)
|
||||
@@ -1,15 +1,14 @@
|
||||
import urllib.parse as parse
|
||||
|
||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
||||
from aws_lambda_powertools.event_handler.exceptions import (
|
||||
NotFoundError,
|
||||
)
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
from meilisearch import Client as Meilisearch
|
||||
|
||||
import meili
|
||||
from boto3clients import dynamodb_client
|
||||
from config import MEILISEARCH_API_KEY, MEILISEARCH_HOST, ORDER_TABLE, USER_TABLE
|
||||
from middlewares import Tenant, TenantMiddleware
|
||||
@@ -35,10 +34,16 @@ def get_orders():
|
||||
sort = event.get_query_string_value('sort', 'create_date:desc')
|
||||
page = int(event.get_query_string_value('page', '1'))
|
||||
hits_per_page = int(event.get_query_string_value('hitsPerPage', '25'))
|
||||
filter_ = parse.unquote(event.get_query_string_value('filter', ''))
|
||||
filter_ = meili.parse(parse.unquote(event.get_query_string_value('filter', '')))
|
||||
|
||||
if tenant.id != '*':
|
||||
filter_ = f'tenant = {tenant.id}'
|
||||
filter_ = [
|
||||
{
|
||||
'attr': 'tenant',
|
||||
'op': '=',
|
||||
'value': tenant.id,
|
||||
},
|
||||
] + filter_
|
||||
|
||||
return meili_client.index(ORDER_TABLE).search(
|
||||
query,
|
||||
@@ -47,7 +52,7 @@ def get_orders():
|
||||
'locales': ['pt'],
|
||||
'page': page,
|
||||
'hitsPerPage': hits_per_page,
|
||||
'filter': filter_,
|
||||
'filter': meili.encode(filter_),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -59,7 +64,11 @@ def get_orders():
|
||||
summary='Get order',
|
||||
)
|
||||
def get_order(id: str):
|
||||
return order_layer.collection.get_item(
|
||||
KeyPair(id, '0'),
|
||||
exc_cls=NotFoundError,
|
||||
return order_layer.collection.get_items(
|
||||
TransactKey(id)
|
||||
+ SortKey('0')
|
||||
+ SortKey('items', path_spec='items')
|
||||
+ SortKey('address')
|
||||
+ SortKey('nfse', path_spec='nfse')
|
||||
+ SortKey('fees', path_spec='fees'),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user