Skip to content

Gaaaaaby/luna-technicaltest

Repository files navigation

ZeBrands Catalog API

FastAPI backend with PostgreSQL, JWT auth, WebSocket admin notifications, and Gmail API email.

1) Setup

  • Install dependencies:
pip install -r requirements.txt
  • Database (PostgreSQL on localhost:5432):
CREATE DATABASE zebrands;
  • Environment (.env in project root):
cp .env.example .env

Required environment variables:

DATABASE_URL=postgresql+asyncpg://postgres:121224@localhost:5432/zebrands
JWT_SECRET_KEY=change-me-in-production
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60
APP_NAME=ZeBrands Catalog API
ENVIRONMENT=development
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GMAIL_SENDER_EMAIL=your_sender_email@gmail.com
GMAIL_RECIPIENT_EMAIL=your_recipient_email@gmail.com

Usa .env.example como plantilla y configúralo localmente con tus credenciales reales.
El GOOGLE_CLIENT_SECRET te lo proporcionaré.

2) Run

uvicorn app.main:app --reload --host 127.0.0.1 --port 8000

Docs: http://127.0.0.1:8000/docs

Seed initial admin:

python seed.py

Admin: admin@example.com / admin123

3) Auth (get token)

PowerShell example:

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/auth/token" -Method POST -Headers @{"Content-Type"="application/x-www-form-urlencoded"} -Body "username=admin@example.com&password=admin123"
$token = ($response.Content | ConvertFrom-Json).access_token

4) Endpoints

  • Products (public):
    • GET /products
    • GET /products/{id} (creates a view log)
  • Products (admin, Authorization: Bearer ):
    • POST /products
    • PUT /products/{id}/update (or PUT /products/{id}/edit)
    • DELETE /products/{id}
  • Admin users (admin):
    • GET /admin/users
    • POST /admin/users
    • PUT /admin/users/{id}/update (or PUT /admin/users/{id}/edit)
    • DELETE /admin/users/{id}

5) Notifications

  • WebSocket (admin): connect to ws://127.0.0.1:8000/ws/admin-notifications?token=<JWT> and keep the connection open to receive product update events.
  • Email (Gmail API): product updates trigger a background email to GMAIL_RECIPIENT_EMAIL.

6) Analytics (admin)

  • GET /analytics/summary — overall counts
  • GET /analytics/product-views — aggregated views (supports days, limit, optional product_id)
  • GET /analytics/product-views/{id}/detailed — recent view logs
  • GET /analytics/user-agents — top agents

Example (PostgreSQL):

CREATE ROLE zebrands_app LOGIN PASSWORD 'strong-password';
GRANT CONNECT ON DATABASE zebrands TO zebrands_app;
\c zebrands
GRANT USAGE ON SCHEMA public TO zebrands_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO zebrands_app;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO zebrands_app;

Then set:

DATABASE_URL=postgresql+asyncpg://zebrands_app:strong-password@localhost:5432/zebrands

---

}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages