Add Django backend, 1C integration, deploy config and API docs
Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
This commit is contained in:
1 parent
5f8d37357d
commit
756f40353b
130 files changed
+10066
No files matched your search
@@ -0,0 +1,163 @@
|
||||
from django.utils import timezone
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from apps.common.authentication import (
|
||||
IntegrationTokenAuthentication,
|
||||
IsIntegrationClient,
|
||||
)
|
||||
from apps.common.exceptions import IntegrationError
|
||||
from apps.orders.models import Order
|
||||
|
||||
from .models import SyncRun
|
||||
from .serializers import (
|
||||
BonusPayloadSerializer,
|
||||
CatalogSyncSerializer,
|
||||
HealthSerializer,
|
||||
ManualSyncResponseSerializer,
|
||||
OrderAckSerializer,
|
||||
OrderStatusPayloadSerializer,
|
||||
PendingOrderSerializer,
|
||||
)
|
||||
from .services import apply_bonus, apply_order_status, upsert_catalog
|
||||
|
||||
|
||||
class IntegrationBaseView(APIView):
|
||||
authentication_classes = [IntegrationTokenAuthentication]
|
||||
permission_classes = [IsIntegrationClient]
|
||||
|
||||
|
||||
class IntegrationHealthView(IntegrationBaseView):
|
||||
serializer_class = HealthSerializer
|
||||
|
||||
def get(self, request):
|
||||
return Response({"status": "ok", "time": timezone.now().isoformat()})
|
||||
|
||||
|
||||
class CatalogSyncView(IntegrationBaseView):
|
||||
serializer_class = CatalogSyncSerializer
|
||||
|
||||
def post(self, request):
|
||||
serializer = CatalogSyncSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
run = SyncRun.objects.create(
|
||||
direction=SyncRun.Direction.INBOUND,
|
||||
entity=SyncRun.Entity.CATALOG,
|
||||
status=SyncRun.Status.RUNNING,
|
||||
started_at=timezone.now(),
|
||||
)
|
||||
try:
|
||||
stats = upsert_catalog(serializer.validated_data, run=run)
|
||||
except Exception as exc: # noqa: BLE001 - report the failure to 1C
|
||||
run.status = SyncRun.Status.FAILED
|
||||
run.error = str(exc)
|
||||
run.finished_at = timezone.now()
|
||||
run.save(update_fields=["status", "error", "finished_at", "updated_at"])
|
||||
return Response(
|
||||
{"error": {"code": "catalog_sync_failed", "message": str(exc)}},
|
||||
status=400,
|
||||
)
|
||||
|
||||
run.status = SyncRun.Status.SUCCESS
|
||||
run.stats = stats
|
||||
run.finished_at = timezone.now()
|
||||
run.save(update_fields=["status", "stats", "finished_at", "updated_at"])
|
||||
return Response(
|
||||
{
|
||||
"status": "ok",
|
||||
"sync_id": serializer.validated_data.get("sync_id", ""),
|
||||
"stats": stats,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class OrderStatusView(IntegrationBaseView):
|
||||
serializer_class = OrderStatusPayloadSerializer
|
||||
|
||||
def post(self, request, number: str):
|
||||
serializer = OrderStatusPayloadSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
try:
|
||||
order = apply_order_status(number, serializer.validated_data)
|
||||
except IntegrationError as exc:
|
||||
status_code = 404 if exc.code == "order_not_found" else 400
|
||||
return Response(
|
||||
{"error": {"code": exc.code, "message": exc.message}},
|
||||
status=status_code,
|
||||
)
|
||||
return Response(
|
||||
{"status": "ok", "order": order.number, "order_status": order.status}
|
||||
)
|
||||
|
||||
|
||||
class BonusView(IntegrationBaseView):
|
||||
serializer_class = BonusPayloadSerializer
|
||||
|
||||
def post(self, request):
|
||||
serializer = BonusPayloadSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
try:
|
||||
user = apply_bonus(serializer.validated_data)
|
||||
except IntegrationError as exc:
|
||||
status_code = 404 if exc.code == "customer_not_found" else 400
|
||||
return Response(
|
||||
{"error": {"code": exc.code, "message": exc.message}},
|
||||
status=status_code,
|
||||
)
|
||||
return Response({"status": "ok", "phone": user.phone})
|
||||
|
||||
|
||||
class PendingOrdersView(IntegrationBaseView):
|
||||
serializer_class = PendingOrderSerializer
|
||||
|
||||
def get(self, request):
|
||||
orders = (
|
||||
Order.objects.filter(external_id="")
|
||||
.exclude(status=Order.Status.CANCELLED)
|
||||
.prefetch_related("items")
|
||||
.order_by("created_at")[:100]
|
||||
)
|
||||
return Response({"orders": PendingOrderSerializer(orders, many=True).data})
|
||||
|
||||
|
||||
class OrderAckView(IntegrationBaseView):
|
||||
serializer_class = OrderAckSerializer
|
||||
|
||||
def post(self, request, number: str):
|
||||
serializer = OrderAckSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
order = Order.objects.filter(number=number).first()
|
||||
if order is None:
|
||||
return Response(
|
||||
{
|
||||
"error": {
|
||||
"code": "order_not_found",
|
||||
"message": f"Заказ {number} не найден",
|
||||
}
|
||||
},
|
||||
status=404,
|
||||
)
|
||||
|
||||
order.external_id = serializer.validated_data["external_id"]
|
||||
order.synced_at = timezone.now()
|
||||
order.sync_error = ""
|
||||
order.save(
|
||||
update_fields=["external_id", "synced_at", "sync_error", "updated_at"]
|
||||
)
|
||||
return Response({"status": "ok"})
|
||||
|
||||
|
||||
class ManualSyncView(APIView):
|
||||
"""Admin-triggered catalog sync (JWT + staff)."""
|
||||
|
||||
permission_classes = [IsAdminUser]
|
||||
serializer_class = ManualSyncResponseSerializer
|
||||
|
||||
def post(self, request):
|
||||
from .tasks import sync_catalog_from_1c
|
||||
|
||||
task = sync_catalog_from_1c.delay()
|
||||
return Response({"status": "queued", "task_id": task.id})
|
||||
Reference in new issue
Block a user