Spectre.Operation.Delivery.Consent (Spectre v0.3.0)

Copy Markdown View Source

Revocable, expiring consent for proactive delivery to one destination.

Summary

Functions

Returns true when the consent is neither revoked nor expired at now (milliseconds); false otherwise, including for a non-integer now.

Builds a validated consent from a struct, map or keyword list.

Revokes the consent at at (milliseconds), returning the updated consent.

Validates the consent's identity, timestamps, channels and portability.

Types

t()

@type t() :: %Spectre.Operation.Delivery.Consent{
  channels: term(),
  destination: term(),
  expires_at: term(),
  granted_at: term(),
  id: term(),
  metadata: term(),
  origin: term(),
  revoked_at: term(),
  subject_id: term()
}

Functions

active?(consent, now)

@spec active?(t(), non_neg_integer()) :: boolean()

Returns true when the consent is neither revoked nor expired at now (milliseconds); false otherwise, including for a non-integer now.

new(consent)

@spec new(t() | map() | keyword()) :: t()

Builds a validated consent from a struct, map or keyword list.

Missing id and granted_at default to a fresh UUIDv7 and the current time. Raises ArgumentError when the resulting consent is invalid.

revoke(consent, at \\ System.system_time(:millisecond))

@spec revoke(t(), non_neg_integer()) :: t()

Revokes the consent at at (milliseconds), returning the updated consent.

Idempotent: an already revoked consent is returned unchanged. Raises ArgumentError when the revocation time precedes the grant time.

validate(consent)

@spec validate(t()) :: :ok | {:error, term()}

Validates the consent's identity, timestamps, channels and portability.

Returns :ok, or {:error, reason} naming the first invalid field.