Skip to content

Commit f8a7e88

Browse files
committed
feat: add owner email to time-entries payload
1 parent cd66aeb commit f8a7e88

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
from utils.extend_model import (
2525
add_project_name_to_time_entries,
2626
add_activity_name_to_time_entries,
27-
)
28-
from utils import worked_time
29-
from utils.worked_time import str_to_datetime
30-
from utils.extend_model import (
3127
create_in_condition,
3228
create_custom_query_from_str,
29+
add_user_email_to_time_entries,
3330
)
31+
from utils import worked_time
32+
from utils.worked_time import str_to_datetime
33+
34+
from utils.azure_users import AzureUsers
3435
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
3536
from time_tracker_api.projects import projects_model
3637
from time_tracker_api.database import CRUDDao, APICosmosDBDao
@@ -177,6 +178,8 @@ def find_all(
177178
activity_dao = activities_model.create_dao()
178179
activities = activity_dao.get_all()
179180
add_activity_name_to_time_entries(time_entries, activities)
181+
182+
add_user_email_to_time_entries(time_entries, AzureUsers().users())
180183
return time_entries
181184

182185
def on_create(self, new_item_data: dict, event_context: EventContext):

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@
129129
description='Name of the activity associated with the time-entry',
130130
example=faker.word(['development', 'QA']),
131131
),
132+
'owner_email': fields.String(
133+
required=True,
134+
title="Owner's Email",
135+
max_length=50,
136+
description='Email of the user that owns the time-entry',
137+
example=faker.email(),
138+
),
132139
}
133140
time_entry_response_fields.update(common_fields)
134141

utils/azure_users.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class MSALConfig:
1414
"""
1515

1616

17+
class AzureUser:
18+
def __init__(self, id, display_name, email):
19+
self.id = id
20+
self.display_name = display_name
21+
self.email = email
22+
23+
1724
class AzureUsers:
1825
def __init__(self, config=MSALConfig):
1926
self.client = msal.ConfidentialClientApplication(
@@ -41,7 +48,19 @@ def set_token(self):
4148

4249
def get_user_info_by_id(self, id):
4350
endpoint = f"{self.config.MSAL_ENDPOINT}/users/{id}?api-version=1.6&$select=displayName,otherMails"
44-
print(endpoint)
51+
# print(endpoint)
52+
http_headers = {
53+
'Authorization': f'Bearer {self.access_token}',
54+
'Accept': 'application/json',
55+
'Content-Type': 'application/json',
56+
}
57+
data = requests.get(
58+
endpoint, headers=http_headers, stream=False
59+
).json()
60+
return data
61+
62+
def get_users_info(self):
63+
endpoint = f"{self.config.MSAL_ENDPOINT}/users?api-version=1.6&$select=displayName,otherMails,objectId"
4564
http_headers = {
4665
'Authorization': f'Bearer {self.access_token}',
4766
'Accept': 'application/json',
@@ -51,3 +70,25 @@ def get_user_info_by_id(self, id):
5170
endpoint, headers=http_headers, stream=False
5271
).json()
5372
return data
73+
74+
def users(self):
75+
endpoint = f"{self.config.MSAL_ENDPOINT}/users?api-version=1.6&$select=displayName,otherMails,objectId"
76+
http_headers = {
77+
'Authorization': f'Bearer {self.access_token}',
78+
#'Accept': 'application/json',
79+
#'Content-Type': 'application/json',
80+
}
81+
data = requests.get(
82+
endpoint, headers=http_headers, stream=False
83+
).json()
84+
# print(data)
85+
86+
users = []
87+
for value in data['value']:
88+
user = AzureUser(
89+
id=value['objectId'],
90+
display_name=value['displayName'],
91+
email=value['otherMails'][0],
92+
)
93+
users.append(user)
94+
return users

utils/extend_model.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def add_activity_name_to_time_entries(time_entries, activities):
3838
setattr(time_entry, 'activity_name', activity.name)
3939

4040

41+
def add_user_email_to_time_entries(time_entries, users):
42+
for time_entry in time_entries:
43+
for user in users:
44+
if time_entry.owner_id == user.id:
45+
setattr(time_entry, 'owner_email', user.email)
46+
47+
4148
def create_in_condition(
4249
data_object: list, attr_to_filter: str = "", first_attr: str = "c.id"
4350
):

0 commit comments

Comments
 (0)