Skip to content

Commit 25aa617

Browse files
committed
Add Use Coupon Command
1 parent 9b78b1e commit 25aa617

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

api/command/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './runAttendeeScenario'
44
export * from './initializeAttendee'
55
export * from './deliverPuzzle'
66
export * from './revokePuzzle'
7+
export * from './useCoupon'

api/command/useCoupon.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Command } from '@/core'
2+
3+
export type UseCouponInput = {
4+
token: string
5+
eventId: string
6+
}
7+
8+
export class UseCouponCommand implements Command<UseCouponInput, boolean> {
9+
constructor() {}
10+
11+
public async execute(_input: UseCouponInput): Promise<boolean> {
12+
return true
13+
}
14+
}

worker/controller/useCoupon.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { IRequest } from 'itty-router'
1+
import { IRequest, StatusError } from 'itty-router'
22
import { OpenAPIRoute, OpenAPIRouteSchema } from '@cloudflare/itty-router-openapi'
33
import { Put } from '@worker/router'
44
import { json } from '@worker/utils'
55
import * as schema from '@api/schema'
6+
import * as Command from '@api/command'
67

7-
export type UseCouponRequest = IRequest
8+
export type UseCouponRequest = {
9+
useCoupon: Command.UseCouponCommand
10+
} & IRequest
811

912
@Put('/event/puzzle/coupon')
1013
export class UseCoupon extends OpenAPIRoute {
@@ -24,7 +27,16 @@ export class UseCoupon extends OpenAPIRoute {
2427
},
2528
}
2629

27-
async handle(_request: UseCouponRequest, _env: unknown, _context: unknown) {
30+
async handle(request: UseCouponRequest, _env: unknown, _context: unknown) {
31+
const token = request.query.token as string
32+
const eventId = request.query.event_id as string
33+
34+
const success = await request.useCoupon.execute({ token, eventId })
35+
36+
if (!success) {
37+
throw new StatusError(400, 'Failed to redeem coupon')
38+
}
39+
2840
return json({ status: 'OK' })
2941
}
3042
}

worker/middlewares/command.ts

+2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ export const withCommands = (request: IRequest, env: Env) => {
3131
puzzleStatusRepository,
3232
puzzleStatsRepository
3333
)
34+
const useCoupon = new Command.UseCouponCommand()
3435

3536
Object.assign(request, {
3637
createAnnouncementCommand,
3738
runAttendeeScenario,
3839
initializeAttendeeCommand,
3940
deliverPuzzle,
4041
revokePuzzle,
42+
useCoupon,
4143
})
4244
}

0 commit comments

Comments
 (0)