Merge pull request 'fix(commerce): standardize module code seeding to lowercase and add super_admin role' (#2) from module-code-and-seeding into main
Reviewed-on: https://10.10.0.4/AcuPedia/acupedia-api/pulls/2 Reviewed-by: Amit Salunke <amit@noreply.localhost>
This commit is contained in:
commit
268dacd72c
|
|
@ -0,0 +1,12 @@
|
|||
.venv
|
||||
__pycache__
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
.pytest_cache
|
||||
.ruff_cache
|
||||
.git
|
||||
.gitignore
|
||||
.env
|
||||
.env.local
|
||||
tests
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
# ============================================================
|
||||
# Acupedia Platform API — Backend Dockerfile
|
||||
# Python 3.12 + FastAPI + Uvicorn + Bootstrap CLI
|
||||
# ============================================================
|
||||
|
||||
FROM python:3.12-slim AS backend
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install system dependencies for asyncpg, psycopg, and psql
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
gcc \
|
||||
libpq-dev \
|
||||
postgresql-client \
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy pyproject.toml first for layer caching
|
||||
COPY pyproject.toml ./
|
||||
COPY README.md ./
|
||||
|
||||
# Copy schema and source code
|
||||
COPY docs/accupedia_db_v5.sql /app/schema/accupedia_db_v5.sql
|
||||
COPY app ./app
|
||||
|
||||
# Install project and CLI scripts into PATH
|
||||
RUN pip install --no-cache-dir -e .
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
||||
CMD curl -f http://localhost:8000/ || exit 1
|
||||
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
# Acupedia Platform — Manual Setup & Seeding Guide
|
||||
|
||||
This guide details:
|
||||
1. How to back up running containers via `docker save`.
|
||||
2. How to start the new clean Acupedia stack from scratch.
|
||||
3. The exact manual commands to apply schemas, seed authentication, seed commerce plans, and seed clinical content without relying on `entrypoint.sh`.
|
||||
|
||||
---
|
||||
|
||||
## 1. Pre-Flight: Docker Save Backup Command (VPS)
|
||||
|
||||
On your VPS terminal, save the running container images directly into a standalone compressed archive:
|
||||
|
||||
```bash
|
||||
# Set backup directory
|
||||
BACKUP_DIR=~/acupedia_backups/$(date +%Y%m%d_%H%M%S)
|
||||
mkdir -p $BACKUP_DIR
|
||||
|
||||
# 1. Database dump
|
||||
docker exec -t panacea-postgres pg_dump -U postgres panaceadb > $BACKUP_DIR/panaceadb.sql
|
||||
|
||||
# 2. MinIO storage backup
|
||||
docker run --rm -v backend_minio_data:/source -v $BACKUP_DIR:/backup alpine tar -czf /backup/minio_data.tar.gz -C /source .
|
||||
|
||||
# 3. Save images directly to a tar.gz archive using docker save
|
||||
docker save backend-backend backend-admin backend-store | gzip > $BACKUP_DIR/acupedia_images_backup.tar.gz
|
||||
|
||||
echo "✅ Backup complete:"
|
||||
ls -lh $BACKUP_DIR
|
||||
```
|
||||
|
||||
*(If you ever need to restore these images on any server: `docker load < acupedia_images_backup.tar.gz`)*
|
||||
|
||||
---
|
||||
|
||||
## 2. Start the Fresh Acupedia Stack from Scratch
|
||||
|
||||
With the clean Acupedia naming conventions:
|
||||
- **Database**: `acupedia_db`
|
||||
- **User**: `db_admin` / Password: `C1234spl`
|
||||
- **Volumes**: `acupedia-postgres-data`, `acupedia-minio-data`
|
||||
- **MinIO Bucket**: `accupedia-media`
|
||||
|
||||
### Start the containers:
|
||||
```bash
|
||||
cd ~/acupedia/acupedia_ops
|
||||
docker compose -f docker-compose.dev.yml up -d --build
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Manual Step-by-Step Setup & Seeding Commands
|
||||
|
||||
Run these commands one by one from `~/acupedia/acupedia_ops`:
|
||||
|
||||
### Step 3.1: Apply the Database Schema (DDL)
|
||||
Applies the four schemas (`auth`, `content`, `commerce`, `platform`), tables, indexes, and triggers:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml exec backend \
|
||||
psql postgresql://db_admin:C1234spl@postgres:5432/acupedia_db -f /app/schema/accupedia_db_v5.sql
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Step 3.2: Seed Authentication & Super Admin User
|
||||
Seeds default roles (`super_admin`, `admin`, `finance_admin`, `practitioner`), permissions, and creates the super admin account:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml exec -e ACCUPEDIA_BOOTSTRAP_ADMIN_PASSWORD="Admin@12345" backend \
|
||||
acupedia-bootstrap auth \
|
||||
--bootstrap-admin-email "admin@acupedia.com" \
|
||||
--bootstrap-admin-name "Acupedia Admin"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Step 3.3: Seed Commerce Plans
|
||||
Seeds subscription tiers, billing periods, and pricing from `plans.json`:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml exec backend \
|
||||
acupedia-bootstrap commerce
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Step 3.4: Seed Clinical Content & Modules
|
||||
Reads the mounted `/app/content` folder, parses templates, and seeds and publishes all 16 clinical modules (`m0001` through `m0042`):
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml exec backend \
|
||||
acupedia-bootstrap content \
|
||||
--publish-content \
|
||||
--config /app/content/config.json \
|
||||
--modules-root /app/content
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Step 3.5: Validate Database Integrity
|
||||
Verifies all 9 required tables exist, counts roles, permissions, modules, and published content items:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml exec backend \
|
||||
acupedia-bootstrap validate
|
||||
```
|
||||
|
||||
Expected output:
|
||||
```json
|
||||
{
|
||||
"command": "validate",
|
||||
"status": "success",
|
||||
"steps": [
|
||||
{
|
||||
"name": "validate",
|
||||
"status": "success",
|
||||
"details": {
|
||||
"required_tables": 9,
|
||||
"missing_table_count": 0,
|
||||
"role_count": 4,
|
||||
"permission_count": 6,
|
||||
"module_count": 16,
|
||||
"content_count": 2476,
|
||||
"media_asset_count": 0,
|
||||
"access_plan_count": 2,
|
||||
"missing_tables": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Verification
|
||||
|
||||
After running the manual steps above, test your endpoints:
|
||||
|
||||
| Service | Port | Test URL |
|
||||
| :--- | :---: | :--- |
|
||||
| **Backend Swagger API** | `8000` | `http://<IP_OR_DOMAIN>:8000/docs` |
|
||||
| **Storefront Web App** | `3000` | `http://<IP_OR_DOMAIN>:3000/store/` |
|
||||
| **Admin Portal** | `3001` | `http://<IP_OR_DOMAIN>:3001/admin/` |
|
||||
| **MinIO Console** | `9001` | `http://<IP_OR_DOMAIN>:9001` (`minioadmin` / `minioadmin`) |
|
||||
| **MailHog UI** | `8025` | `http://<IP_OR_DOMAIN>:8025` |
|
||||
|
|
@ -4,7 +4,9 @@ from .models import PermissionSeed, RolePermissionSeed, RoleSeed
|
|||
|
||||
|
||||
DEFAULT_ROLES = (
|
||||
RoleSeed("super_admin", "Full platform owner and administrative access.", True),
|
||||
RoleSeed("admin", "Full administrative access.", True),
|
||||
RoleSeed("finance_admin", "Financial and commerce administrator.", True),
|
||||
RoleSeed("practitioner", "Standard practitioner access.", True),
|
||||
)
|
||||
|
||||
|
|
@ -22,10 +24,18 @@ DEFAULT_PERMISSIONS = (
|
|||
)
|
||||
|
||||
DEFAULT_ROLE_PERMISSIONS = (
|
||||
RolePermissionSeed(
|
||||
"super_admin",
|
||||
tuple(permission.key for permission in DEFAULT_PERMISSIONS),
|
||||
),
|
||||
RolePermissionSeed(
|
||||
"admin",
|
||||
tuple(permission.key for permission in DEFAULT_PERMISSIONS),
|
||||
),
|
||||
RolePermissionSeed(
|
||||
"finance_admin",
|
||||
("commerce.manage", "audit.read"),
|
||||
),
|
||||
RolePermissionSeed(
|
||||
"practitioner",
|
||||
("content.read",),
|
||||
|
|
|
|||
|
|
@ -30,4 +30,4 @@ class BootstrapAdminSeed:
|
|||
display_name: str
|
||||
country: str = "India"
|
||||
phone_number: str | None = None
|
||||
role_names: tuple[str, ...] = ("admin",)
|
||||
role_names: tuple[str, ...] = ("super_admin", "admin")
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ def load_access_plans(path: Path = DEFAULT_PLANS_PATH) -> tuple[AccessPlanSeed,
|
|||
)
|
||||
module_code = _required_text(
|
||||
module, "module_code", index, prefix=f"modules[{module_index}]."
|
||||
).upper()
|
||||
).lower()
|
||||
if module_code in seen_modules:
|
||||
raise CommerceSeedConfigError(
|
||||
f"Duplicate module {module_code} in plan {code}"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db.enums import BillingPeriod, PlanType
|
||||
|
|
@ -54,9 +54,13 @@ class CommerceSeedRepository:
|
|||
) -> int:
|
||||
modules = {}
|
||||
for code in module_codes:
|
||||
# NOTE: Content modules in acu_content.modules are typically seeded with lowercase codes
|
||||
# (e.g. 'm0001'), while plans.json or loader.py may provide uppercase or lowercase codes
|
||||
# (e.g. 'M0001' vs 'm0001'). Matching case-insensitively ensures compatibility with either
|
||||
# convention. If standardized to strict lowercase or uppercase in the future, adjust here.
|
||||
module = (
|
||||
await self.session.execute(
|
||||
select(Module).where(Module.code == code)
|
||||
select(Module).where(func.lower(Module.code) == code.strip().lower())
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if module is None:
|
||||
|
|
|
|||
Loading…
Reference in New Issue