Skip to content

Commit

Permalink
Add puzzle revoke event to stat
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Oct 11, 2023
1 parent 1713294 commit 7af9486
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/repository/puzzleStats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type D1Database } from '@cloudflare/workers-types'
import { type Class } from '@/core/utils'
import { Repository } from '@/core'
import { Stats, StatEvent, PuzzleStatIncremented } from '@/puzzle'
import { Stats, StatEvent, PuzzleStatIncremented, PuzzleStatDecremented } from '@/puzzle'

type EventSchema = {
id: string
Expand All @@ -14,6 +14,7 @@ type EventSchema = {

const eventConstructors: Record<string, Class<StatEvent>> = {
PuzzleStatIncremented: PuzzleStatIncremented,
PuzzleStatDecremented: PuzzleStatDecremented,
}

export class D1PuzzleStatsRepository implements Repository<Stats> {
Expand Down
19 changes: 19 additions & 0 deletions features/puzzle_dashboard.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,22 @@ Feature: Puzzle Dashboard
]
"""
Then the response status should be 200

Scenario: The event has some revoke event
Given there have some puzzle stat events
| id | type | aggregate_id | version | payload | occurred_at |
| b44845bd-8bd2-428d-ad65-f6a619bf8a96 | PuzzleStatIncremented | SITCON | 0 | { "puzzleName": "=" } | 2023-09-10 20:48:00 |
| 1a76df0e-7b36-48a3-b745-18a0e7f9df92 | PuzzleStatDecremented | SITCON | 1 | { "puzzleName": "=" } | 2023-09-10 20:49:00 |
When I make a GET request to "/event/puzzle/dashboard?event_id=SITCON"
And the response json should be:
"""
[
{
"puzzle": "=", "quantity": 1, "currency": 0
},
{
"puzzle": "total", "quantity": 1, "currency": 0
}
]
"""
Then the response status should be 200
9 changes: 9 additions & 0 deletions src/puzzle/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ export class PuzzleStatIncremented extends StatEvent {
this.puzzleName = puzzleName
}
}

export class PuzzleStatDecremented extends StatEvent {
public readonly puzzleName: string

constructor(id: string, aggregateId: string, occurredAt: Date, puzzleName: string) {
super(id, aggregateId, occurredAt)
this.puzzleName = puzzleName
}
}
18 changes: 17 additions & 1 deletion src/puzzle/stats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AggregateRoot, Replayable, getCurrentTime } from '@/core'
import { StatEvent, PuzzleStatIncremented } from './event'
import { StatEvent, PuzzleStatIncremented, PuzzleStatDecremented } from './event'

export class PuzzleStat {
public readonly name: string
Expand All @@ -24,6 +24,10 @@ export class PuzzleStat {
this._delivered++
this._valid++
}

revoke(): void {
this._valid--
}
}

@Replayable
Expand All @@ -50,6 +54,10 @@ export class Stats extends AggregateRoot<string, StatEvent> {
this.apply(new PuzzleStatIncremented(crypto.randomUUID(), this.id, getCurrentTime(), name))
}

revokePuzzle(name: string): void {
this.apply(new PuzzleStatDecremented(crypto.randomUUID(), this.id, getCurrentTime(), name))
}

private _onPuzzleStatIncremented(event: PuzzleStatIncremented): void {
let puzzle = this._puzzles.get(event.puzzleName)
if (puzzle === undefined) {
Expand All @@ -58,4 +66,12 @@ export class Stats extends AggregateRoot<string, StatEvent> {
}
puzzle.deliver()
}

private _onPuzzleStatDecremented(event: PuzzleStatDecremented): void {
const puzzle = this._puzzles.get(event.puzzleName)
if (puzzle === undefined) {
return
}
puzzle.revoke()
}
}

0 comments on commit 7af9486

Please sign in to comment.