Skip to content

Commit 58de09b

Browse files
PabloPablo
authored andcommitted
fix: TT-220 refactoring for projects
1 parent f988a8e commit 58de09b

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

tests/time_tracker_api/projects/projects_model_test.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@patch(
1212
'time_tracker_api.projects.projects_model.ProjectCosmosDBRepository.find_partition_key_value'
1313
)
14-
def test_find_all_v2(
14+
def test_find_all_new_version(
1515
find_partition_key_value_mock,
1616
event_context: EventContext,
1717
project_repository: ProjectCosmosDBRepository,
@@ -28,13 +28,11 @@ def test_find_all_v2(
2828
project_repository.container = Mock()
2929
project_repository.container.query_items = query_items_mock
3030

31-
result = project_repository.find_all_v2(
32-
event_context,
33-
['id'],
34-
['customer_id']
31+
result = project_repository.find_all(
32+
event_context, ['id'], ['customer_id']
3533
)
3634
find_partition_key_value_mock.assert_called_once()
3735
assert len(result) == 1
3836
project = result[0]
3937
assert isinstance(project, ProjectCosmosDBModel)
40-
assert project.__dict__ == expected_item
38+
assert project.__dict__ == expected_item

time_tracker_api/projects/projects_model.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def __init__(self):
6868
mapper=ProjectCosmosDBModel,
6969
)
7070

71-
def find_all_v2(
71+
def find_all(
7272
self,
7373
event_context: EventContext,
74-
project_ids: List[str],
74+
project_ids: List[str] = None,
7575
customer_ids: List[str] = None,
7676
visible_only=True,
7777
mapper: Callable = None,
@@ -97,7 +97,9 @@ class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
9797
def __init__(self, repository):
9898
CosmosDBDao.__init__(self, repository)
9999

100-
def get_all(self, conditions: dict = None, **kwargs) -> list:
100+
def get_all(
101+
self, conditions: dict = None, project_ids: List = None, **kwargs
102+
) -> list:
101103
"""
102104
Get all the projects an active client has
103105
:param (dict) conditions: Conditions for querying the database
@@ -110,29 +112,27 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
110112
max_count=kwargs.get('max_count', None)
111113
)
112114

115+
# TODO: evaluate another approach in order that memory filtering will be make in Database instead
113116
customers_id = [
114117
customer.id
115118
for customer in customers
116119
if customer.status == 'active'
117120
]
118121
conditions = conditions if conditions else {}
119-
custom_condition = "c.customer_id IN {}".format(
120-
str(tuple(customers_id))
122+
customers_ids = [v for k, v in conditions.items()]
123+
customers_ids = (
124+
customers_ids + customers_id if project_ids else customers_ids
125+
)
126+
127+
projects = self.repository.find_all(
128+
event_context=event_ctx,
129+
project_ids=project_ids,
130+
customer_ids=customers_ids,
121131
)
122-
# TODO this must be refactored to be used from the utils module ↑
123-
if "custom_sql_conditions" in kwargs:
124-
kwargs["custom_sql_conditions"].append(custom_condition)
125-
else:
126-
kwargs["custom_sql_conditions"] = [custom_condition]
127-
projects = self.repository.find_all(event_ctx, conditions, **kwargs)
128132

129133
add_customer_name_to_projects(projects, customers)
130134
return projects
131135

132-
def get_all_with_id_in_list(self, id_list):
133-
event_ctx = self.create_event_context("read-many")
134-
return self.repository.find_all_v2(event_ctx, id_list)
135-
136136

137137
def create_dao() -> ProjectDao:
138138
repository = ProjectCosmosDBRepository()

time_tracker_api/time_entries/time_entries_repository.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ def add_complementary_info(
146146
self, time_entries=None, max_count=None, exist_conditions=False
147147
):
148148
if time_entries:
149-
custom_conditions = create_in_condition(time_entries, "project_id")
149+
project_ids_set = set([x.project_id for x in time_entries])
150+
project_ids = list(project_ids_set)
151+
150152
custom_conditions_activity = create_in_condition(
151153
time_entries, "activity_id"
152154
)
153155

154156
project_dao = projects_model.create_dao()
155157
projects = project_dao.get_all(
156-
custom_sql_conditions=[custom_conditions],
158+
project_ids=project_ids,
157159
visible_only=False,
158160
max_count=max_count,
159161
)

0 commit comments

Comments
 (0)