Skip to content

Commit

Permalink
Add command error when puzzle receiver not found
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Nov 5, 2023
1 parent 59a60ec commit e5548cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
7 changes: 3 additions & 4 deletions api/command/deliverPuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type DeliverPuzzleOutput = {
attendeeName: string
}

export class PuzzleReceiverNotFoundError extends Error {}

export class DeliverPuzzleCommand implements Command<DeliverPuzzleInput, DeliverPuzzleOutput> {
private readonly attendees: Repository<Attendee>

Expand All @@ -20,10 +22,7 @@ export class DeliverPuzzleCommand implements Command<DeliverPuzzleInput, Deliver
async execute(input: DeliverPuzzleInput): Promise<DeliverPuzzleOutput> {
const attendee = await this.attendees.findById(input.token)
if (!attendee) {
return {
success: false,
attendeeName: '',
}
throw new PuzzleReceiverNotFoundError()
}

return {
Expand Down
10 changes: 7 additions & 3 deletions features/puzzle_delivery.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,21 @@ Feature: Puzzle Delivery
"message": "token and receiver required"
}
"""

Scenario: POST /event/puzzle/deliver with nonexistent receiver token
When I make a POST request to "/event/puzzle/deliver?token=f185f505-d8c0-43ce-9e7b-bb9e8909072d":
Given there have some booths
| token | name | event_id |
| 1024914b-ee65-4728-b687-8341f5affa89 | COSCUP | SITCON |
When I make a POST request to "/event/puzzle/deliver?token=1024914b-ee65-4728-b687-8341f5affa89":
"""
{
"receiver": "1cf41a53-4aea-452b-a204-6b0b52eee380"
}
"""
Then the response status should be 404
And the response json should be:
Then the response json should be:
"""
{
"message": "invalid receiver token"
}
"""
And the response status should be 404
29 changes: 16 additions & 13 deletions worker/controller/puzzleDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,27 @@ export class PuzzleDelivery {
throw new StatusError(400, 'token and receiver required')
}

const receiver = await request.attendeeInfo.execute({ token: receiverToken })
if (!receiver) {
throw new StatusError(404, 'invalid receiver token')
}

const booth = await request.getBoothByToken.execute({ token: String(boothToken) })
if (!booth) {
throw new StatusError(400, 'invalid token')
}

const result = await request.deliverPuzzle.execute({
token: receiverToken,
})

return json<schema.PuzzleDeliveredResponse>({
status: 'OK',
user_id: result.attendeeName,
})
try {
const result = await request.deliverPuzzle.execute({
token: receiverToken,
})

return json<schema.PuzzleDeliveredResponse>({
status: 'OK',
user_id: result.attendeeName,
})
} catch (e: unknown) {
if (e instanceof Command.PuzzleReceiverNotFoundError) {
throw new StatusError(404, 'invalid receiver token')
}

throw e
}
}
}

Expand Down

0 comments on commit e5548cd

Please sign in to comment.