Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: TT-220 refactoring for projects
  • Loading branch information
Pablo authored and Pablo committed May 17, 2021
commit 58de09b85d71fefbfd53ddabf424da580ddbe38e
10 changes: 4 additions & 6 deletions tests/time_tracker_api/projects/projects_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@patch(
'time_tracker_api.projects.projects_model.ProjectCosmosDBRepository.find_partition_key_value'
)
def test_find_all_v2(
def test_find_all_new_version(
find_partition_key_value_mock,
event_context: EventContext,
project_repository: ProjectCosmosDBRepository,
Expand All @@ -28,13 +28,11 @@ def test_find_all_v2(
project_repository.container = Mock()
project_repository.container.query_items = query_items_mock

result = project_repository.find_all_v2(
event_context,
['id'],
['customer_id']
result = project_repository.find_all(
event_context, ['id'], ['customer_id']
)
find_partition_key_value_mock.assert_called_once()
assert len(result) == 1
project = result[0]
assert isinstance(project, ProjectCosmosDBModel)
assert project.__dict__ == expected_item
assert project.__dict__ == expected_item
30 changes: 15 additions & 15 deletions time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def __init__(self):
mapper=ProjectCosmosDBModel,
)

def find_all_v2(
def find_all(
self,
event_context: EventContext,
project_ids: List[str],
project_ids: List[str] = None,
customer_ids: List[str] = None,
visible_only=True,
mapper: Callable = None,
Expand All @@ -97,7 +97,9 @@ class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
def __init__(self, repository):
CosmosDBDao.__init__(self, repository)

def get_all(self, conditions: dict = None, **kwargs) -> list:
def get_all(
self, conditions: dict = None, project_ids: List = None, **kwargs
) -> list:
"""
Get all the projects an active client has
:param (dict) conditions: Conditions for querying the database
Expand All @@ -110,29 +112,27 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
max_count=kwargs.get('max_count', None)
)

# TODO: evaluate another approach in order that memory filtering will be make in Database instead
customers_id = [
customer.id
for customer in customers
if customer.status == 'active'
]
conditions = conditions if conditions else {}
custom_condition = "c.customer_id IN {}".format(
str(tuple(customers_id))
customers_ids = [v for k, v in conditions.items()]
customers_ids = (
customers_ids + customers_id if project_ids else customers_ids
)

projects = self.repository.find_all(
event_context=event_ctx,
project_ids=project_ids,
customer_ids=customers_ids,
)
# TODO this must be refactored to be used from the utils module ↑
if "custom_sql_conditions" in kwargs:
kwargs["custom_sql_conditions"].append(custom_condition)
else:
kwargs["custom_sql_conditions"] = [custom_condition]
projects = self.repository.find_all(event_ctx, conditions, **kwargs)

add_customer_name_to_projects(projects, customers)
return projects

def get_all_with_id_in_list(self, id_list):
event_ctx = self.create_event_context("read-many")
return self.repository.find_all_v2(event_ctx, id_list)


def create_dao() -> ProjectDao:
repository = ProjectCosmosDBRepository()
Expand Down
6 changes: 4 additions & 2 deletions time_tracker_api/time_entries/time_entries_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ def add_complementary_info(
self, time_entries=None, max_count=None, exist_conditions=False
):
if time_entries:
custom_conditions = create_in_condition(time_entries, "project_id")
project_ids_set = set([x.project_id for x in time_entries])
project_ids = list(project_ids_set)

custom_conditions_activity = create_in_condition(
time_entries, "activity_id"
)

project_dao = projects_model.create_dao()
projects = project_dao.get_all(
custom_sql_conditions=[custom_conditions],
project_ids=project_ids,
visible_only=False,
max_count=max_count,
)
Expand Down