import { AfterLoad, BeforeInsert, Column, Entity, Index, ManyToOne, OneToMany, PrimaryGeneratedColumn, RelationId, } from 'typeorm'; import { EntityHelper } from 'src/utils/entity-helper'; import { TeamRole } from 'src/team-roles/entities/team-roles.entity'; import { Team } from 'src/teams/entities/team.entity'; import { User } from 'src/users/entities/user.entity'; import { Transaction } from 'src/transactions/entitites/transaction.entity'; @Entity() export class Player extends EntityHelper { @PrimaryGeneratedColumn() id: number; @Column() firstName: string; @Column() lastName: string; @ManyToOne(() => TeamRole, { eager: true, }) teamRole?: TeamRole | null; @Index('IDX_player_team_id') @ManyToOne(() => Team, { eager: true, }) team: Team; @Column({ type: 'decimal', precision: 10, scale: 2, default: 0 }) balance: number; @Index('IDX_player_user_id') @ManyToOne(() => User, (user) => user.players, { eager: true, }) user?: User | null; @RelationId((player: Player) => player.user) userId?: number | null; @OneToMany(() => Transaction, (transaction) => transaction.player) transactions: Transaction[]; @AfterLoad() updateValue() { this.balance = Number(this.balance); } @Column({ default: true }) active: boolean; @BeforeInsert() prepareData() { if (this.balance == null) { this.balance = 0; } if (this.transactions == null) { this.transactions = []; } } }