fixed entity relation, removed circular dependency

This commit is contained in:
hannathkadher
2025-02-10 20:36:29 +04:00
parent f5f9e9dfe3
commit e9a3cd14a8
40 changed files with 250 additions and 95 deletions
@@ -0,0 +1,39 @@
import {
IsUUID,
IsInt,
IsOptional,
ValidateNested,
IsArray,
} from 'class-validator';
import { Type } from 'class-transformer';
import { SpaceModelDto } from '../../space-model/dtos/space-model.dto';
import { ProductDto } from '../../product/dtos/product.dto';
import { NewTagDto } from '../../tag/dtos/tag.dto';
import { SpaceProductAllocationDto } from '../../space/dtos/space-product-allocation.dto';
export class SpaceModelProductAllocationDto {
@IsUUID()
uuid: string;
@ValidateNested()
@Type(() => SpaceModelDto)
spaceModel: SpaceModelDto;
@ValidateNested()
@Type(() => ProductDto)
product: ProductDto;
@IsInt()
allowedQuantity: number;
@IsArray()
@ValidateNested({ each: true })
@Type(() => NewTagDto)
allowedTags: NewTagDto[];
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => SpaceProductAllocationDto)
inheritedSpaceAllocations?: SpaceProductAllocationDto[];
}
@@ -0,0 +1,26 @@
import { IsUUID, IsInt, ValidateNested, IsArray } from 'class-validator';
import { Type } from 'class-transformer';
import { SubSpaceModelDto } from './subspace-model.dto';
import { ProductDto } from '@app/common/modules/product/dtos';
import { NewTagDto } from '@app/common/modules/tag/dtos';
export class SubspaceModelProductAllocationDto {
@IsUUID()
uuid: string;
@ValidateNested()
@Type(() => SubSpaceModelDto)
subspaceModel: SubSpaceModelDto;
@ValidateNested()
@Type(() => ProductDto)
product: ProductDto;
@IsInt()
allowedQuantity: number;
@IsArray()
@ValidateNested({ each: true })
@Type(() => NewTagDto)
allowedTags: NewTagDto[];
}
@@ -1,11 +1,19 @@
import { Entity, Column, ManyToOne, ManyToMany, JoinTable, OneToMany } from 'typeorm';
import {
Entity,
Column,
ManyToOne,
ManyToMany,
JoinTable,
OneToMany,
} from 'typeorm';
import { SpaceModelEntity } from './space-model.entity';
import { NewTagEntity } from '../../tag/entities';
import { ProductEntity } from '../../product/entities';
import { NewTagEntity } from '../../tag/entities/tag.entity';
import { ProductEntity } from '../../product/entities/product.entity';
import { SpaceProductAllocationEntity } from '../../space/entities/space-product-allocation.entity';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
@Entity({ name: 'space_model_product_allocation' })
export class SpaceModelProductAllocationEntity {
export class SpaceModelProductAllocationEntity extends AbstractEntity<SpaceModelProductAllocationEntity> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
@@ -38,4 +46,9 @@ export class SpaceModelProductAllocationEntity {
},
)
public inheritedSpaceAllocations: SpaceProductAllocationEntity[];
constructor(partial: Partial<SpaceModelProductAllocationEntity>) {
super();
Object.assign(this, partial);
}
}
@@ -3,9 +3,9 @@ import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceModelDto } from '../dtos';
import { SubspaceModelEntity } from './subspace-model';
import { ProjectEntity } from '../../project/entities';
import { SpaceEntity } from '../../space/entities';
import { TagModel } from './tag-model.entity';
import { SpaceModelProductAllocationEntity } from './space-model-product-allocation.entity';
import { SpaceEntity } from '../../space/entities/space.entity';
@Entity({ name: 'space-model' })
export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
@@ -1,21 +1,12 @@
import {
Entity,
Column,
ManyToOne,
ManyToMany,
JoinTable,
Unique,
OneToMany,
} from 'typeorm';
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
import { SubspaceModelEntity } from './subspace-model.entity';
import { NewTagEntity } from '@app/common/modules/tag';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space';
import { ProductEntity } from '@app/common/modules/product/entities/product.entity';
import { NewTagEntity } from '@app/common/modules/tag/entities/tag.entity';
import { SubspaceModelProductAllocationDto } from '../../dtos/subspace-model/subspace-model-product-allocation.dto';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
@Entity({ name: 'subspace_model_product_allocation' })
@Unique(['subspaceModel', 'product', 'allowedTags'])
export class SubspaceModelProductAllocationEntity {
export class SubspaceModelProductAllocationEntity extends AbstractEntity<SubspaceModelProductAllocationDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
@@ -26,7 +17,10 @@ export class SubspaceModelProductAllocationEntity {
@ManyToOne(
() => SubspaceModelEntity,
(subspaceModel) => subspaceModel.productAllocations,
{ nullable: false, onDelete: 'CASCADE' },
{
nullable: false,
onDelete: 'CASCADE',
},
)
public subspaceModel: SubspaceModelEntity;
@@ -36,16 +30,12 @@ export class SubspaceModelProductAllocationEntity {
@Column({ type: 'int', default: 1 })
public allowedQuantity: number;
@ManyToMany(() => NewTagEntity)
@ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations)
@JoinTable({ name: 'subspace_model_product_tags' })
public allowedTags: NewTagEntity[];
@OneToMany(
() => SubspaceProductAllocationEntity,
(allocation) => allocation.inheritedFromModel,
{
cascade: true,
},
)
public inheritedSubspaceAllocations: SubspaceProductAllocationEntity[];
constructor(partial: Partial<SubspaceModelProductAllocationEntity>) {
super();
Object.assign(this, partial);
}
}
@@ -2,9 +2,9 @@ import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.e
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
import { SubSpaceModelDto } from '../../dtos';
import { SpaceModelEntity } from '../space-model.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities';
import { TagModel } from '../tag-model.entity';
import { SubspaceModelProductAllocationEntity } from './subspace-model-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
@Entity({ name: 'subspace-model' })
export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {