From 219974385a94f7a996bb1b50a4addf6a10a2f0b9 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 10 Feb 2025 12:59:06 +0400 Subject: [PATCH] added product allocation for subspace and space model --- .../src/modules/space-model/entities/index.ts | 1 + .../space-model-product-allocation.entity.ts | 41 +++++++++++++++ .../entities/space-model.entity.ts | 8 +++ .../entities/subspace-model/index.ts | 2 +- ...ubspace-model-product-allocation.entity.ts | 51 +++++++++++++++++++ .../subspace-model/subspace-model.entity.ts | 10 +++- .../repositories/space-model.repository.ts | 22 +++++++- 7 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 libs/common/src/modules/space-model/entities/space-model-product-allocation.entity.ts create mode 100644 libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts diff --git a/libs/common/src/modules/space-model/entities/index.ts b/libs/common/src/modules/space-model/entities/index.ts index f4fffbd..56de1b5 100644 --- a/libs/common/src/modules/space-model/entities/index.ts +++ b/libs/common/src/modules/space-model/entities/index.ts @@ -1,3 +1,4 @@ export * from './space-model.entity'; export * from './subspace-model'; export * from './tag-model.entity'; +export * from './space-model-product-allocation.entity'; diff --git a/libs/common/src/modules/space-model/entities/space-model-product-allocation.entity.ts b/libs/common/src/modules/space-model/entities/space-model-product-allocation.entity.ts new file mode 100644 index 0000000..18288fe --- /dev/null +++ b/libs/common/src/modules/space-model/entities/space-model-product-allocation.entity.ts @@ -0,0 +1,41 @@ +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 { SpaceProductAllocationEntity } from '../../space/entities/space-product-allocation.entity'; + +@Entity({ name: 'space_model_product_allocation' }) +export class SpaceModelProductAllocationEntity { + @Column({ + type: 'uuid', + default: () => 'gen_random_uuid()', + nullable: false, + }) + public uuid: string; + + @ManyToOne( + () => SpaceModelEntity, + (spaceModel) => spaceModel.productAllocations, + { nullable: false, onDelete: 'CASCADE' }, + ) + public spaceModel: SpaceModelEntity; + + @ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' }) + public product: ProductEntity; + + @Column({ type: 'int', default: 1 }) + public allowedQuantity: number; + + @ManyToMany(() => NewTagEntity) + @JoinTable({ name: 'space_model_product_tags' }) + public allowedTags: NewTagEntity[]; + + @OneToMany( + () => SpaceProductAllocationEntity, + (allocation) => allocation.inheritedFromModel, + { + cascade: true, + }, + ) + public inheritedSpaceAllocations: SpaceProductAllocationEntity[]; +} diff --git a/libs/common/src/modules/space-model/entities/space-model.entity.ts b/libs/common/src/modules/space-model/entities/space-model.entity.ts index 648e90d..d0b471e 100644 --- a/libs/common/src/modules/space-model/entities/space-model.entity.ts +++ b/libs/common/src/modules/space-model/entities/space-model.entity.ts @@ -5,6 +5,7 @@ import { SubspaceModelEntity } from './subspace-model'; import { ProjectEntity } from '../../project/entities'; import { SpaceEntity } from '../../space/entities'; import { TagModel } from './tag-model.entity'; +import { SpaceModelProductAllocation } from './space-model-product-allocation.entity'; @Entity({ name: 'space-model' }) export class SpaceModelEntity extends AbstractEntity { @@ -51,6 +52,13 @@ export class SpaceModelEntity extends AbstractEntity { @OneToMany(() => TagModel, (tag) => tag.spaceModel) tags: TagModel[]; + @OneToMany( + () => SpaceModelProductAllocation, + (allocation) => allocation.spaceModel, + { cascade: true }, + ) + public productAllocations: SpaceModelProductAllocation[]; + constructor(partial: Partial) { super(); Object.assign(this, partial); diff --git a/libs/common/src/modules/space-model/entities/subspace-model/index.ts b/libs/common/src/modules/space-model/entities/subspace-model/index.ts index 262490e..06a40d5 100644 --- a/libs/common/src/modules/space-model/entities/subspace-model/index.ts +++ b/libs/common/src/modules/space-model/entities/subspace-model/index.ts @@ -1,2 +1,2 @@ export * from './subspace-model.entity'; - \ No newline at end of file +export * from './subspace-model-product-allocation.entity'; diff --git a/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts b/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts new file mode 100644 index 0000000..f1dd6a0 --- /dev/null +++ b/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts @@ -0,0 +1,51 @@ +import { + Entity, + Column, + ManyToOne, + ManyToMany, + JoinTable, + Unique, + OneToMany, +} 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'; + +@Entity({ name: 'subspace_model_product_allocation' }) +@Unique(['subspaceModel', 'product', 'allowedTags']) +export class SubspaceModelProductAllocationEntity { + @Column({ + type: 'uuid', + default: () => 'gen_random_uuid()', + nullable: false, + }) + public uuid: string; + + @ManyToOne( + () => SubspaceModelEntity, + (subspaceModel) => subspaceModel.productAllocations, + { nullable: false, onDelete: 'CASCADE' }, + ) + public subspaceModel: SubspaceModelEntity; + + @ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' }) + public product: ProductEntity; + + @Column({ type: 'int', default: 1 }) + public allowedQuantity: number; + + @ManyToMany(() => NewTagEntity) + @JoinTable({ name: 'subspace_model_product_tags' }) + public allowedTags: NewTagEntity[]; + + @OneToMany( + () => SubspaceProductAllocationEntity, + (allocation) => allocation.inheritedFromModel, + { + cascade: true, + }, + ) + public inheritedSubspaceAllocations: SubspaceProductAllocationEntity[]; +} diff --git a/libs/common/src/modules/space-model/entities/subspace-model/subspace-model.entity.ts b/libs/common/src/modules/space-model/entities/subspace-model/subspace-model.entity.ts index f5f6377..41dd75a 100644 --- a/libs/common/src/modules/space-model/entities/subspace-model/subspace-model.entity.ts +++ b/libs/common/src/modules/space-model/entities/subspace-model/subspace-model.entity.ts @@ -1,9 +1,10 @@ import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity'; -import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm'; +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 { SubspaceModelProductAllocation } from './subspace-model-product-allocation.entity'; @Entity({ name: 'subspace-model' }) export class SubspaceModelEntity extends AbstractEntity { @@ -42,4 +43,11 @@ export class SubspaceModelEntity extends AbstractEntity { @OneToMany(() => TagModel, (tag) => tag.subspaceModel) tags: TagModel[]; + + @OneToMany( + () => SubspaceModelProductAllocation, + (allocation) => allocation.subspaceModel, + { cascade: true }, + ) + public productAllocations: SubspaceModelProductAllocation[]; } diff --git a/libs/common/src/modules/space-model/repositories/space-model.repository.ts b/libs/common/src/modules/space-model/repositories/space-model.repository.ts index 4af0d57..d425946 100644 --- a/libs/common/src/modules/space-model/repositories/space-model.repository.ts +++ b/libs/common/src/modules/space-model/repositories/space-model.repository.ts @@ -1,6 +1,12 @@ import { DataSource, Repository } from 'typeorm'; import { Injectable } from '@nestjs/common'; -import { SpaceModelEntity, SubspaceModelEntity, TagModel } from '../entities'; +import { + SpaceModelEntity, + SpaceModelProductAllocation, + SubspaceModelEntity, + SubspaceModelProductAllocation, + TagModel, +} from '../entities'; @Injectable() export class SpaceModelRepository extends Repository { @@ -21,3 +27,17 @@ export class TagModelRepository extends Repository { super(TagModel, dataSource.createEntityManager()); } } + +@Injectable() +export class SpaceModelProductAllocationRepoitory extends Repository { + constructor(private dataSource: DataSource) { + super(SpaceModelProductAllocation, dataSource.createEntityManager()); + } +} + +@Injectable() +export class SubspaceModelProductAllocationRepoitory extends Repository { + constructor(private dataSource: DataSource) { + super(SubspaceModelProductAllocation, dataSource.createEntityManager()); + } +}