Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.5 KB
Raw Blame
"""Credential-safe values for the workspace Resend configuration lifecycle."""

from __future__ import annotations

from dataclasses import dataclass, field

from pydantic import BaseModel, ConfigDict, Field, NaiveDatetime

from core.human_input_v2.entities import EmailProviderType
from core.human_input_v2.shared import (
    AccountId,
    EmailProviderId,
    NormalizedEmail,
    TenantId,
)
from repositories.human_input_v2.channel_credentials import ChannelEncryptedCredentials


class ResendCredentials(BaseModel):
    """Complete authentication credentials stored inside the Channel envelope."""

    model_config = ConfigDict(extra="forbid", frozen=True, strict=True, hide_input_in_errors=True)

    api_key: str = Field(repr=False)


@dataclass(frozen=True, slots=True)
class ResendCandidate:
    """Complete candidate settings for a Resend save or test operation."""

    sender_email: NormalizedEmail
    sender_name: str
    credentials: ResendCredentials = field(repr=False)


@dataclass(frozen=True, slots=True)
class EmailConfigurationSnapshot:
    """Complete internal token guarding a configuration write."""

    configuration_id: EmailProviderId
    config_version: int

    def __post_init__(self) -> None:
        if self.config_version < 1:
            raise ValueError("config version must be positive")


@dataclass(frozen=True, slots=True)
class EmailChannelConfiguration:
    """Workspace-owned Resend configuration independent from ORM lifetime."""

    id: EmailProviderId
    tenant_id: TenantId
    sender_email: NormalizedEmail
    sender_name: str
    encrypted_credentials: ChannelEncryptedCredentials = field(repr=False)
    configured_by_account_id: AccountId | None
    created_at: NaiveDatetime
    updated_at: NaiveDatetime
    config_version: int = 1
    provider: EmailProviderType = EmailProviderType.RESEND

    def __post_init__(self) -> None:
        if self.provider is not EmailProviderType.RESEND:
            raise ValueError("only the Resend email provider is supported")
        if self.config_version < 1:
            raise ValueError("config version must be positive")

    @property
    def snapshot(self) -> EmailConfigurationSnapshot:
        return EmailConfigurationSnapshot(self.id, self.config_version)


@dataclass(frozen=True, slots=True)
class EmailChannelView:
    """Credential-free configuration state exposed by the Email owner."""

    id: EmailProviderId
    provider: EmailProviderType
    created_at: NaiveDatetime
    updated_at: NaiveDatetime
    sender_name: str
    sender_email: str
    revision: EmailConfigurationSnapshot