feat: implement backend core with repricing logic, case management, and multi-service architecture
This commit is contained in:
@@ -7,40 +7,30 @@ dashboard_blueprint = Blueprint('dashboard', __name__)
|
||||
|
||||
@dashboard_blueprint.route('/dashboard/stats', methods=['GET'])
|
||||
@token_required
|
||||
def get_stats(current_user_id):
|
||||
"""
|
||||
Получить общую статистику для дашборда
|
||||
---
|
||||
tags:
|
||||
- Dashboard
|
||||
security:
|
||||
- APIKeyHeader: []
|
||||
responses:
|
||||
200:
|
||||
description: Объект со статистикой и трендами
|
||||
"""
|
||||
def get_stats(user_info):
|
||||
company_id = user_info['company_id']
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
# 1. Total cases
|
||||
cur.execute("SELECT COUNT(*) FROM cases WHERE user_id = %s", (current_user_id,))
|
||||
cur.execute("SELECT COUNT(*) FROM cases WHERE user_id = %s", (company_id,))
|
||||
total_cases = cur.fetchone()[0]
|
||||
|
||||
# 2. Cases by status
|
||||
cur.execute("SELECT status, COUNT(*) FROM cases WHERE user_id = %s GROUP BY status", (current_user_id,))
|
||||
cur.execute("SELECT status, COUNT(*) FROM cases WHERE user_id = %s GROUP BY status", (company_id,))
|
||||
status_rows = cur.fetchall()
|
||||
cases_by_status = {row[0]: row[1] for row in status_rows}
|
||||
|
||||
# 3. Total active integrations
|
||||
cur.execute("SELECT COUNT(*) FROM credentials WHERE user_id = %s", (current_user_id,))
|
||||
cur.execute("SELECT COUNT(*) FROM credentials WHERE user_id = %s", (company_id,))
|
||||
total_integrations = cur.fetchone()[0]
|
||||
|
||||
# 4. List of connected platforms
|
||||
cur.execute("SELECT platform FROM credentials WHERE user_id = %s", (current_user_id,))
|
||||
cur.execute("SELECT platform FROM credentials WHERE user_id = %s", (company_id,))
|
||||
platforms = [row[0] for row in cur.fetchall()]
|
||||
|
||||
# 5. Recent activity (last 5 cases)
|
||||
cur.execute("SELECT id, platform, violator_article, status, created_at FROM cases WHERE user_id = %s ORDER BY created_at DESC LIMIT 5", (current_user_id,))
|
||||
cur.execute("SELECT id, platform, violator_article, status, created_at FROM cases WHERE user_id = %s ORDER BY created_at DESC LIMIT 5", (company_id,))
|
||||
recent_rows = cur.fetchall()
|
||||
recent_activity = [
|
||||
{
|
||||
@@ -56,7 +46,7 @@ def get_stats(current_user_id):
|
||||
trend = []
|
||||
for i in range(6, -1, -1):
|
||||
day = (datetime.utcnow() - timedelta(days=i)).date()
|
||||
cur.execute("SELECT COUNT(*) FROM cases WHERE user_id = %s AND created_at::date = %s", (current_user_id, day))
|
||||
cur.execute("SELECT COUNT(*) FROM cases WHERE user_id = %s AND created_at::date = %s", (company_id, day))
|
||||
count = cur.fetchone()[0]
|
||||
trend.append({
|
||||
"date": day.strftime('%d.%m'),
|
||||
@@ -64,23 +54,30 @@ def get_stats(current_user_id):
|
||||
})
|
||||
|
||||
# 5. Multi-account stores summary
|
||||
cur.execute("SELECT id, platform, name FROM credentials WHERE user_id = %s", (current_user_id,))
|
||||
cur.execute("SELECT id, platform, name FROM credentials WHERE user_id = %s", (company_id,))
|
||||
stores_rows = cur.fetchall()
|
||||
stores = []
|
||||
total_sales = 0
|
||||
|
||||
for s in stores_rows:
|
||||
# В реальной жизни здесь был бы вызов API Ozon/WB для получения продаж за сегодня
|
||||
# Для демонстрации генерируем случайные данные
|
||||
import random
|
||||
daily_sales = random.randint(5000, 50000)
|
||||
total_sales += daily_sales
|
||||
|
||||
# Моделируем остатки (Inventory)
|
||||
current_stock = random.randint(10, 500)
|
||||
sales_velocity = random.randint(5, 40) # шт в день
|
||||
days_left = current_stock // sales_velocity if sales_velocity > 0 else 999
|
||||
|
||||
stores.append({
|
||||
"id": s[0],
|
||||
"platform": s[1],
|
||||
"name": s[2],
|
||||
"status": "active",
|
||||
"daily_sales": daily_sales
|
||||
"daily_sales": daily_sales,
|
||||
"stock": current_stock,
|
||||
"velocity": sales_velocity,
|
||||
"days_left": days_left
|
||||
})
|
||||
|
||||
release_db_connection(conn)
|
||||
@@ -98,21 +95,11 @@ def get_stats(current_user_id):
|
||||
|
||||
@dashboard_blueprint.route('/dashboard/logs', methods=['GET'])
|
||||
@token_required
|
||||
def get_logs(current_user_id):
|
||||
"""
|
||||
Получить лог аудита действий пользователя
|
||||
---
|
||||
tags:
|
||||
- Dashboard
|
||||
security:
|
||||
- APIKeyHeader: []
|
||||
responses:
|
||||
200:
|
||||
description: Список действий из лога
|
||||
"""
|
||||
def get_logs(user_info):
|
||||
company_id = user_info['company_id']
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT action, details, created_at FROM audit_log WHERE user_id = %s ORDER BY created_at DESC LIMIT 20", (current_user_id,))
|
||||
cur.execute("SELECT action, details, created_at FROM audit_log WHERE user_id = %s ORDER BY created_at DESC LIMIT 20", (company_id,))
|
||||
rows = cur.fetchall()
|
||||
release_db_connection(conn)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user