11from dataclasses import dataclass
2+ from azure .cosmos import PartitionKey
23from commons .data_access_layer .cosmos_db import (
34 CosmosDBModel ,
5+ CosmosDBDao ,
6+ CosmosDBRepository ,
7+ )
8+ from time_tracker_api .database import CRUDDao , APICosmosDBDao
9+ from time_tracker_api .customers .customers_model import (
10+ create_dao as customers_create_dao ,
411)
512from time_tracker_api .customers .customers_model import CustomerCosmosDBModel
613
14+ from utils .extend_model import add_customer_name_to_projects
15+
16+
17+ class ProjectDao (CRUDDao ):
18+ pass
19+
20+
21+ container_definition = {
22+ 'id' : 'project' ,
23+ 'partition_key' : PartitionKey (path = '/tenant_id' ),
24+ 'unique_key_policy' : {
25+ 'uniqueKeys' : [{'paths' : ['/name' , '/customer_id' , '/deleted' ]},]
26+ },
27+ }
28+
729
830@dataclass ()
931class ProjectCosmosDBModel (CosmosDBModel ):
@@ -30,3 +52,52 @@ def __repr__(self):
3052
3153 def __str___ (self ):
3254 return "the project \" %s\" " % self .name # pragma: no cover
55+
56+
57+ class ProjectCosmosDBRepository (CosmosDBRepository ):
58+ def __init__ (self ):
59+ CosmosDBRepository .__init__ (
60+ self ,
61+ container_id = container_definition ['id' ],
62+ partition_key_attribute = 'tenant_id' ,
63+ mapper = ProjectCosmosDBModel ,
64+ )
65+
66+
67+ class ProjectCosmosDBDao (APICosmosDBDao , ProjectDao ):
68+ def __init__ (self , repository ):
69+ CosmosDBDao .__init__ (self , repository )
70+
71+ def get_all (self , conditions : dict = None , ** kwargs ) -> list :
72+ """
73+ Get all the projects an active client has
74+ :param (dict) conditions: Conditions for querying the database
75+ :param (dict) kwargs: Pass arguments
76+ :return (list): ProjectCosmosDBModel object list
77+ """
78+ event_ctx = self .create_event_context ("read-many" )
79+ customer_dao = customers_create_dao ()
80+ customers = customer_dao .get_all (
81+ max_count = kwargs .get ('max_count' , None )
82+ )
83+
84+ customers_id = [customer .id for customer in customers ]
85+ conditions = conditions if conditions else {}
86+ custom_condition = "c.customer_id IN {}" .format (
87+ str (tuple (customers_id ))
88+ )
89+ # TODO this must be refactored to be used from the utils module ↑
90+ if "custom_sql_conditions" in kwargs :
91+ kwargs ["custom_sql_conditions" ].append (custom_condition )
92+ else :
93+ kwargs ["custom_sql_conditions" ] = [custom_condition ]
94+ projects = self .repository .find_all (event_ctx , conditions , ** kwargs )
95+
96+ add_customer_name_to_projects (projects , customers )
97+ return projects
98+
99+
100+ def create_dao () -> ProjectDao :
101+ repository = ProjectCosmosDBRepository ()
102+
103+ return ProjectCosmosDBDao (repository )
0 commit comments