From 64796fcdf7eedc1f1d129560dee289fc586f9c50 Mon Sep 17 00:00:00 2001 From: OkaykOrhmn Date: Wed, 23 Jul 2025 15:11:27 +0330 Subject: [PATCH] replaced relations with foreign keys --- src/answer/entity/answer.entity.ts | 14 +-- src/attachment/dto/create_attachment.dto.ts | 3 +- src/attachment/entity/attachment.entity.ts | 18 +--- src/form/dto/create-form.dto.ts | 38 ++++---- src/form/entity/form.entity.ts | 32 +++---- src/formPage/dto/create-formPage.dto.ts | 10 +- src/formPage/entity/formPage.entity.ts | 15 +-- src/formResult/entity/formResult.entity.ts | 7 +- src/formResult/formResult.controller.ts | 7 ++ src/formResult/formResult.module.ts | 7 +- src/formResult/formResult.service.ts | 95 +++++++++++++++++++ src/formSection/dto/create-formSection.dto.ts | 36 ++++--- src/formSection/entity/formSection.entity.ts | 27 ++---- src/option/entity/option.entity.ts | 5 - src/participant/dto/create-participant.dto.ts | 7 +- src/participant/entity/participant.entity.ts | 12 +-- .../dto/create-participantGroup.dto.ts | 4 +- .../entity/participantGroup.entity.ts | 3 - src/question/dto/create-question.dto.ts | 17 ++-- src/question/entity/question.entity.ts | 14 +-- src/realm/dto/create-realm.dto.ts | 28 +++--- src/realm/entity/realm.entity.ts | 20 ++-- 22 files changed, 218 insertions(+), 201 deletions(-) diff --git a/src/answer/entity/answer.entity.ts b/src/answer/entity/answer.entity.ts index a09a292..6b80d90 100644 --- a/src/answer/entity/answer.entity.ts +++ b/src/answer/entity/answer.entity.ts @@ -1,22 +1,14 @@ -import { Column, Entity, ManyToOne } from 'typeorm'; +import { Column, Entity } from 'typeorm'; import { BaseEntity } from 'src/_core/entity/_base.entity'; -import { Question } from '../../question/entity/question.entity'; @Entity({ name: 'answers' }) export class Answer extends BaseEntity { - @Column({ - type: 'uuid', - }) + @Column({ type: 'uuid' }) participantId: string; - @Column({ - type: 'uuid', - }) + @Column({ type: 'uuid' }) questionId: string; @Column() value: string; - - @ManyToOne(() => Question, question => question.answers) - question: Question; } \ No newline at end of file diff --git a/src/attachment/dto/create_attachment.dto.ts b/src/attachment/dto/create_attachment.dto.ts index 1c2333c..dec3faf 100644 --- a/src/attachment/dto/create_attachment.dto.ts +++ b/src/attachment/dto/create_attachment.dto.ts @@ -16,6 +16,5 @@ export class CreateAttachmentDto extends BaseDto { storageUrl: string; @IsUUID() - @IsOptional() - ownerId?: string; + ownerId: string; } \ No newline at end of file diff --git a/src/attachment/entity/attachment.entity.ts b/src/attachment/entity/attachment.entity.ts index f707267..910e134 100644 --- a/src/attachment/entity/attachment.entity.ts +++ b/src/attachment/entity/attachment.entity.ts @@ -1,8 +1,5 @@ -import { Column, Entity, ManyToOne } from 'typeorm'; +import { Column, Entity } from 'typeorm'; import { BaseEntity } from 'src/_core/entity/_base.entity'; -import { Question } from 'src/question/entity/question.entity'; -import { FormSection } from '../../formSection/entity/formSection.entity'; -import { Form } from '../../form/entity/form.entity'; @Entity({ name: 'attachments' }) export class Attachment extends BaseEntity { @@ -21,17 +18,6 @@ export class Attachment extends BaseEntity { @Column() storageUrl: string; - @Column({ - type: 'uuid', - }) + @Column({ type: 'uuid' }) ownerId: string; - - @ManyToOne(() => Question, (question) => question.attachments) - question: Question; - - @ManyToOne(() => FormSection, formSection => formSection.attachments) - formSection: FormSection; - - @ManyToOne(() => Form, form => form.attachments) - form: Form; } \ No newline at end of file diff --git a/src/form/dto/create-form.dto.ts b/src/form/dto/create-form.dto.ts index a54d40c..39d4824 100644 --- a/src/form/dto/create-form.dto.ts +++ b/src/form/dto/create-form.dto.ts @@ -1,9 +1,5 @@ -import { IsString, IsOptional, ValidateNested, IsArray } from 'class-validator'; -import { Type } from 'class-transformer'; +import { IsString, IsOptional, IsArray, IsUUID, IsBoolean } from 'class-validator'; import { BaseDto } from '../../_core/dto/base.dto'; -import { CreateFormPageDto } from '../../formPage/dto/create-formPage.dto'; -import { CreateParticipantDto } from '../../participant/dto/create-participant.dto'; -import { CreateAttachmentDto } from '../../attachment/dto/create_attachment.dto'; export class CreateFormDto extends BaseDto { @IsString() @@ -12,21 +8,25 @@ export class CreateFormDto extends BaseDto { @IsString() description: string; - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateAttachmentDto) - @IsOptional() - attachments?: CreateAttachmentDto[]; // Keep if attachments are still part of the DTO + @IsBoolean() + isEnded: boolean; - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateFormPageDto) @IsOptional() - pages?: CreateFormPageDto[]; + @IsArray() + @IsUUID('4', { each: true }) + pageIds?: string[]; - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateParticipantDto) @IsOptional() - participants?: CreateParticipantDto[]; -} + @IsArray() + @IsUUID('4', { each: true }) + participantIds?: string[]; + + @IsOptional() + @IsArray() + @IsUUID('4', { each: true }) + attachmentIds?: string[]; + + @IsOptional() + @IsUUID('4') + formResultId?: string; +} \ No newline at end of file diff --git a/src/form/entity/form.entity.ts b/src/form/entity/form.entity.ts index d665702..a21ef51 100644 --- a/src/form/entity/form.entity.ts +++ b/src/form/entity/form.entity.ts @@ -1,11 +1,7 @@ -import { Entity, Column, OneToMany, ManyToOne } from 'typeorm'; +import { Entity, Column } from 'typeorm'; import { BaseEntity } from '../../_core/entity/_base.entity'; -import { FormPage } from '../../formPage/entity/formPage.entity'; -import { Participant } from '../../participant/entity/participant.entity'; -import { Realm } from '../../realm/entity/realm.entity'; -import { Attachment } from '../../attachment/entity/attachment.entity'; -@Entity() +@Entity({ name: 'forms' }) export class Form extends BaseEntity { @Column() title: string; @@ -13,20 +9,18 @@ export class Form extends BaseEntity { @Column() description: string; - // One-to-Many relationship with FormPage - @OneToMany(() => FormPage, formPage => formPage.form, { cascade: true, eager: true }) - pages: FormPage[]; + @Column({ default: false }) + isEnded: boolean; - // One-to-Many relationship with Participant - @OneToMany(() => Participant, participant => participant.form, { cascade: true, eager: true }) - participants: Participant[]; + @Column('simple-array', { nullable: true }) + pageIds: string[]; + @Column('simple-array', { nullable: true }) + participantIds: string[]; - // One-to-Many relationship with Attachment - @OneToMany(() => Attachment, attachment => attachment.form, { cascade: true, eager: true }) - attachments: Attachment[]; + @Column('simple-array', { nullable: true }) + attachmentIds: string[]; - @ManyToOne(() => Realm, realm => realm.forms, { /*cascade: true, eager: true */}) - realm: Realm; - -} + @Column({ type: 'uuid', nullable: true }) + formResultId: string; +} \ No newline at end of file diff --git a/src/formPage/dto/create-formPage.dto.ts b/src/formPage/dto/create-formPage.dto.ts index 6fe8bf2..7c61c4d 100644 --- a/src/formPage/dto/create-formPage.dto.ts +++ b/src/formPage/dto/create-formPage.dto.ts @@ -1,7 +1,5 @@ -import { IsString, ValidateNested, IsArray } from 'class-validator'; -import { Type } from 'class-transformer'; +import { IsString, IsArray, IsOptional, IsUUID } from 'class-validator'; import { BaseDto } from '../../_core/dto/base.dto'; -import { CreateFormSectionDto } from '../../formSection/dto/create-formSection.dto'; export class CreateFormPageDto extends BaseDto { @IsString() @@ -10,8 +8,8 @@ export class CreateFormPageDto extends BaseDto { @IsString() description: string; + @IsOptional() @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateFormSectionDto) - formSections: CreateFormSectionDto[]; + @IsUUID('4', { each: true }) + formSectionIds?: string[]; } diff --git a/src/formPage/entity/formPage.entity.ts b/src/formPage/entity/formPage.entity.ts index cb22a7a..246a26c 100644 --- a/src/formPage/entity/formPage.entity.ts +++ b/src/formPage/entity/formPage.entity.ts @@ -1,7 +1,6 @@ -import { Entity, Column, OneToMany, ManyToOne } from 'typeorm'; +import { Entity, Column } from 'typeorm'; import { BaseEntity } from '../../_core/entity/_base.entity'; -import { FormSection } from '../../formSection/entity/formSection.entity'; -import { Form } from '../../form/entity/form.entity'; + @Entity() export class FormPage extends BaseEntity { @@ -11,12 +10,6 @@ export class FormPage extends BaseEntity { @Column() description: string; - // One-to-Many relationship with FormSection - @OneToMany(() => FormSection, formSection => formSection.formPage, { cascade: true, eager: true }) - formSections: FormSection[]; - - // Many-to-One relationship with Form - @ManyToOne(() => Form, form => form.pages) // Ensure 'pages' matches the property name in Form entity - form: Form; - + @Column('simple-array', { nullable: true }) + formSectionIds: string[]; } diff --git a/src/formResult/entity/formResult.entity.ts b/src/formResult/entity/formResult.entity.ts index 6233f7d..5e5658e 100644 --- a/src/formResult/entity/formResult.entity.ts +++ b/src/formResult/entity/formResult.entity.ts @@ -1,5 +1,7 @@ -import { Entity, Column } from 'typeorm'; +import { Entity, Column, ManyToOne, OneToOne } from 'typeorm'; import { BaseEntity } from '../../_core/entity/_base.entity'; +import { Realm } from '../../realm/entity/realm.entity'; +import { Form } from '../../form/entity/form.entity'; // Options class (embedded) export class Options { @@ -24,6 +26,9 @@ export class Opinion { @Entity() export class FormResult extends BaseEntity { + @OneToOne(() => Form, form => form.formResult, { /*cascade: true, eager: true */}) + form: Form; + @Column({ type: 'int', default: 0 }) numParticipants: number; diff --git a/src/formResult/formResult.controller.ts b/src/formResult/formResult.controller.ts index 71b5247..315706a 100644 --- a/src/formResult/formResult.controller.ts +++ b/src/formResult/formResult.controller.ts @@ -43,4 +43,11 @@ export class FormResultController { async remove(@Param('id', new ParseUUIDPipe()) id: string): Promise { return this.formResultService.remove(id); } + + @Get('form/:formId/statistics') + async getFormStatistics( + @Param('formId', new ParseUUIDPipe()) formId: string, + ): Promise { + return this.formResultService.getFormStatistics(formId); + } } \ No newline at end of file diff --git a/src/formResult/formResult.module.ts b/src/formResult/formResult.module.ts index c9d9a29..3ec99d5 100644 --- a/src/formResult/formResult.module.ts +++ b/src/formResult/formResult.module.ts @@ -3,12 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { FormResultService } from './formResult.service'; import { FormResultController } from './formResult.controller'; import { FormResult } from './entity/formResult.entity'; +import { Form } from '../form/entity/form.entity'; +import { Question } from '../question/entity/question.entity'; +import { Answer } from '../answer/entity/answer.entity'; @Module({ imports: [ - TypeOrmModule.forFeature([ - FormResult, - ]), + TypeOrmModule.forFeature([FormResult, Form, Question, Answer]), ], controllers: [FormResultController], providers: [FormResultService], diff --git a/src/formResult/formResult.service.ts b/src/formResult/formResult.service.ts index 374cdf9..f19e57b 100644 --- a/src/formResult/formResult.service.ts +++ b/src/formResult/formResult.service.ts @@ -4,12 +4,21 @@ import { Repository } from 'typeorm'; import { FormResult } from './entity/formResult.entity'; import { CreateFormResultDto } from './dto/create-formResult.dto'; import { UpdateFormResultDto } from './dto/update-formResult.dto'; +import { Answer } from 'src/answer/entity/answer.entity'; +import { Question } from '../question/entity/question.entity'; +import { Form } from '../form/entity/form.entity'; @Injectable() export class FormResultService { constructor( + @InjectRepository(Form) + private readonly formRepository: Repository
, @InjectRepository(FormResult) private readonly formResultRepo: Repository, + @InjectRepository(Question) + private readonly questionRepository: Repository, + @InjectRepository(Answer) + private readonly answerRepository: Repository, ) {} async create(data: CreateFormResultDto): Promise { @@ -85,4 +94,90 @@ export class FormResultService { throw new NotFoundException(`FormResult with ID "${id}" not found`); } } + + async getFormStatistics(formId: string): Promise { + // 1. Find the Form entity + const form = await this.formRepository.findOne({ where: { id: formId } }); + if (!form) { + throw new NotFoundException(`Form with ID "${formId}" not found`); + } + + // 2. Compute numQuestions + const numQuestions = await this.questionRepository.count({ + where: { + formSection: { + formPage: { + form: { id: formId }, + }, + }, + }, + }); + + // 3. Compute numParticipants + const numParticipantsResult = await this.answerRepository + .createQueryBuilder('answer') + .select('COUNT(DISTINCT answer.participantId)', 'count') + .innerJoin('answer.question', 'question') + .innerJoin('question.formSection', 'formSection') + .innerJoin('formSection.formPage', 'formPage') + .where('formPage.formId = :formId', { formId }) + .getRawOne(); + const numParticipants = numParticipantsResult ? Number(numParticipantsResult.count) : 0; + + // 4. Compute numAnswers + const numAnswers = await this.answerRepository.count({ + where: { + question: { + formSection: { + formPage: { + form: { id: formId }, + }, + }, + }, + }, + }); + + // 5. Compute numCompleteParticipants + const subQuery = this.answerRepository + .createQueryBuilder('answer') + .select('answer.participantId') + .innerJoin('answer.question', 'question') + .innerJoin('question.formSection', 'formSection') + .innerJoin('formSection.formPage', 'formPage') + .where('formPage.formId = :formId', { formId }) + .groupBy('answer.participantId') + .having('COUNT(DISTINCT answer.questionId) = :numQuestions', { numQuestions }); + + const numCompleteParticipantsResult = await this.answerRepository.manager + .createQueryBuilder() + .select('COUNT(*)', 'count') + .from(`(${subQuery.getQuery()})`, 'subquery') + .setParameters(subQuery.getParameters()) + .getRawOne(); + const numCompleteParticipants = numCompleteParticipantsResult ? Number(numCompleteParticipantsResult.count) : 0; + + // 6. Find or create FormResult for the Form + let formResult = await this.formResultRepo.findOne({ where: { form: { id: formId } }, relations: ['form'] }); + if (!formResult) { + formResult = this.formResultRepo.create({ + form, // Link to the Form entity + numQuestions, + numParticipants, + numAnswers, + numComplete: numCompleteParticipants, + opinions: [], // Initialize as empty or compute if needed + status: 'active', + }); + } else { + formResult.numQuestions = numQuestions; + formResult.numParticipants = numParticipants; + formResult.numAnswers = numAnswers; + formResult.numComplete = numCompleteParticipants; + // Preserve existing opinions or update if needed + } + + // 7. Save and return the FormResult + return await this.formResultRepo.save(formResult); + } + } diff --git a/src/formSection/dto/create-formSection.dto.ts b/src/formSection/dto/create-formSection.dto.ts index 861f0a8..ac4c4b6 100644 --- a/src/formSection/dto/create-formSection.dto.ts +++ b/src/formSection/dto/create-formSection.dto.ts @@ -1,21 +1,19 @@ -import { IsString, IsOptional, IsUUID, ValidateNested, IsArray, IsEnum, IsDateString } from 'class-validator'; +import { IsString, IsOptional, IsUUID, ValidateNested, IsArray, IsEnum } from 'class-validator'; import { Type } from 'class-transformer'; import { BaseDto } from '../../_core/dto/base.dto'; -import { CreateAttachmentDto } from '../../attachment/dto/create_attachment.dto'; -import { CreateQuestionDto } from '../../question/dto/create-question.dto'; // DTO for DisplayCondition export class CreateDisplayConditionDto { - @IsUUID() - answer: string; + @IsUUID('4') + answer: string; // UUID referencing Answer.id - @IsUUID() - question: string; + @IsUUID('4') + question: string; // UUID referencing Question.id @IsEnum(['equal', 'contains', 'not_equal', 'not_contains']) relation: 'equal' | 'contains' | 'not_equal' | 'not_contains'; } -//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ + export class CreateFormSectionDto extends BaseDto { @IsString() title: string; @@ -23,19 +21,19 @@ export class CreateFormSectionDto extends BaseDto { @IsString() description: string; - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateAttachmentDto) @IsOptional() - attachments?: CreateAttachmentDto[]; + @IsArray() + @IsUUID('4', { each: true }) + attachmentIds?: string[]; + @IsOptional() + @IsArray() + @IsUUID('4', { each: true }) + questionIds?: string[]; + + @IsOptional() @IsArray() @ValidateNested({ each: true }) @Type(() => CreateDisplayConditionDto) - displayCondition: CreateDisplayConditionDto[]; - - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateQuestionDto) - questions: CreateQuestionDto[]; -} \ No newline at end of file + displayCondition?: CreateDisplayConditionDto[]; +} diff --git a/src/formSection/entity/formSection.entity.ts b/src/formSection/entity/formSection.entity.ts index d965668..563075f 100644 --- a/src/formSection/entity/formSection.entity.ts +++ b/src/formSection/entity/formSection.entity.ts @@ -1,12 +1,8 @@ -// src/formSection/entity/formSection.entity.ts -import { Entity, Column, OneToMany, ManyToOne } from 'typeorm'; // Removed Embeddable import +import { Entity, Column } from 'typeorm'; // Removed Embeddable import import { BaseEntity } from '../../_core/entity/_base.entity'; -import { Attachment } from '../../attachment/entity/attachment.entity'; -import { Question } from '../../question/entity/question.entity'; -import { FormPage } from 'src/formPage/entity/formPage.entity'; // DisplayCondition is now a regular class, embedded using @Column(() => DisplayCondition) -export class DisplayCondition { +export class DisplayCondition { // needs clarification @Column() answer: string; // Assuming this is an ID reference to an Answer entity @@ -28,19 +24,12 @@ export class FormSection extends BaseEntity { @Column() description: string; - // One-to-Many relationship with Attachment - @OneToMany(() => Attachment, attachment => attachment.formSection, { cascade: true, eager: true }) - attachments: Attachment[]; + @Column('simple-array', { nullable: true }) + attachmentIds: string[]; - // Using @Column(() => DisplayCondition) for embedded array of objects - @Column(() => DisplayCondition) + @Column('simple-array', { nullable: true }) + questionIds: string[]; + + @Column(() => DisplayCondition) // needs to be checked displayCondition: DisplayCondition[]; - - // One-to-Many relationship with Question - @OneToMany(() => Question, question => question.formSection, { cascade: true, eager: true }) - questions: Question[]; - - // Many-to-One relationship with FormPage - @ManyToOne(() => FormPage, formPage => formPage.formSections) - formPage: FormPage; } \ No newline at end of file diff --git a/src/option/entity/option.entity.ts b/src/option/entity/option.entity.ts index 636df36..8d917e0 100644 --- a/src/option/entity/option.entity.ts +++ b/src/option/entity/option.entity.ts @@ -6,9 +6,4 @@ import { Question } from 'src/question/entity/question.entity'; export class Option extends BaseEntity { @Column() text: string; - - @ManyToOne(() => Question, (question) => question.options) - question: Question; - - } \ No newline at end of file diff --git a/src/participant/dto/create-participant.dto.ts b/src/participant/dto/create-participant.dto.ts index fbded40..d09acf5 100644 --- a/src/participant/dto/create-participant.dto.ts +++ b/src/participant/dto/create-participant.dto.ts @@ -1,9 +1,10 @@ -import { IsString, IsEnum, IsOptional, IsUUID, ValidateNested } from 'class-validator'; -import { Type } from 'class-transformer'; -import { MetadataDto } from '../../_core/dto/metadataEntry.dto'; +import { IsString, IsEnum, IsOptional, IsUUID } from 'class-validator'; import { BaseDto } from '../../_core/dto/base.dto'; export class CreateParticipantDto extends BaseDto { + @IsUUID('4') + userId: string; + @IsString() displayName: string; diff --git a/src/participant/entity/participant.entity.ts b/src/participant/entity/participant.entity.ts index b5245a7..a2cef84 100644 --- a/src/participant/entity/participant.entity.ts +++ b/src/participant/entity/participant.entity.ts @@ -1,7 +1,5 @@ -import { Column, Entity, ManyToOne } from 'typeorm'; +import { Column, Entity } from 'typeorm'; import { BaseEntity } from 'src/_core/entity/_base.entity'; -import { Form } from 'src/form/entity/form.entity'; -import { Realm } from '../../realm/entity/realm.entity'; @Entity({ name: 'participants' }) export class Participant extends BaseEntity { @@ -20,12 +18,4 @@ export class Participant extends BaseEntity { @Column() displayName: string; - - // Many-to-One relationship with Form - @ManyToOne(() => Form, form => form.participants) - form: Form; - - // Many-to-One relationship with Realm (if a participant belongs to a realm) - @ManyToOne(() => Realm, realm => realm.participants) - realm: Realm; } \ No newline at end of file diff --git a/src/participantGroup/dto/create-participantGroup.dto.ts b/src/participantGroup/dto/create-participantGroup.dto.ts index 6596ecc..ab7e771 100644 --- a/src/participantGroup/dto/create-participantGroup.dto.ts +++ b/src/participantGroup/dto/create-participantGroup.dto.ts @@ -1,6 +1,4 @@ -import { IsString, IsEnum, IsOptional, IsUUID, ValidateNested } from 'class-validator'; -import { Type } from 'class-transformer'; -import { MetadataDto } from '../../_core/dto/metadataEntry.dto'; +import { IsString, IsEnum } from 'class-validator'; import { BaseDto } from '../../_core/dto/base.dto'; export class CreateParticipantGroupDto extends BaseDto{ diff --git a/src/participantGroup/entity/participantGroup.entity.ts b/src/participantGroup/entity/participantGroup.entity.ts index cb9a55a..eacd064 100644 --- a/src/participantGroup/entity/participantGroup.entity.ts +++ b/src/participantGroup/entity/participantGroup.entity.ts @@ -1,9 +1,6 @@ import { Entity, - PrimaryGeneratedColumn, Column, - CreateDateColumn, - UpdateDateColumn, } from 'typeorm'; import { BaseEntity } from '../../_core/entity/_base.entity'; diff --git a/src/question/dto/create-question.dto.ts b/src/question/dto/create-question.dto.ts index c155f68..128f427 100644 --- a/src/question/dto/create-question.dto.ts +++ b/src/question/dto/create-question.dto.ts @@ -4,12 +4,9 @@ import { IsOptional, IsArray, IsEnum, - ValidateNested, + IsUUID, } from 'class-validator'; -import { Type } from 'class-transformer'; import { BaseDto } from '../../_core/dto/base.dto'; -import { CreateOptionDto } from '../../option/dto/create-option.dto'; -import { CreateAttachmentDto } from '../../attachment/dto/create_attachment.dto'; export class CreateQuestionDto extends BaseDto { @IsString() @@ -32,13 +29,11 @@ export class CreateQuestionDto extends BaseDto { @IsOptional() @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateOptionDto) - options?: CreateOptionDto[]; + @IsUUID('4', { each: true }) + optionIds?: string[]; @IsOptional() @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateAttachmentDto) - attachments?: CreateAttachmentDto[]; -} + @IsUUID('4', { each: true }) + attachmentIds?: string[]; +} \ No newline at end of file diff --git a/src/question/entity/question.entity.ts b/src/question/entity/question.entity.ts index a708e50..cae53b9 100644 --- a/src/question/entity/question.entity.ts +++ b/src/question/entity/question.entity.ts @@ -35,15 +35,9 @@ export class Question extends BaseEntity { }) validationRules?: string; - @OneToMany(() => Option, (option) => option.question, { cascade: true }) - options: Option[]; + @Column('simple-array', { nullable: true }) + optionIds: string[]; - @OneToMany(() => Attachment, (attachment) => attachment.question, { cascade: true }) - attachments: Attachment[]; - - @OneToMany(() => Answer, answer => answer.question, { cascade: true }) - answers: Answer[]; // Answers to this question - - @ManyToOne(() => FormSection, formSection => formSection.questions) - formSection: FormSection; + @Column('simple-array', { nullable: true }) + attachmentIds: string[]; } \ No newline at end of file diff --git a/src/realm/dto/create-realm.dto.ts b/src/realm/dto/create-realm.dto.ts index a3b1961..3b46670 100644 --- a/src/realm/dto/create-realm.dto.ts +++ b/src/realm/dto/create-realm.dto.ts @@ -1,8 +1,5 @@ -import { IsString, IsOptional, ValidateNested, IsArray } from 'class-validator'; -import { Type } from 'class-transformer'; +import { IsString, IsOptional, IsArray, IsUUID } from 'class-validator'; import { BaseDto } from '../../_core/dto/base.dto'; -import { CreateFormDto } from '../../form/dto/create-form.dto'; -import { CreateParticipantDto } from '../../participant/dto/create-participant.dto'; export class CreateRealmDto extends BaseDto { @IsString() @@ -11,19 +8,16 @@ export class CreateRealmDto extends BaseDto { @IsString() description: string; - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateFormDto) @IsOptional() - forms?: CreateFormDto[]; - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateParticipantDto) - @IsOptional() - participants?: CreateParticipantDto[]; + @IsUUID('4', { each: true }) + formIds?: string[]; - // @ValidateNested() // Not IsArray, as it's a single owner - // @Type(() => CreateParticipantDto) - // owner: CreateParticipantDto; -} + @IsOptional() + @IsArray() + @IsUUID('4', { each: true }) // I'd say it's not needed, cause form has participants + participantIds?: string[]; + + @IsUUID('4') + ownerId: string; +} \ No newline at end of file diff --git a/src/realm/entity/realm.entity.ts b/src/realm/entity/realm.entity.ts index 4b53cbf..f91c5aa 100644 --- a/src/realm/entity/realm.entity.ts +++ b/src/realm/entity/realm.entity.ts @@ -1,7 +1,5 @@ -import { Entity, Column, OneToMany, OneToOne, JoinColumn} from 'typeorm'; +import { Entity, Column } from 'typeorm'; import { BaseEntity } from '../../_core/entity/_base.entity'; -import { Form } from '../../form/entity/form.entity'; -import { Participant } from '../../participant/entity/participant.entity'; @Entity() export class Realm extends BaseEntity { @@ -11,14 +9,12 @@ export class Realm extends BaseEntity { @Column() description: string; - @OneToMany(() => Form, form => form.realm, { cascade: true, eager: true }) - forms: Form[]; + @Column('simple-array', { nullable: true }) + formIds: string[]; - @OneToMany(() => Participant, participant => participant.realm, { cascade: true, eager: true }) - participants: Participant[]; + @Column('simple-array', { nullable: true }) // I'd say it's not needed, cause form has participants + participantIds: string[]; - @OneToOne(() => Participant, { cascade: true, eager: true }) - @JoinColumn({ name: 'ownerId', referencedColumnName: 'userId' }) // be explicit - owner: Participant; - -} + @Column({ type: 'uuid' }) + ownerId: string; +} \ No newline at end of file