soma.cell

Subscription Lifecycle

Models a SaaS subscription: trial -> active -> cancelled etc.

Example 068 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell Subscription keeps 2 memory slots: subs (Map<String, String>, persistent and consistent) and status (Map<String, String>, persistent and consistent). Its state machine sub_status starts in trial and allows 7 transitions: trial -> active, trial -> cancelled, active -> paused, active -> cancelled, paused -> active, paused -> cancelled and cancelled -> trial. Handlers: create (line 34) and demo (line 41).

Generated from soma describe on this file.

The source

068_subscription.cell

// ============================================================================
// 068 — Subscription Lifecycle
// ============================================================================
// Models a SaaS subscription: trial -> active -> cancelled etc.
//
// Run:   soma run 068_subscription.cell demo
// Expect:
//   Subscription created (trial)
//   Activated!
//   Status: active
//   Paused
//   Resumed
//   Cancelled

cell Subscription {

    memory {
        subs: Map<String, String> [persistent, consistent]
        status: Map<String, String> [persistent, consistent]
    }

    state sub_status {
        initial: trial

        trial -> active
        trial -> cancelled
        active -> paused
        active -> cancelled
        paused -> active
        paused -> cancelled
        cancelled -> trial
    }

    on create(plan: String) {
        let id = to_string(next_id())
        subs.set(id, plan)
        status.set(id, "trial")
        return map("id", id, "plan", plan, "status", "trial")
    }

    on demo() {
        create("Pro")
        print("Subscription created (trial)")

        transition("1", "active")
        print("Activated!")

        let st = get_status("1")
        print("Status: {st}")

        transition("1", "paused")
        print("Paused")

        transition("1", "active")
        print("Resumed")

        transition("1", "cancelled")
        print("Cancelled")

        return "done"
    }
}

What the toolchain says

$ soma check 068_subscription.cell
✓ All checks passed.
$ soma run 068_subscription.cell demo
Subscription created (trial)
Activated!
Status: active
Paused
Resumed
Cancelled
done