infrasynth-backend-kit/infrasynth/notifications/models.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

79 lines
3 KiB
Python

from django.db import models
from django.db.models import Q
from infrasynth.shared.enums import ChannelType
from infrasynth.tenancy.mixins import GlobalOrTenantModel, TenantOwnedModel
class NotificationTemplate(GlobalOrTenantModel):
"""A template. ``tenant IS NULL`` is the platform default; a tenant row overrides it."""
slug = models.SlugField(max_length=100)
name = models.CharField(max_length=200)
channel = models.CharField(max_length=20, choices=ChannelType.choices)
subject_template = models.CharField(max_length=500)
body_template = models.TextField()
is_html = models.BooleanField(default=False)
namespace = models.CharField(max_length=100, default="", blank=True)
metadata = models.JSONField(default=dict)
class Meta:
db_table = "notifications_template"
constraints = [
models.UniqueConstraint(
fields=["tenant", "slug", "namespace"],
name="uniq_template_slug_namespace_per_tenant",
),
models.UniqueConstraint(
fields=["slug", "namespace"],
condition=Q(tenant__isnull=True),
name="uniq_global_template_slug_namespace",
),
]
class NotificationDispatch(TenantOwnedModel):
class Status(models.TextChoices):
PENDING = "pending", "Pending"
SENT = "sent", "Sent"
FAILED = "failed", "Failed"
RETRYING = "retrying", "Retrying"
template = models.ForeignKey(
NotificationTemplate,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="dispatches",
)
recipient = models.CharField(max_length=255)
channel = models.CharField(max_length=20, choices=ChannelType.choices)
subject = models.CharField(max_length=500)
body = models.TextField()
status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
error_message = models.TextField(blank=True, default="")
attempt = models.PositiveSmallIntegerField(default=0)
next_retry_at = models.DateTimeField(null=True, blank=True)
context_snapshot = models.JSONField(default=dict, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
completed_at = models.DateTimeField(null=True, blank=True)
request_id = models.UUIDField(null=True, blank=True)
class Meta:
db_table = "notifications_dispatch"
indexes = [models.Index(fields=["tenant_id", "status"])]
class ChannelConfig(TenantOwnedModel):
slug = models.SlugField(max_length=100)
channel_type = models.CharField(max_length=20, choices=ChannelType.choices)
display_name = models.CharField(max_length=200)
config = models.JSONField(default=dict)
is_active = models.BooleanField(default=True)
priority = models.PositiveSmallIntegerField(default=0)
class Meta:
db_table = "notifications_channel_config"
constraints = [
models.UniqueConstraint(fields=["tenant", "slug"], name="uniq_channel_config_slug_per_tenant"),
]