feat: add join columns
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
BaseEntity,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { Message } from './message.entity';
|
||||
|
||||
@@ -19,7 +20,15 @@ export class Conversation extends BaseEntity {
|
||||
@Column({ type: 'varchar', nullable: true })
|
||||
title!: string | null;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'userId',
|
||||
nullable: false,
|
||||
})
|
||||
userId!: number;
|
||||
|
||||
@ManyToOne(() => User, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'userId' })
|
||||
user!: User;
|
||||
|
||||
@OneToMany(() => Message, (message) => message.conversation)
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
BaseEntity,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { Conversation } from './conversation.entity';
|
||||
import { MessageRole } from '../enums/message-role.enum';
|
||||
@@ -14,9 +15,17 @@ export class Message extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'conversationId',
|
||||
nullable: false,
|
||||
})
|
||||
conversationId!: number;
|
||||
|
||||
@ManyToOne(() => Conversation, (conversation) => conversation.messages, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'conversationId' })
|
||||
conversation!: Conversation;
|
||||
|
||||
@Column({
|
||||
|
||||
@@ -38,14 +38,6 @@ export class CreateQuestionDTO {
|
||||
})
|
||||
content!: string;
|
||||
|
||||
// @IsPositive({
|
||||
// message: t('validation.wrongField', {
|
||||
// field: '$t(questions.fields.order)',
|
||||
// }),
|
||||
// })
|
||||
// @IsOptional()
|
||||
// order?: number;
|
||||
|
||||
@IsPositive({
|
||||
message: t('validation.wrongField', {
|
||||
field: '$t(questions.fields.weight)',
|
||||
|
||||
@@ -28,10 +28,6 @@ export class FindQuestionsDTO extends PartialType(PaginationQueryDTO) {
|
||||
@IsOptional()
|
||||
content?: string;
|
||||
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
questionOrder?: number;
|
||||
|
||||
@IsPositive()
|
||||
@IsOptional()
|
||||
weight?: number;
|
||||
|
||||
@@ -27,14 +27,6 @@ export class UpdateQuestionDTO {
|
||||
@IsOptional()
|
||||
content?: string;
|
||||
|
||||
@IsPositive({
|
||||
message: t('validation.wrongField', {
|
||||
field: '$t(questions.fields.order)',
|
||||
}),
|
||||
})
|
||||
@IsOptional()
|
||||
order?: number;
|
||||
|
||||
@IsPositive({
|
||||
message: t('validation.wrongField', {
|
||||
field: '$t(questions.fields.weight)',
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
BaseEntity,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { Question } from './question.entity';
|
||||
import { UNIQUE_QUESTION_ORDER_IDX } from '../constants';
|
||||
@@ -15,9 +16,17 @@ export class Choice extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'questionId',
|
||||
nullable: false,
|
||||
})
|
||||
questionId!: number;
|
||||
|
||||
@ManyToOne(() => Question, (question) => question.choices, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'questionId' })
|
||||
question!: Question;
|
||||
|
||||
@Column({
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
@@ -12,9 +13,17 @@ export class QuestionRangeRule extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'questionId',
|
||||
nullable: false,
|
||||
})
|
||||
questionId!: number;
|
||||
|
||||
@ManyToOne(() => Question, (question) => question.rangeRules, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'questionId' })
|
||||
question!: Question;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
@@ -18,10 +19,26 @@ export class QuestionTransition extends BaseEntity {
|
||||
@ManyToOne(() => Test, (test) => test.transitions, { onDelete: 'CASCADE' })
|
||||
test!: Test;
|
||||
|
||||
@ManyToOne(() => Question, { nullable: true, onDelete: 'CASCADE' })
|
||||
fromQuestion!: Question | null;
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'fromQuestionId',
|
||||
nullable: true,
|
||||
})
|
||||
fromQuestionId!: number | null;
|
||||
|
||||
@ManyToOne(() => Question, { nullable: true, onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'fromQuestionId' })
|
||||
fromQuestion!: Question | null;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'toQuestionId',
|
||||
nullable: true,
|
||||
})
|
||||
toQuestionId!: number | null;
|
||||
|
||||
@ManyToOne(() => Question, { nullable: true, onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'toQuestionId' })
|
||||
toQuestion!: Question | null;
|
||||
|
||||
@Column({
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
PrimaryGeneratedColumn,
|
||||
@@ -13,12 +12,10 @@ import {
|
||||
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 extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
@@ -46,10 +43,6 @@ export class Question extends BaseEntity {
|
||||
@Column({ type: 'text', nullable: false })
|
||||
content!: string;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
@Index()
|
||||
order!: number;
|
||||
|
||||
@Column({ type: 'int', default: 1 })
|
||||
weight!: number;
|
||||
|
||||
|
||||
@@ -4,6 +4,5 @@ export enum QuestionSortField {
|
||||
TYPE = 'type',
|
||||
CONTENT = 'content',
|
||||
WEIGHT = 'weight',
|
||||
ORDER = 'order',
|
||||
CREATED_AT = 'createdAt',
|
||||
}
|
||||
|
||||
@@ -13,10 +13,6 @@ export const filterQuestions = (query: FindQuestionsDTO) => {
|
||||
where.content = ILike(`%${query.content}%`);
|
||||
}
|
||||
|
||||
if (query.questionOrder !== undefined) {
|
||||
where.order = query.questionOrder;
|
||||
}
|
||||
|
||||
if (query.weight !== undefined) {
|
||||
where.weight = query.weight;
|
||||
}
|
||||
|
||||
@@ -41,22 +41,9 @@ export class QuestionsCommandProvider {
|
||||
public async createOne(createQuestionDto: CreateQuestionDTO) {
|
||||
const test = await this.testsService.adminGetOne(createQuestionDto.test);
|
||||
|
||||
let maxOrder: { max: string } | undefined;
|
||||
|
||||
try {
|
||||
maxOrder = await this.questionsRepository
|
||||
.createQueryBuilder('q')
|
||||
.select('MAX(q.order)', 'max')
|
||||
.where('q.testId = :testId', { testId: test.id })
|
||||
.getRawOne();
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
const newQuestion = this.questionsRepository.create({
|
||||
...createQuestionDto,
|
||||
test,
|
||||
order: (Number(maxOrder?.max) || 0) + 1,
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
@@ -12,9 +13,17 @@ export class ResultRange extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'testId',
|
||||
nullable: false,
|
||||
})
|
||||
testId!: number;
|
||||
|
||||
@ManyToOne(() => Test, (test) => test.resultRanges, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'testId' })
|
||||
test!: Test;
|
||||
|
||||
@Column({
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
BaseEntity,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { Submission } from './submission.entity';
|
||||
import { Question } from '@/modules/questions/entities/question.entity';
|
||||
@@ -22,15 +23,39 @@ export class SubmissionAnswer extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'submissionId',
|
||||
nullable: false,
|
||||
})
|
||||
submissionId!: number;
|
||||
|
||||
@ManyToOne(() => Submission, (submission) => submission.answers, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'submissionId' })
|
||||
submission!: Submission;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'questionId',
|
||||
nullable: false,
|
||||
})
|
||||
questionId!: number;
|
||||
|
||||
@ManyToOne(() => Question, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'questionId' })
|
||||
question!: Question;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'choiceId',
|
||||
nullable: true,
|
||||
})
|
||||
choiceId?: number;
|
||||
|
||||
@ManyToOne(() => Choice, { nullable: true, onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'choiceId' })
|
||||
choice?: Choice;
|
||||
|
||||
@ManyToMany(() => Choice)
|
||||
|
||||
@@ -18,8 +18,15 @@ export class SubmissionResult extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'submissionId',
|
||||
nullable: false,
|
||||
})
|
||||
submissionId!: number;
|
||||
|
||||
@OneToOne(() => Submission, { onDelete: 'CASCADE' })
|
||||
@JoinColumn()
|
||||
@JoinColumn({ name: 'submissionId' })
|
||||
submission!: Submission;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
@@ -28,7 +35,15 @@ export class SubmissionResult extends BaseEntity {
|
||||
@Column({ type: 'int', nullable: false })
|
||||
probability!: number;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'resultRangeId',
|
||||
nullable: true,
|
||||
})
|
||||
resultRangeId?: number;
|
||||
|
||||
@ManyToOne(() => ResultRange, { nullable: true, onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'resultRangeId' })
|
||||
resultRange?: ResultRange;
|
||||
|
||||
@CreateDateColumn()
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
OneToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
BaseEntity,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { SubmissionStatus } from '../enums/submission-status.enum';
|
||||
import { SubmissionAnswer } from './submission-answer.entity';
|
||||
@@ -23,10 +24,26 @@ export class Submission extends BaseEntity {
|
||||
@Column({ type: 'varchar', nullable: true })
|
||||
code!: string;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'userId',
|
||||
nullable: false,
|
||||
})
|
||||
userId!: number;
|
||||
|
||||
@ManyToOne(() => User, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'userId' })
|
||||
user!: User;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'testId',
|
||||
nullable: false,
|
||||
})
|
||||
testId!: number;
|
||||
|
||||
@ManyToOne(() => Test, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'testId' })
|
||||
test!: Test;
|
||||
|
||||
@Column({
|
||||
@@ -36,7 +53,15 @@ export class Submission extends BaseEntity {
|
||||
})
|
||||
status!: SubmissionStatus;
|
||||
|
||||
@Column({
|
||||
type: 'int',
|
||||
name: 'currentQuestionId',
|
||||
nullable: true,
|
||||
})
|
||||
currentQuestionId!: number | null;
|
||||
|
||||
@ManyToOne(() => Question, { onDelete: 'SET NULL', nullable: true })
|
||||
@JoinColumn({ name: 'currentQuestionId' })
|
||||
currentQuestion!: Question | null;
|
||||
|
||||
@OneToMany(() => SubmissionAnswer, (answer) => answer.submission)
|
||||
|
||||
Reference in New Issue
Block a user