import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm'; import { OrganizationAggregate } from '../Organization/OrganizationAggregate'; import { UserAggregate } from '../User/UserAggregate'; import { logError } from '../../Application/Services/Logger'; export enum Type { LUCK = 0, JOKER = 1, QUESTION = 2 } export enum CType { PUBLIC = 0, PRIVATE = 1, ORGANIZATION = 2 } export enum State { ACTIVE = 0, SOFT_DELETE = 1 } export enum CardType { QUIZ = 0, SENTENCE_PAIRING = 1, OWN_ANSWER = 2, TRUE_FALSE = 3, CLOSER = 4 } export enum ConsequenceType { MOVE_FORWARD = 0, MOVE_BACKWARD = 1, LOSE_TURN = 2, EXTRA_TURN = 3, GO_TO_START = 5 } export interface Consequence { type: ConsequenceType; value?: number; } export interface Card { text: string; type?: CardType; answer?: string | null; consequence?: Consequence | null; } @Entity('Decks') export class DeckAggregate { @PrimaryGeneratedColumn('uuid') id!: string; @Column({ type: 'varchar', length: 255 }) name!: string; @Column({ type: 'int'}) type!: Type; @Column({ type: 'uuid', name: 'user_id' }) userid!: string; @CreateDateColumn({ name: 'creation_date' }) creationdate!: Date; @Column({ type: 'json' }) cards!: Card[]; @Column({ type: 'int', default: 0, name: 'played_number' }) playedNumber!: number; @Column({ type: 'int', default: CType.PUBLIC }) ctype!: CType; @UpdateDateColumn() updateDate!: Date; @Column({ type: 'int', default: State.ACTIVE }) state!: State; @ManyToOne(() => OrganizationAggregate, { nullable: true }) @JoinColumn({ name: 'organization_id' }) organization!: OrganizationAggregate | null; @ManyToOne(() => UserAggregate, { eager: false }) @JoinColumn({ name: 'user_id' }) user!: UserAggregate | null; isEditable() { // A deck is editable if the user is the creator return (userId: string) => { return this.user?.id.toString() === userId; }; } }