Update routes/logs.py
Browse files- routes/logs.py +55 -32
routes/logs.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import os
|
2 |
import logging
|
3 |
import aiohttp
|
|
|
|
|
4 |
from fastapi import APIRouter, HTTPException, Query, Header
|
5 |
from typing import Dict, Any
|
6 |
|
@@ -125,6 +127,31 @@ async def get_logs(
|
|
125 |
else:
|
126 |
logger.warning("⚠️ Erro ao buscar dados dos usuários")
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
# Função para gerar mensagem descritiva
|
129 |
def generate_message(log, user_info):
|
130 |
action = log.get("action")
|
@@ -143,7 +170,7 @@ async def get_logs(
|
|
143 |
except Exception:
|
144 |
new_data = {}
|
145 |
|
146 |
-
email = user_info.get("email", "
|
147 |
target_email = None
|
148 |
|
149 |
if reference == "User" and new_data:
|
@@ -151,50 +178,46 @@ async def get_logs(
|
|
151 |
elif reference == "Approval" and old_data:
|
152 |
target_email = old_data.get("name")
|
153 |
elif reference == "Collaboration":
|
154 |
-
target_email = (
|
155 |
-
new_data.get("email") or old_data.get("email")
|
156 |
-
)
|
157 |
elif reference == "Onboarding":
|
158 |
target_email = None
|
159 |
|
160 |
if action == "update" and reference == "User":
|
161 |
-
return f"{email}
|
162 |
elif action == "approve" and reference == "Approval":
|
163 |
-
return f"{email}
|
164 |
elif action == "deny" and reference == "Approval":
|
165 |
-
return f"{email}
|
166 |
elif action == "update" and reference == "Onboarding":
|
167 |
-
return f"{email}
|
168 |
elif action == "add" and reference == "Onboarding":
|
169 |
-
return f"{email}
|
170 |
elif action == "delete" and reference == "Onboarding":
|
171 |
-
return f"{email}
|
172 |
elif action == "add" and reference == "Collaboration":
|
173 |
-
return f"{email}
|
174 |
elif action == "delete" and reference == "Collaboration":
|
175 |
-
return f"{email}
|
176 |
elif action == "update" and reference == "Collaboration":
|
177 |
-
return f"{email}
|
178 |
|
179 |
-
return f"{email}
|
180 |
-
|
181 |
-
enriched_logs
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
"
|
186 |
-
"
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
"message": generate_message(log, user_raw)
|
197 |
-
})
|
198 |
|
199 |
has_next = len(logs) == limit
|
200 |
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
import aiohttp
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
import pytz
|
6 |
from fastapi import APIRouter, HTTPException, Query, Header
|
7 |
from typing import Dict, Any
|
8 |
|
|
|
127 |
else:
|
128 |
logger.warning("⚠️ Erro ao buscar dados dos usuários")
|
129 |
|
130 |
+
|
131 |
+
def format_relative_date(iso_date: str) -> str:
|
132 |
+
ny_tz = pytz.timezone("America/New_York")
|
133 |
+
try:
|
134 |
+
created_dt = datetime.fromisoformat(iso_date)
|
135 |
+
created_dt = created_dt.astimezone(ny_tz)
|
136 |
+
except Exception:
|
137 |
+
return iso_date
|
138 |
+
|
139 |
+
now = datetime.now(ny_tz)
|
140 |
+
diff = now - created_dt
|
141 |
+
|
142 |
+
if diff < timedelta(seconds=60):
|
143 |
+
return "just now"
|
144 |
+
elif diff < timedelta(hours=1):
|
145 |
+
minutes = int(diff.total_seconds() // 60)
|
146 |
+
return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
|
147 |
+
elif diff < timedelta(hours=24):
|
148 |
+
hours = int(diff.total_seconds() // 3600)
|
149 |
+
return f"{hours} hour{'s' if hours != 1 else ''} ago"
|
150 |
+
elif (now.date() - created_dt.date()).days == 1:
|
151 |
+
return f"Yesterday at {created_dt.strftime('%I:%M %p').lstrip('0')}"
|
152 |
+
else:
|
153 |
+
return created_dt.strftime("%m/%d/%Y")
|
154 |
+
|
155 |
# Função para gerar mensagem descritiva
|
156 |
def generate_message(log, user_info):
|
157 |
action = log.get("action")
|
|
|
170 |
except Exception:
|
171 |
new_data = {}
|
172 |
|
173 |
+
email = user_info.get("email", "Someone")
|
174 |
target_email = None
|
175 |
|
176 |
if reference == "User" and new_data:
|
|
|
178 |
elif reference == "Approval" and old_data:
|
179 |
target_email = old_data.get("name")
|
180 |
elif reference == "Collaboration":
|
181 |
+
target_email = new_data.get("email") or old_data.get("email")
|
|
|
|
|
182 |
elif reference == "Onboarding":
|
183 |
target_email = None
|
184 |
|
185 |
if action == "update" and reference == "User":
|
186 |
+
return f"{email} updated profile information of {target_email or 'a user'}"
|
187 |
elif action == "approve" and reference == "Approval":
|
188 |
+
return f"{email} approved stylist {target_email or ''}"
|
189 |
elif action == "deny" and reference == "Approval":
|
190 |
+
return f"{email} denied stylist {target_email or ''}"
|
191 |
elif action == "update" and reference == "Onboarding":
|
192 |
+
return f"{email} updated an onboarding question"
|
193 |
elif action == "add" and reference == "Onboarding":
|
194 |
+
return f"{email} added a new onboarding question"
|
195 |
elif action == "delete" and reference == "Onboarding":
|
196 |
+
return f"{email} deleted an onboarding question"
|
197 |
elif action == "add" and reference == "Collaboration":
|
198 |
+
return f"{email} added {target_email or 'a collaborator'} to the team"
|
199 |
elif action == "delete" and reference == "Collaboration":
|
200 |
+
return f"{email} removed {target_email or 'a collaborator'} from the team"
|
201 |
elif action == "update" and reference == "Collaboration":
|
202 |
+
return f"{email} updated permissions of {target_email or 'a collaborator'}"
|
203 |
|
204 |
+
return f"{email} performed action: {action} on {reference}"
|
205 |
+
|
206 |
+
enriched_logs.append({
|
207 |
+
"id": log["id"],
|
208 |
+
"user": {
|
209 |
+
"id": user_raw.get("id"),
|
210 |
+
"email": user_raw.get("email"),
|
211 |
+
"avatar": user_raw.get("avatar")
|
212 |
+
},
|
213 |
+
"action": log["action"],
|
214 |
+
"reference": log["reference"],
|
215 |
+
"old_data": log["old_data"],
|
216 |
+
"new_data": log["new_data"],
|
217 |
+
"created_at": log["created_at"],
|
218 |
+
"message": generate_message(log, user_raw),
|
219 |
+
"formatted_date": format_relative_date(log["created_at"])
|
220 |
+
})
|
|
|
|
|
221 |
|
222 |
has_next = len(logs) == limit
|
223 |
|