Review a generated Ktor order route

from Kotlin backend development
Kotlin 2.4.10 / Ktor 3.5.1 / Java 25 LTS advanced 8 min 4 issues to find

Review this route before it accepts production orders.

Authenticate the tenant, validate an order, create it durably and idempotently, and publish a notification after commit.

kotlin
fun Route.orderRoutes(
    service: OrderService,
    notifications: Notifications,
) {
    post("/orders") {
        val request = call.receive<CreateOrderRequest>()
        val tenantId = call.request.headers["X-Tenant"]!!
        val key = call.request.headers["Idempotency-Key"]

        val existing = service.findByKey(key)
        if (existing != null) return@post call.respond(existing)

        val order = service.create(tenantId, request)
        GlobalScope.launch {
            notifications.send(order)
        }

        call.respond(HttpStatusCode.Created, order)
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error