replaced relations with foreign keys

This commit is contained in:
OkaykOrhmn 2025-07-23 15:11:27 +03:30
parent aeb7a73c4c
commit 64796fcdf7
22 changed files with 218 additions and 201 deletions

View File

@ -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;
}

View File

@ -16,6 +16,5 @@ export class CreateAttachmentDto extends BaseDto {
storageUrl: string;
@IsUUID()
@IsOptional()
ownerId?: string;
ownerId: string;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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[];
@ManyToOne(() => Realm, realm => realm.forms, { /*cascade: true, eager: true */})
realm: Realm;
@Column('simple-array', { nullable: true })
attachmentIds: string[];
@Column({ type: 'uuid', nullable: true })
formResultId: string;
}

View File

@ -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[];
}

View File

@ -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[];
}

View File

@ -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;

View File

@ -43,4 +43,11 @@ export class FormResultController {
async remove(@Param('id', new ParseUUIDPipe()) id: string): Promise<void> {
return this.formResultService.remove(id);
}
@Get('form/:formId/statistics')
async getFormStatistics(
@Param('formId', new ParseUUIDPipe()) formId: string,
): Promise<FormResult> {
return this.formResultService.getFormStatistics(formId);
}
}

View File

@ -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],

View File

@ -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<Form>,
@InjectRepository(FormResult)
private readonly formResultRepo: Repository<FormResult>,
@InjectRepository(Question)
private readonly questionRepository: Repository<Question>,
@InjectRepository(Answer)
private readonly answerRepository: Repository<Answer>,
) {}
async create(data: CreateFormResultDto): Promise<FormResult> {
@ -85,4 +94,90 @@ export class FormResultService {
throw new NotFoundException(`FormResult with ID "${id}" not found`);
}
}
async getFormStatistics(formId: string): Promise<FormResult> {
// 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);
}
}

View File

@ -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[];
displayCondition?: CreateDisplayConditionDto[];
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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{

View File

@ -1,9 +1,6 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
import { BaseEntity } from '../../_core/entity/_base.entity';

View File

@ -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[];
}

View File

@ -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[];
}

View File

@ -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;
}

View File

@ -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[];
@OneToOne(() => Participant, { cascade: true, eager: true })
@JoinColumn({ name: 'ownerId', referencedColumnName: 'userId' }) // be explicit
owner: Participant;
@Column('simple-array', { nullable: true }) // I'd say it's not needed, cause form has participants
participantIds: string[];
@Column({ type: 'uuid' })
ownerId: string;
}