soma.cell

Ticket Tracking System

A support ticket system with state machine for ticket lifecycle.

Example 062 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 TicketSystem keeps 3 memory slots: tickets (Map<String, String>, persistent and consistent), assignees (Map<String, String>, persistent and consistent) and status (Map<String, String>, persistent and consistent). Its state machine ticket_status starts in open and allows 8 transitions: open -> in_progress, in_progress -> resolved, in_progress -> blocked, blocked -> in_progress, resolved -> closed, resolved -> reopened, reopened -> in_progress and * -> cancelled. cancelled and closed are terminal. Handlers: create (line 37), assign (line 45) and demo (line 50).

Generated from soma describe on this file.

The source

062_ticket_system.cell

// ============================================================================
// 062 — Ticket Tracking System
// ============================================================================
// A support ticket system with state machine for ticket lifecycle.
//
// Run:   soma run 062_ticket_system.cell demo
// Expect:
//   Created ticket: Bug in login page
//   Status: open
//   Assigned to: Alice
//   Moved to: in_progress
//   Resolved!
//   Final status: resolved

cell TicketSystem {

    memory {
        // Separate maps: one for data, one for state tracking
        tickets: Map<String, String> [persistent, consistent]
        assignees: Map<String, String> [persistent, consistent]
        status: Map<String, String> [persistent, consistent]
    }

    state ticket_status {
        initial: open

        open -> in_progress
        in_progress -> resolved
        in_progress -> blocked
        blocked -> in_progress
        resolved -> closed
        resolved -> reopened
        reopened -> in_progress
        * -> cancelled
    }

    on create(title: String, priority: String) {
        let id = to_string(next_id())
        tickets.set(id, title)
        // IMPORTANT: set initial state in the status map
        status.set(id, "open")
        return map("id", id, "title", title, "priority", priority)
    }

    on assign(id: String, person: String) {
        assignees.set(id, person)
        return map("id", id, "assignee", person)
    }

    on demo() {
        let ticket = create("Bug in login page", "high")
        print("Created ticket: {ticket.title}")

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

        assign("1", "Alice")
        print("Assigned to: Alice")

        transition("1", "in_progress")
        print("Moved to: in_progress")

        transition("1", "resolved")
        print("Resolved!")

        let final_st = get_status("1")
        print("Final status: {final_st}")

        return "done"
    }
}

What the toolchain says

$ soma check 062_ticket_system.cell
✓ All checks passed.
$ soma run 062_ticket_system.cell demo
Created ticket: Bug in login page
Status: open
Assigned to: Alice
Moved to: in_progress
Resolved!
Final status: resolved
done