import { Test } from '@/modules/tests/entities/test.entity'; import { Column, CreateDateColumn, Entity, Index, ManyToOne, OneToMany, PrimaryGeneratedColumn, } from 'typeorm'; import { QuestionType } from '../enums/question-type.enum'; import type { QuestionMetadata } from '../types/question-metadata.type'; import { Choice } from './choice.entity'; import { UNIQUE_TEST_ORDER_IDX } from '../constants'; import { QuestionRangeRule } from './question-range-rule.entity'; import { QuestionTransition } from './question-transition.entity'; @Entity() @Index(UNIQUE_TEST_ORDER_IDX, ['test', 'order'], { unique: true }) export class Question { @PrimaryGeneratedColumn() id!: number; @ManyToOne(() => Test, (test) => test.questions, { onDelete: 'CASCADE', }) test!: Test; @Column({ type: 'enum', enum: QuestionType, default: QuestionType.SINGLE_CHOICE, }) type!: QuestionType; @Column({ type: 'text', nullable: false }) content!: string; @Column({ type: 'int', nullable: false }) @Index() order!: number; @Column({ type: 'int', default: 1 }) weight!: number; @Column({ type: 'boolean', default: true }) required!: boolean; @OneToMany(() => QuestionRangeRule, (rangeRule) => rangeRule.question) rangeRules?: Array; @Column({ type: 'jsonb', nullable: true }) metadata!: QuestionMetadata; @OneToMany(() => Choice, (choice) => choice.question) choices!: Array; @OneToMany(() => QuestionTransition, (transition) => transition.fromQuestion) outgoingTransitions!: Array; @OneToMany(() => QuestionTransition, (transition) => transition.toQuestion) incomingTransitions!: Array; @CreateDateColumn() createdAt!: Date; }