infrasynth-backend-kit/infrasynth/webhooks/migrations/0001_initial.py
jcv-dev 551b42eab5 feat: production-hardening pass across the kit
Close the gaps between the documented contract (API-STANDARD, TENANCY,
ENTITLEMENTS) and the implementation, and remove committed build artifacts.

Security:
- verify + process inbound webhooks (HMAC/handler verify, size limit,
  timestamp tolerance, idempotency via InboundEvent.external_id)
- real 2FA login flow (pre-auth challenge; tokens only after verify/recovery)
- wire HybridPermission into security/audit views; add API-key rotate and
  users/<id>/permissions|roles endpoints
- tenant-scoped throttling on by default; webhook replay protection
- verify MercadoPago webhook signatures
- login brute-force guard, configurable password policy, real ALTCHA PoW

Correctness:
- apply verified billing webhooks idempotently (subscription/entitlement/
  invoice/PaymentTransaction); scheduled payment lifecycle jobs
- capture audit update diffs automatically; add audit retention purge
- working notification retries, per-channel rate limits, log retention
- pluggable virus scanner, upload-size limit, pipeline toggle
- feature rollout %/environment targeting; settings-driven registrations
- workflow guards (instance cap, route depth, self-assignment, clone on re-entry)
- wire every previously-dead INFRASYNTH_* setting; drop truly dead ones

Delivery:
- README + CHANGELOG; CI format check + coverage gate
- keep test media out of the tree; untrack .coverage, __pycache__,
  egg-info, docs/ and invoice artifacts
2026-09-24 10:41:21 -05:00

206 lines
8.5 KiB
Python

# Generated by Django 5.2.17 on 2026-09-24 14:10
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("tenancy", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="InboundEndpoint",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("name", models.CharField(max_length=255)),
("slug", models.SlugField()),
(
"source",
models.CharField(
choices=[
("stripe", "Stripe"),
("github", "GitHub"),
("mercadopago", "Mercado Pago"),
("custom", "Custom"),
],
max_length=50,
),
),
("secret", models.CharField(max_length=500)),
("handler", models.CharField(max_length=500)),
("is_active", models.BooleanField(default=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
],
options={
"db_table": "webhooks_inbound_endpoint",
},
),
migrations.CreateModel(
name="InboundEvent",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("event_type", models.CharField(max_length=255)),
("raw_payload", models.JSONField()),
("is_verified", models.BooleanField(default=False)),
("is_processed", models.BooleanField(default=False)),
("error", models.TextField(blank=True)),
("received_at", models.DateTimeField(auto_now_add=True)),
("processed_at", models.DateTimeField(blank=True, null=True)),
(
"endpoint",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="events",
to="infrasynth_webhooks.inboundendpoint",
),
),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
],
options={
"db_table": "webhooks_inbound_event",
},
),
migrations.CreateModel(
name="OutboundEndpoint",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("name", models.CharField(max_length=255)),
("url", models.URLField(max_length=1000)),
("secret", models.CharField(max_length=500)),
("is_active", models.BooleanField(default=True)),
("retry_policy", models.JSONField(blank=True, default=dict)),
("headers", models.JSONField(blank=True, default=dict)),
("timeout_seconds", models.PositiveIntegerField(default=30)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
],
options={
"db_table": "webhooks_outbound_endpoint",
},
),
migrations.CreateModel(
name="OutboundSubscription",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("event_name", models.CharField(db_index=True, max_length=255)),
("is_active", models.BooleanField(default=True)),
("payload_template", models.TextField(blank=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"endpoint",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="subscriptions",
to="infrasynth_webhooks.outboundendpoint",
),
),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
],
options={
"db_table": "webhooks_outbound_subscription",
},
),
migrations.CreateModel(
name="OutboundDelivery",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("payload", models.JSONField()),
("response_status", models.PositiveSmallIntegerField(blank=True, null=True)),
("response_body", models.TextField(blank=True)),
("attempt", models.PositiveIntegerField(default=0)),
(
"status",
models.CharField(
choices=[("success", "Success"), ("failed", "Failed"), ("retrying", "Retrying")],
default="retrying",
max_length=20,
),
),
("next_retry_at", models.DateTimeField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("completed_at", models.DateTimeField(blank=True, null=True)),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
(
"subscription",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="deliveries",
to="infrasynth_webhooks.outboundsubscription",
),
),
],
options={
"db_table": "webhooks_outbound_delivery",
},
),
migrations.AddConstraint(
model_name="inboundendpoint",
constraint=models.UniqueConstraint(fields=("tenant", "slug"), name="uniq_inbound_endpoint_slug_per_tenant"),
),
migrations.AddIndex(
model_name="inboundevent",
index=models.Index(fields=["tenant_id", "is_processed"], name="webhooks_in_tenant__9d147e_idx"),
),
migrations.AddIndex(
model_name="outboundendpoint",
index=models.Index(fields=["tenant_id", "is_active"], name="webhooks_ou_tenant__2c9365_idx"),
),
migrations.AddConstraint(
model_name="outboundsubscription",
constraint=models.UniqueConstraint(
fields=("endpoint", "event_name"), name="uniq_subscription_event_per_endpoint"
),
),
migrations.AddIndex(
model_name="outbounddelivery",
index=models.Index(fields=["tenant_id", "status"], name="webhooks_ou_tenant__7f1857_idx"),
),
]