import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm'; import { GameEntity } from '../game/game.entity'; import { FactionEntity } from '../faction/faction.entity'; import { DataDTO } from './mapdrawing.dto'; /////////////////////////////////////////////////////////////////////////// /// MapDrawingEntity & MapDrawingHistoryEntity reflect database tables. /// /// /// /// MapDrawing ownershipCheck checks users rights to MapDrawing /// /////////////////////////////////////////////////////////////////////////// @Entity('MapDrawing') export class MapDrawingEntity { @PrimaryGeneratedColumn('uuid') mapDrawingId: string; @Column({ type: 'bool', nullable: true }) drawingIsActive: boolean; @Column({ type: 'json', nullable: true }) data: DataDTO; // When Faction or game that has the drawing in question is deleted from // the database, the drawing is also deleted @ManyToOne(type => FactionEntity, faction => faction.mapDrawings, { onDelete: 'CASCADE', }) faction: FactionEntity; @ManyToOne(type => GameEntity, gameEntity => gameEntity.id, { onDelete: 'CASCADE', }) gameId: GameEntity; async ownershipCheck(factionEntity, role) { if (role === 'admin') { return factionEntity == this.faction; } else { return this.faction && factionEntity.factionId === this.faction.factionId ? true : false; } } } @Entity('MapDrawingHistory') export class MapDrawingHistoryEntity { @PrimaryGeneratedColumn('uuid') mapDrawingHistoryId: string; @Column('float') timestamp: number; @Column('bool') drawingIsActive: boolean; @Column('json') data: JSON; // If drawing is deleted, it's histories are deleted also @ManyToOne(() => MapDrawingEntity, mapDrawing => mapDrawing.mapDrawingId, { onDelete: 'CASCADE', }) mapdrawing: string; }