HawkEx.Billing.Subscription (hawk_ex v0.1.0)

Copy Markdown View Source

Ecto schema for an account's subscription to a plan.

Subscriptions are append-friendly records. When a subscription ends, its status transitions to "canceled" and the record remains in the database. When an account subscribes again, HawkEx creates a new subscription record. This preserves billing history without soft-delete semantics.

Status lifecycle

trialing  active  past_due  canceled
                                   
     

Active statuses

Both "trialing" and "active" grant entitlement access. Use active_statuses/0 when building queries instead of duplicating the list.

One active subscription per account

Enforced by a partial unique index on account_id where status is in active_statuses/0. The HawkEx.Billing context also checks this before inserting a subscription so callers receive a clear error tuple.

Summary

Functions

Statuses that grant entitlement access.

Builds a changeset for creating or updating a subscription.

All lifecycle statuses accepted by the schema.

Functions

active_statuses()

Statuses that grant entitlement access.

changeset(subscription, attrs)

Builds a changeset for creating or updating a subscription.

The schema defaults status to "active". Context functions such as HawkEx.Billing.subscribe/2 set trial and billing period fields for normal subscription creation.

valid_statuses()

All lifecycle statuses accepted by the schema.