Update routes/logs.py
Browse files- routes/logs.py +51 -9
routes/logs.py
CHANGED
@@ -109,37 +109,79 @@ async def get_logs(
|
|
109 |
"has_next": False
|
110 |
}
|
111 |
|
112 |
-
#
|
113 |
user_ids = list({log["user"] for log in logs if log.get("user")})
|
114 |
user_info_map = {}
|
115 |
|
116 |
if user_ids:
|
117 |
user_ids_query = ",".join(f'"{uid}"' for uid in user_ids)
|
118 |
-
users_url = f"{SUPABASE_URL}/rest/v1/User?id=in.({user_ids_query})&select=id,
|
119 |
|
120 |
async with aiohttp.ClientSession() as session:
|
121 |
async with session.get(users_url, headers=SUPABASE_HEADERS) as response:
|
122 |
if response.status == 200:
|
123 |
users_data = await response.json()
|
124 |
-
user_info_map = {user["id"]:
|
125 |
else:
|
126 |
logger.warning("⚠️ Erro ao buscar dados dos usuários")
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
enriched_logs = []
|
129 |
for log in logs:
|
130 |
-
|
131 |
enriched_logs.append({
|
132 |
"id": log["id"],
|
133 |
"user": {
|
134 |
-
"id":
|
135 |
-
"
|
136 |
-
"avatar":
|
137 |
},
|
138 |
"action": log["action"],
|
139 |
"reference": log["reference"],
|
140 |
"old_data": log["old_data"],
|
141 |
"new_data": log["new_data"],
|
142 |
-
"created_at": log["created_at"]
|
|
|
143 |
})
|
144 |
|
145 |
has_next = len(logs) == limit
|
@@ -154,4 +196,4 @@ async def get_logs(
|
|
154 |
raise he
|
155 |
except Exception as e:
|
156 |
logger.error(f"❌ Erro interno ao buscar logs: {str(e)}")
|
157 |
-
raise HTTPException(status_code=500, detail="Erro interno do servidor")
|
|
|
109 |
"has_next": False
|
110 |
}
|
111 |
|
112 |
+
# Buscar dados dos usuários
|
113 |
user_ids = list({log["user"] for log in logs if log.get("user")})
|
114 |
user_info_map = {}
|
115 |
|
116 |
if user_ids:
|
117 |
user_ids_query = ",".join(f'"{uid}"' for uid in user_ids)
|
118 |
+
users_url = f"{SUPABASE_URL}/rest/v1/User?id=in.({user_ids_query})&select=id,email,avatar,name"
|
119 |
|
120 |
async with aiohttp.ClientSession() as session:
|
121 |
async with session.get(users_url, headers=SUPABASE_HEADERS) as response:
|
122 |
if response.status == 200:
|
123 |
users_data = await response.json()
|
124 |
+
user_info_map = {user["id"]: user for user in users_data}
|
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")
|
131 |
+
reference = log.get("reference")
|
132 |
+
old_data = log.get("old_data")
|
133 |
+
new_data = log.get("new_data")
|
134 |
+
|
135 |
+
email = user_info.get("email", "Alguém")
|
136 |
+
name2 = None
|
137 |
+
|
138 |
+
try:
|
139 |
+
if reference == "User" and new_data:
|
140 |
+
name2 = eval(new_data).get("name")
|
141 |
+
elif reference == "Approval" and old_data:
|
142 |
+
name2 = eval(old_data).get("name")
|
143 |
+
elif reference in ("Collaboration", "Onboarding") and new_data:
|
144 |
+
name2 = eval(new_data).get("name")
|
145 |
+
except Exception:
|
146 |
+
name2 = None
|
147 |
+
|
148 |
+
if action == "update" and reference == "User":
|
149 |
+
return f"{email} atualizou informações de {name2 or 'um usuário'}"
|
150 |
+
elif action == "approve" and reference == "Approval":
|
151 |
+
return f"{email} aprovou o estilista {name2 or ''}"
|
152 |
+
elif action == "deny" and reference == "Approval":
|
153 |
+
return f"{email} negou o estilista {name2 or ''}"
|
154 |
+
elif action == "update" and reference == "Onboarding":
|
155 |
+
return f"{email} atualizou uma pergunta do onboarding"
|
156 |
+
elif action == "add" and reference == "Onboarding":
|
157 |
+
return f"{email} adicionou uma pergunta ao onboarding"
|
158 |
+
elif action == "delete" and reference == "Onboarding":
|
159 |
+
return f"{email} excluiu uma pergunta do onboarding"
|
160 |
+
elif action == "add" and reference == "Collaboration":
|
161 |
+
return f"{email} adicionou um novo colaborador"
|
162 |
+
elif action == "delete" and reference == "Collaboration":
|
163 |
+
return f"{email} excluiu um colaborador da lista de colaboradores"
|
164 |
+
elif action == "update" and reference == "Collaboration":
|
165 |
+
return f"{email} atualizou as permissões de um colaborador"
|
166 |
+
|
167 |
+
return f"{email} realizou uma ação: {action} em {reference}"
|
168 |
+
|
169 |
enriched_logs = []
|
170 |
for log in logs:
|
171 |
+
user_raw = user_info_map.get(log["user"], {})
|
172 |
enriched_logs.append({
|
173 |
"id": log["id"],
|
174 |
"user": {
|
175 |
+
"id": user_raw.get("id"),
|
176 |
+
"email": user_raw.get("email"),
|
177 |
+
"avatar": user_raw.get("avatar")
|
178 |
},
|
179 |
"action": log["action"],
|
180 |
"reference": log["reference"],
|
181 |
"old_data": log["old_data"],
|
182 |
"new_data": log["new_data"],
|
183 |
+
"created_at": log["created_at"],
|
184 |
+
"message": generate_message(log, user_raw)
|
185 |
})
|
186 |
|
187 |
has_next = len(logs) == limit
|
|
|
196 |
raise he
|
197 |
except Exception as e:
|
198 |
logger.error(f"❌ Erro interno ao buscar logs: {str(e)}")
|
199 |
+
raise HTTPException(status_code=500, detail="Erro interno do servidor")
|