Skip to content

Commit

Permalink
refactor(schemas): add types to schemas nested fields (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi authored Jun 17, 2023
1 parent 645ca7e commit 6d4c5b1
Show file tree
Hide file tree
Showing 35 changed files with 850 additions and 720 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { OmitType } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { Expose, Type } from "class-transformer";
import { Player } from "../../../schemas/player/player.schema";
import { MakeGamePlayTargetDto } from "./make-game-play-target.dto";

class MakeGamePlayTargetWithRelationsDto extends OmitType(MakeGamePlayTargetDto, ["playerId"] as const) {
@Type(() => Player)
@Expose()
public player: Player;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { OmitType } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { Expose, Type } from "class-transformer";
import { Player } from "../../../schemas/player/player.schema";
import { MakeGamePlayVoteDto } from "./make-game-play-vote.dto";

class MakeGamePlayVoteWithRelationsDto extends OmitType(MakeGamePlayVoteDto, ["sourceId", "targetId"] as const) {
@Type(() => Player)
@Expose()
public source: Player;

@Type(() => Player)
@Expose()
public target: Player;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MakeGamePlayWithRelationsDto extends OmitType(MakeGamePlayDto, ["targets",
public votes?: MakeGamePlayVoteWithRelationsDto[];

@Expose()
@Type(() => GameAdditionalCard)
public chosenCard?: GameAdditionalCard;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose, Type } from "class-transformer";
import { doesArrayRespectBounds } from "../../../../../shared/validation/helpers/validation.helper";
import { gameHistoryRecordPlaySourceApiProperties, gameHistoryRecordPlaySourceFieldsSpecs } from "../../../constants/game-history-record/game-history-record-play/game-history-record-play-source.constant";
import { GameSource } from "../../../types/game.type";
import type { Player } from "../../player/player.schema";
import { PlayerSchema } from "../../player/player.schema";
import { PlayerSchema, Player } from "../../player/player.schema";

@Schema({
versionKey: false,
Expand All @@ -17,6 +17,7 @@ class GameHistoryRecordPlaySource {
required: gameHistoryRecordPlaySourceFieldsSpecs.name.required,
enum: gameHistoryRecordPlaySourceFieldsSpecs.name.enum,
})
@Expose()
public name: GameSource;

@ApiProperty(gameHistoryRecordPlaySourceApiProperties.players)
Expand All @@ -25,6 +26,8 @@ class GameHistoryRecordPlaySource {
validate: [(players: Player[]): boolean => doesArrayRespectBounds(players, { minItems: gameHistoryRecordPlaySourceFieldsSpecs.players.minItems }), "Path `play.source.players` length is less than minimum allowed value (1)."],
type: [PlayerSchema],
})
@Type(() => Player)
@Expose()
public players: Player[];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose, Type } from "class-transformer";
import { gameHistoryRecordPlayTargetApiProperties, gameHistoryRecordPlayTargetFieldsSpecs } from "../../../constants/game-history-record/game-history-record-play/game-history-record-play-target.constant";
import { WITCH_POTIONS } from "../../../enums/game-play.enum";
import { Player, PlayerSchema } from "../../player/player.schema";
Expand All @@ -15,17 +16,21 @@ class GameHistoryRecordPlayTarget {
required: gameHistoryRecordPlayTargetFieldsSpecs.player.required,
type: PlayerSchema,
})
@Type(() => Player)
@Expose()
public player: Player;

@ApiProperty(gameHistoryRecordPlayTargetApiProperties.isInfected)
@Prop({ required: gameHistoryRecordPlayTargetFieldsSpecs.isInfected.required })
@Expose()
public isInfected?: boolean;

@ApiProperty(gameHistoryRecordPlayTargetApiProperties.drankPotion)
@Prop({
required: gameHistoryRecordPlayTargetFieldsSpecs.drankPotion.required,
enum: gameHistoryRecordPlayTargetFieldsSpecs.drankPotion.enum,
})
@Expose()
public drankPotion?: WITCH_POTIONS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose, Type } from "class-transformer";
import { gameHistoryRecordPlayVoteFieldsSpecs } from "../../../constants/game-history-record/game-history-record-play/game-history-record-play-vote.constant";
import { Player, PlayerSchema } from "../../player/player.schema";

Expand All @@ -14,13 +15,17 @@ class GameHistoryRecordPlayVote {
required: gameHistoryRecordPlayVoteFieldsSpecs.source.required,
type: PlayerSchema,
})
@Type(() => Player)
@Expose()
public source: Player;

@ApiProperty(gameHistoryRecordPlayVoteFieldsSpecs.target)
@Prop({
required: gameHistoryRecordPlayVoteFieldsSpecs.target.required,
type: PlayerSchema,
})
@Type(() => Player)
@Expose()
public target: Player;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { Expose, Type } from "class-transformer";
import { ROLE_SIDES } from "../../../../role/enums/role.enum";
import { gameHistoryRecordPlayApiProperties, gameHistoryRecordPlayFieldsSpecs } from "../../../constants/game-history-record/game-history-record-play/game-history-record-play.constant";
import { GAME_HISTORY_RECORD_VOTING_RESULTS } from "../../../enums/game-history-record.enum";
import { GAME_PLAY_ACTIONS } from "../../../enums/game-play.enum";
import { GameAdditionalCardSchema, GameAdditionalCard } from "../../game-additional-card/game-additional-card.schema";
import { GameHistoryRecordPlaySource, GameHistoryRecordPlaySourceSchema } from "./game-history-record-play-source.schema";
import { GameHistoryRecordPlayTargetSchema } from "./game-history-record-play-target.schema";
import type { GameHistoryRecordPlayTarget } from "./game-history-record-play-target.schema";
import { GameHistoryRecordPlayVoteSchema } from "./game-history-record-play-vote.schema";
import type { GameHistoryRecordPlayVote } from "./game-history-record-play-vote.schema";
import { GameHistoryRecordPlayTargetSchema, GameHistoryRecordPlayTarget } from "./game-history-record-play-target.schema";
import { GameHistoryRecordPlayVoteSchema, GameHistoryRecordPlayVote } from "./game-history-record-play-vote.schema";

@Schema({
versionKey: false,
Expand All @@ -31,6 +29,7 @@ class GameHistoryRecordPlay {
required: gameHistoryRecordPlayFieldsSpecs.source.required,
type: GameHistoryRecordPlaySourceSchema,
})
@Type(() => GameHistoryRecordPlaySource)
@Expose()
public source: GameHistoryRecordPlaySource;

Expand All @@ -40,6 +39,7 @@ class GameHistoryRecordPlay {
type: [GameHistoryRecordPlayTargetSchema],
default: undefined,
})
@Type(() => GameHistoryRecordPlayTarget)
@Expose()
public targets?: GameHistoryRecordPlayTarget[];

Expand All @@ -49,6 +49,7 @@ class GameHistoryRecordPlay {
type: [GameHistoryRecordPlayVoteSchema],
default: undefined,
})
@Type(() => GameHistoryRecordPlayVote)
@Expose()
public votes?: GameHistoryRecordPlayVote[];

Expand All @@ -70,6 +71,7 @@ class GameHistoryRecordPlay {
required: gameHistoryRecordPlayFieldsSpecs.chosenCard.required,
type: GameAdditionalCardSchema,
})
@Type(() => GameAdditionalCard)
@Expose()
public chosenCard?: GameAdditionalCard;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { SchemaTypes, Types } from "mongoose";
import type { HydratedDocument } from "mongoose";
import { gameHistoryRecordApiProperties, gameHistoryRecordFieldsSpecs } from "../../constants/game-history-record/game-history-record.constant";
import { GAME_PHASES } from "../../enums/game.enum";
import type { Player } from "../player/player.schema";
import { PlayerSchema } from "../player/player.schema";
import { PlayerSchema, Player } from "../player/player.schema";
import { GameHistoryRecordPlay, GameHistoryRecordPlaySchema } from "./game-history-record-play/game-history-record-play.schema";

@Schema({
Expand Down Expand Up @@ -54,6 +53,7 @@ class GameHistoryRecord {
required: gameHistoryRecordFieldsSpecs.play.required,
type: GameHistoryRecordPlaySchema,
})
@Type(() => GameHistoryRecordPlay)
@Expose()
public play: GameHistoryRecordPlay;

Expand All @@ -63,6 +63,7 @@ class GameHistoryRecord {
type: [PlayerSchema],
default: undefined,
})
@Type(() => Player)
@Expose()
public revealedPlayers?: Player[];

Expand All @@ -72,6 +73,7 @@ class GameHistoryRecord {
type: [PlayerSchema],
default: undefined,
})
@Type(() => Player)
@Expose()
public deadPlayers?: Player[];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { compositionGameOptionsApiProperties, compositionGameOptionsFieldsSpecs } from "../../constants/game-options/composition-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { compositionGameOptionsApiProperties, compositionGameOptionsFieldsSpecs
class CompositionGameOptions {
@ApiProperty(compositionGameOptionsApiProperties.isHidden)
@Prop({ default: compositionGameOptionsFieldsSpecs.isHidden.default })
@Expose()
public isHidden: boolean;
}

Expand Down
5 changes: 5 additions & 0 deletions src/modules/game/schemas/game-options/game-options.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose, Type } from "class-transformer";
import { gameOptionsApiProperties } from "../../constants/game-options/game-options.constant";
import { CompositionGameOptions, CompositionGameOptionsSchema } from "./composition-game-options.schema";
import { RolesGameOptions, RolesGameOptionsSchema } from "./roles-game-options/roles-game-options.schema";
Expand All @@ -15,13 +16,17 @@ class GameOptions {
type: CompositionGameOptionsSchema,
default: () => ({}),
})
@Type(() => CompositionGameOptions)
@Expose()
public composition: CompositionGameOptions;

@ApiProperty(gameOptionsApiProperties.roles)
@Prop({
type: RolesGameOptionsSchema,
default: () => ({}),
})
@Type(() => RolesGameOptions)
@Expose()
public roles: RolesGameOptions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { ancientGameOptionsApiProperties, ancientGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/ancient-game-options.constant";

@Schema({
Expand All @@ -14,10 +15,12 @@ class AncientGameOptions {
min: ancientGameOptionsFieldsSpecs.livesCountAgainstWerewolves.minimum,
max: ancientGameOptionsFieldsSpecs.livesCountAgainstWerewolves.maximum,
})
@Expose()
public livesCountAgainstWerewolves: number;

@ApiProperty(ancientGameOptionsApiProperties.doesTakeHisRevenge)
@Prop({ default: ancientGameOptionsFieldsSpecs.doesTakeHisRevenge.default })
@Expose()
public doesTakeHisRevenge: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { bearTamerGameOptionsApiProperties, bearTamerGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/bear-tamer-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { bearTamerGameOptionsApiProperties, bearTamerGameOptionsFieldsSpecs } fr
class BearTamerGameOptions {
@ApiProperty(bearTamerGameOptionsApiProperties.doesGrowlIfInfected)
@Prop({ default: bearTamerGameOptionsFieldsSpecs.doesGrowlIfInfected.default })
@Expose()
public doesGrowlIfInfected: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { bigBadWolfGameOptionsApiProperties, bigBadWolfGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/big-bad-wolf-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { bigBadWolfGameOptionsApiProperties, bigBadWolfGameOptionsFieldsSpecs }
class BigBadWolfGameOptions {
@ApiProperty(bigBadWolfGameOptionsApiProperties.isPowerlessIfWerewolfDies)
@Prop({ default: bigBadWolfGameOptionsFieldsSpecs.isPowerlessIfWerewolfDies.default })
@Expose()
public isPowerlessIfWerewolfDies: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { dogWolfGameOptionsApiProperties, dogWolfGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/dog-wolf-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { dogWolfGameOptionsApiProperties, dogWolfGameOptionsFieldsSpecs } from "
class DogWolfGameOptions {
@ApiProperty(dogWolfGameOptionsApiProperties.isChosenSideRevealed)
@Prop({ default: dogWolfGameOptionsFieldsSpecs.isChosenSideRevealed.default })
@Expose()
public isChosenSideRevealed: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { foxGameOptionsApiProperties, foxGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/fox-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { foxGameOptionsApiProperties, foxGameOptionsFieldsSpecs } from "../../..
class FoxGameOptions {
@ApiProperty(foxGameOptionsApiProperties.isPowerlessIfMissesWerewolf)
@Prop({ default: foxGameOptionsFieldsSpecs.isPowerlessIfMissesWerewolf.default })
@Expose()
public isPowerlessIfMissesWerewolf: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { guardGameOptionsApiProperties, guardGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/guard-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { guardGameOptionsApiProperties, guardGameOptionsFieldsSpecs } from "../.
class GuardGameOptions {
@ApiProperty(guardGameOptionsApiProperties.canProtectTwice)
@Prop({ default: guardGameOptionsFieldsSpecs.canProtectTwice.default })
@Expose()
public canProtectTwice: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { idiotGameOptionsApiProperties, idiotGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/idiot-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { idiotGameOptionsApiProperties, idiotGameOptionsFieldsSpecs } from "../.
class IdiotGameOptions {
@ApiProperty(idiotGameOptionsApiProperties.doesDieOnAncientDeath)
@Prop({ default: idiotGameOptionsFieldsSpecs.doesDieOnAncientDeath.default })
@Expose()
public doesDieOnAncientDeath: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { littleGirlGameOptionsApiProperties, littleGirlGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/little-girl-game-options.constant";

@Schema({
Expand All @@ -10,6 +11,7 @@ import { littleGirlGameOptionsApiProperties, littleGirlGameOptionsFieldsSpecs }
class LittleGirlGameOptions {
@ApiProperty(littleGirlGameOptionsApiProperties.isProtectedByGuard)
@Prop({ default: littleGirlGameOptionsFieldsSpecs.isProtectedByGuard.default })
@Expose()
public isProtectedByGuard: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { piedPiperGameOptionsApiProperties, piedPiperGameOptionsFieldsSpecs } from "../../../constants/game-options/roles-game-options/pied-piper-game-options.constant";

@Schema({
Expand All @@ -14,10 +15,12 @@ class PiedPiperGameOptions {
min: piedPiperGameOptionsFieldsSpecs.charmedPeopleCountPerNight.minimum,
max: piedPiperGameOptionsFieldsSpecs.charmedPeopleCountPerNight.maximum,
})
@Expose()
public charmedPeopleCountPerNight: number;

@ApiProperty(piedPiperGameOptionsApiProperties.isPowerlessIfInfected)
@Prop({ default: piedPiperGameOptionsFieldsSpecs.isPowerlessIfInfected.default })
@Expose()
public isPowerlessIfInfected: boolean;
}

Expand Down
Loading

0 comments on commit 6d4c5b1

Please sign in to comment.