infrasynth-backend-kit/infrasynth/billing/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

321 lines
13 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
import infrasynth.shared.enums
class Migration(migrations.Migration):
initial = True
dependencies = [
("infrasynth_files", "0001_initial"),
("tenancy", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="App",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("slug", models.SlugField(max_length=100, unique=True)),
("name", models.CharField(max_length=200)),
(
"monetization",
models.CharField(choices=infrasynth.shared.enums.MonetizationModel.choices, max_length=20),
),
("is_active", models.BooleanField(default=True)),
("metadata", models.JSONField(default=dict)),
],
options={
"db_table": "billing_app",
},
),
migrations.CreateModel(
name="PaymentGateway",
fields=[
("slug", models.SlugField(max_length=100, primary_key=True, serialize=False)),
("display_name", models.CharField(max_length=200)),
("gateway_class", models.CharField(max_length=500)),
("config", models.JSONField(default=dict)),
("is_active", models.BooleanField(default=True)),
("supported_currencies", models.JSONField(default=list)),
("webhook_secret", models.CharField(blank=True, max_length=500)),
],
options={
"db_table": "billing_gateway",
},
),
migrations.CreateModel(
name="Plan",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("slug", models.SlugField(max_length=100)),
("name", models.CharField(max_length=200)),
("price_amount", models.BigIntegerField(help_text="Minor units (cents). Never a float.")),
("price_currency", models.CharField(default="USD", max_length=3)),
(
"interval",
models.CharField(
choices=infrasynth.shared.enums.PlanInterval.choices,
default=infrasynth.shared.enums.PlanInterval["MONTHLY"],
max_length=20,
),
),
("trial_days", models.PositiveIntegerField(default=0)),
("features", models.JSONField(default=dict)),
("limits", models.JSONField(default=dict)),
("is_active", models.BooleanField(default=True)),
("external_id", models.CharField(blank=True, max_length=200)),
(
"app",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="plans", to="infrasynth_billing.app"
),
),
(
"gateway",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_billing.paymentgateway",
),
),
],
options={
"db_table": "billing_plan",
},
),
migrations.CreateModel(
name="Entitlement",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
(
"status",
models.CharField(
choices=infrasynth.shared.enums.EntitlementStatus.choices,
default=infrasynth.shared.enums.EntitlementStatus["ACTIVE"],
max_length=20,
),
),
("started_at", models.DateTimeField(auto_now_add=True)),
("current_period_end", models.DateTimeField(blank=True, null=True)),
("expires_at", models.DateTimeField(blank=True, null=True)),
("cancel_at_period_end", models.BooleanField(default=False)),
("source", models.CharField(default="manual", max_length=20)),
("metadata", models.JSONField(default=dict)),
(
"app",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="entitlements",
to="infrasynth_billing.app",
),
),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
(
"plan",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_billing.plan",
),
),
],
options={
"db_table": "billing_entitlement",
},
),
migrations.CreateModel(
name="Subscription",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("external_id", models.CharField(blank=True, max_length=255)),
("status", models.CharField(choices=infrasynth.shared.enums.SubscriptionStatus.choices, max_length=20)),
("current_period_start", models.DateTimeField(blank=True, null=True)),
("current_period_end", models.DateTimeField(blank=True, null=True)),
("cancel_at_period_end", models.BooleanField(default=False)),
("cancelled_at", models.DateTimeField(blank=True, null=True)),
("trial_end", models.DateTimeField(blank=True, null=True)),
("metadata", models.JSONField(default=dict)),
(
"entitlement",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="subscriptions",
to="infrasynth_billing.entitlement",
),
),
(
"gateway",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_billing.paymentgateway",
),
),
(
"plan",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_billing.plan",
),
),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
],
options={
"db_table": "billing_subscription",
},
),
migrations.CreateModel(
name="Invoice",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("external_id", models.CharField(blank=True, max_length=255)),
("invoice_number", models.CharField(max_length=100)),
("amount", models.BigIntegerField(help_text="Minor units (cents). Never a float.")),
("currency", models.CharField(default="USD", max_length=3)),
("tax_amount", models.BigIntegerField(default=0)),
("tax_name", models.CharField(blank=True, max_length=100)),
("status", models.CharField(choices=infrasynth.shared.enums.InvoiceStatus.choices, max_length=20)),
("due_date", models.DateTimeField(blank=True, null=True)),
("paid_at", models.DateTimeField(blank=True, null=True)),
("line_items", models.JSONField(default=list)),
("metadata", models.JSONField(default=dict)),
(
"pdf_file",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_files.storedfile",
),
),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
(
"gateway",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_billing.paymentgateway",
),
),
(
"subscription",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="invoices",
to="infrasynth_billing.subscription",
),
),
],
options={
"db_table": "billing_invoice",
},
),
migrations.CreateModel(
name="PaymentTransaction",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("external_id", models.CharField(blank=True, max_length=255)),
("amount", models.BigIntegerField(help_text="Minor units (cents). Never a float.")),
("currency", models.CharField(default="USD", max_length=3)),
("status", models.CharField(blank=True, max_length=50)),
("payment_method", models.CharField(blank=True, max_length=100)),
("metadata", models.JSONField(default=dict)),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"gateway",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="infrasynth_billing.paymentgateway",
),
),
(
"invoice",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="transactions",
to="infrasynth_billing.invoice",
),
),
(
"tenant",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="tenancy.tenant",
),
),
],
options={
"db_table": "billing_transaction",
"indexes": [models.Index(fields=["tenant_id", "status"], name="billing_tra_tenant__7efcbc_idx")],
},
),
migrations.AddConstraint(
model_name="plan",
constraint=models.UniqueConstraint(fields=("app", "slug"), name="uniq_plan_slug_per_app"),
),
migrations.AddConstraint(
model_name="entitlement",
constraint=models.UniqueConstraint(fields=("tenant", "app"), name="uniq_tenant_app_entitlement"),
),
migrations.AddIndex(
model_name="subscription",
index=models.Index(fields=["tenant_id", "status"], name="billing_sub_tenant__d36cf8_idx"),
),
migrations.AddConstraint(
model_name="invoice",
constraint=models.UniqueConstraint(
fields=("tenant", "invoice_number"), name="uniq_invoice_number_per_tenant"
),
),
]