mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-12 23:23:22 +00:00
Initial Commit
This commit is contained in:
99
app/routers/admin/settings.py
Normal file
99
app/routers/admin/settings.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
import asyncpg
|
||||
|
||||
from app.core.responses import ok
|
||||
from app.dependencies import get_db, require_admin
|
||||
from app.exceptions import NotFoundError
|
||||
from app.models.admin import StoreSettingUpdate, AdminUserCreate, EmailTemplateUpdate
|
||||
from app.services import auth_service
|
||||
|
||||
router = APIRouter(tags=["Admin — Settings"])
|
||||
|
||||
|
||||
# ── Store settings ────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/settings")
|
||||
async def get_settings(
|
||||
db: asyncpg.Connection = Depends(get_db),
|
||||
_: dict = Depends(require_admin),
|
||||
):
|
||||
rows = await db.fetch("SELECT key, value, updated_at FROM store_settings ORDER BY key")
|
||||
return ok([dict(r) for r in rows])
|
||||
|
||||
|
||||
@router.put("/settings/{key}")
|
||||
async def update_setting(
|
||||
key: str,
|
||||
body: StoreSettingUpdate,
|
||||
db: asyncpg.Connection = Depends(get_db),
|
||||
_: dict = Depends(require_admin),
|
||||
):
|
||||
import json
|
||||
row = await db.fetchrow(
|
||||
"""
|
||||
INSERT INTO store_settings (key, value)
|
||||
VALUES ($1, $2::jsonb)
|
||||
ON CONFLICT (key) DO UPDATE SET value = $2::jsonb, updated_at = now()
|
||||
RETURNING *
|
||||
""",
|
||||
key, json.dumps(body.value),
|
||||
)
|
||||
return ok(dict(row))
|
||||
|
||||
|
||||
# ── Email templates ───────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/email-templates")
|
||||
async def get_templates(
|
||||
db: asyncpg.Connection = Depends(get_db),
|
||||
_: dict = Depends(require_admin),
|
||||
):
|
||||
rows = await db.fetch("SELECT * FROM email_templates ORDER BY name")
|
||||
return ok([dict(r) for r in rows])
|
||||
|
||||
|
||||
@router.put("/email-templates/{name}")
|
||||
async def update_template(
|
||||
name: str,
|
||||
body: EmailTemplateUpdate,
|
||||
db: asyncpg.Connection = Depends(get_db),
|
||||
_: dict = Depends(require_admin),
|
||||
):
|
||||
row = await db.fetchrow(
|
||||
"""
|
||||
INSERT INTO email_templates (name, subject, body_html, body_text)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (name) DO UPDATE SET subject = $2, body_html = $3, body_text = $4, updated_at = now()
|
||||
RETURNING *
|
||||
""",
|
||||
name, body.subject, body.body_html, body.body_text,
|
||||
)
|
||||
return ok(dict(row))
|
||||
|
||||
|
||||
# ── Admin users ───────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/users")
|
||||
async def list_admin_users(
|
||||
db: asyncpg.Connection = Depends(get_db),
|
||||
_: dict = Depends(require_admin),
|
||||
):
|
||||
rows = await db.fetch(
|
||||
"SELECT id, email, full_name, created_at FROM profiles WHERE role = 'admin' ORDER BY created_at"
|
||||
)
|
||||
return ok([dict(r) for r in rows])
|
||||
|
||||
|
||||
@router.post("/users", status_code=201)
|
||||
async def create_admin_user(
|
||||
body: AdminUserCreate,
|
||||
db: asyncpg.Connection = Depends(get_db),
|
||||
_: dict = Depends(require_admin),
|
||||
):
|
||||
user = await auth_service.create_admin_user(body.email, body.password, body.full_name, db)
|
||||
return ok({
|
||||
"id": str(user["id"]),
|
||||
"email": user["email"],
|
||||
"full_name": user["full_name"],
|
||||
"role": user["role"],
|
||||
})
|
||||
Reference in New Issue
Block a user