|
| 1 | +import { Entity, Enum, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core' |
| 2 | +import Game from './game' |
| 3 | +import User from './user' |
| 4 | + |
| 5 | +export enum GameActivityType { |
| 6 | + PLAYER_PROPS_UPDATED, |
| 7 | + LEADERBOARD_CREATED, |
| 8 | + LEADERBOARD_UPDATED, |
| 9 | + LEADERBOARD_DELETED, |
| 10 | + LEADERBOARD_ENTRY_HIDDEN, |
| 11 | + LEADERBOARD_ENTRY_RESTORED, |
| 12 | + API_KEY_CREATED, |
| 13 | + API_KEY_REVOKED |
| 14 | +} |
| 15 | + |
| 16 | +@Entity() |
| 17 | +export default class GameActivity { |
| 18 | + @PrimaryKey() |
| 19 | + id: number |
| 20 | + |
| 21 | + @ManyToOne(() => Game) |
| 22 | + game: Game |
| 23 | + |
| 24 | + @ManyToOne(() => User) |
| 25 | + user: User |
| 26 | + |
| 27 | + @Enum(() => GameActivityType) |
| 28 | + type: GameActivityType |
| 29 | + |
| 30 | + @Property({ type: 'json' }) |
| 31 | + extra: { |
| 32 | + [key: string]: unknown, |
| 33 | + displayable?: { |
| 34 | + [key: string]: string |
| 35 | + } |
| 36 | + } = {} |
| 37 | + |
| 38 | + @Property() |
| 39 | + createdAt: Date = new Date() |
| 40 | + |
| 41 | + constructor(game: Game, user: User) { |
| 42 | + this.game = game |
| 43 | + this.user = user |
| 44 | + } |
| 45 | + |
| 46 | + /* istanbul ignore next */ |
| 47 | + private getActivity(): string { |
| 48 | + switch (this.type) { |
| 49 | + case GameActivityType.PLAYER_PROPS_UPDATED: |
| 50 | + return `${this.user.email} updated a player's props` |
| 51 | + case GameActivityType.LEADERBOARD_CREATED: |
| 52 | + return `${this.user.email} created the leaderboard ${this.extra.leaderboardInternalName}` |
| 53 | + case GameActivityType.LEADERBOARD_UPDATED: |
| 54 | + return `${this.user.email} updated the leaderboard ${this.extra.leaderboardInternalName}` |
| 55 | + case GameActivityType.LEADERBOARD_DELETED: |
| 56 | + return `${this.user.email} deleted the leaderboard ${this.extra.leaderboardInternalName}` |
| 57 | + case GameActivityType.LEADERBOARD_ENTRY_HIDDEN: |
| 58 | + return `${this.user.email} hid a leaderboard entry in ${this.extra.leaderboardInternalName}` |
| 59 | + case GameActivityType.LEADERBOARD_ENTRY_RESTORED: |
| 60 | + return `${this.user.email} restored a leaderboard entry in ${this.extra.leaderboardInternalName}` |
| 61 | + case GameActivityType.API_KEY_CREATED: |
| 62 | + return `${this.user.email} created an access key` |
| 63 | + case GameActivityType.API_KEY_REVOKED: |
| 64 | + return `${this.user.email} remove an access key` |
| 65 | + default: |
| 66 | + return '' |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + toJSON() { |
| 71 | + return { |
| 72 | + id: this.id, |
| 73 | + type: this.type, |
| 74 | + description: this.getActivity(), |
| 75 | + extra: this.extra.display, |
| 76 | + createdAt: this.createdAt |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments