Skip to content

Commit

Permalink
Refactor use scenario api to OpenAPI route
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Nov 23, 2023
1 parent 3102069 commit f179554
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions api/schema/scenario.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { LocalizedText } from './localized'
import { AttendeeMetadata } from './attendee'
import { Path, Str } from '@cloudflare/itty-router-openapi'

export const ScenarioIdPath = Path(Str, { description: 'scenarion name to apply' })

export type Scenario = {
order: number
Expand Down
18 changes: 14 additions & 4 deletions worker/controller/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as schema from '@api/schema'
import { RunAttendeeScenario } from '@api/command'
import { AttendeeInfo } from '@api/query'
import { datetimeToUnix } from '@api/utils'
import { get } from '@worker/router'
import { Get } from '@worker/router'
import { OpenAPIRoute, OpenAPIRouteSchema } from '@cloudflare/itty-router-openapi'

export type UseRequest = {
attendeeInfo: AttendeeInfo
Expand All @@ -31,9 +32,18 @@ function formatScenario(scenario: Record<string, any>) {
return result
}

export class UseController {
@get('/use/:scenarioId')
async use({ attendeeInfo, runAttendeeScenario, scenarioId, query }: UseRequest) {
@Get('/use/:scenarioId')
export class ApplyScenario extends OpenAPIRoute {
static schema: OpenAPIRouteSchema = {
description: 'Use scenario',
tags: ['Event'],
parameters: {
token: schema.OptionalTokenQuery,
scenarioId: schema.ScenarioIdPath,
},
}

async handle({ attendeeInfo, runAttendeeScenario, scenarioId, query }: UseRequest) {
if (!query.token) {
throw new StatusError(400, 'token required')
}
Expand Down
1 change: 0 additions & 1 deletion worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const router = OpenAPIRouter({
router.all('*', withParams).all('*', withCommands).all('*', withQueries).all('*', withTestability)

const _Controllers = [
Controller.UseController,
Controller.PuzzleStatus,
Controller.PuzzleDeliverer,
Controller.PuzzleDashboard,
Expand Down

0 comments on commit f179554

Please sign in to comment.