diff --git a/libs/common/src/modules/space/entities/space-product-allocation.entity.ts b/libs/common/src/modules/space/entities/space-product-allocation.entity.ts new file mode 100644 index 0000000..04cbf42 --- /dev/null +++ b/libs/common/src/modules/space/entities/space-product-allocation.entity.ts @@ -0,0 +1,37 @@ +import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm'; +import { SpaceEntity } from './space.entity'; +import { SpaceModelProductAllocationEntity } from '../../space-model'; +import { ProductEntity } from '../../product/entities'; +import { NewTagEntity } from '../../tag'; + +@Entity({ name: 'space_product_allocation' }) +export class SpaceProductAllocationEntity { + @Column({ + type: 'uuid', + default: () => 'gen_random_uuid()', + nullable: false, + }) + public uuid: string; + + @ManyToOne(() => SpaceEntity, (space) => space.productAllocations, { + nullable: false, + onDelete: 'CASCADE', + }) + public space: SpaceEntity; + + @ManyToOne(() => SpaceModelProductAllocationEntity, { + nullable: true, + onDelete: 'SET NULL', + }) + public inheritedFromModel?: SpaceModelProductAllocationEntity; + + @ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' }) + public product: ProductEntity; + + @Column({ type: 'int', default: 1 }) + public allowedQuantity: number; + + @ManyToMany(() => NewTagEntity) + @JoinTable({ name: 'space_product_tags' }) + public allowedTags: NewTagEntity[]; +} diff --git a/libs/common/src/modules/space/entities/space.entity.ts b/libs/common/src/modules/space/entities/space.entity.ts index a0b908d..7953802 100644 --- a/libs/common/src/modules/space/entities/space.entity.ts +++ b/libs/common/src/modules/space/entities/space.entity.ts @@ -10,6 +10,7 @@ import { SceneEntity } from '../../scene/entities'; import { SpaceModelEntity } from '../../space-model'; import { InviteUserSpaceEntity } from '../../Invite-user/entities'; import { TagEntity } from './tag.entity'; +import { SpaceProductAllocationEntity } from './space-product-allocation.entity'; @Entity({ name: 'space' }) export class SpaceEntity extends AbstractEntity { @@ -105,6 +106,15 @@ export class SpaceEntity extends AbstractEntity { @OneToMany(() => TagEntity, (tag) => tag.space) tags: TagEntity[]; + @OneToMany( + () => SpaceProductAllocationEntity, + (allocation) => allocation.space, + { + cascade: true, + }, + ) + public productAllocations: SpaceProductAllocationEntity[]; + constructor(partial: Partial) { super(); Object.assign(this, partial); diff --git a/libs/common/src/modules/space/entities/subspace/index.ts b/libs/common/src/modules/space/entities/subspace/index.ts index be13961..32d055c 100644 --- a/libs/common/src/modules/space/entities/subspace/index.ts +++ b/libs/common/src/modules/space/entities/subspace/index.ts @@ -1 +1,2 @@ export * from './subspace.entity'; +export * from './subspace-product-allocation.entity'; diff --git a/libs/common/src/modules/space/entities/subspace/subspace-product-allocation.entity.ts b/libs/common/src/modules/space/entities/subspace/subspace-product-allocation.entity.ts new file mode 100644 index 0000000..e18145b --- /dev/null +++ b/libs/common/src/modules/space/entities/subspace/subspace-product-allocation.entity.ts @@ -0,0 +1,45 @@ +import { + Entity, + Column, + ManyToOne, + ManyToMany, + JoinTable, + Unique, +} from 'typeorm'; +import { SubspaceEntity } from './subspace.entity'; +import { ProductEntity } from '@app/common/modules/product/entities'; +import { NewTagEntity } from '@app/common/modules/tag'; +import { SubspaceModelProductAllocationEntity } from '@app/common/modules/space-model'; + +@Entity({ name: 'subspace_product_allocation' }) +@Unique(['subspaceModel', 'product', 'allowedTags']) +export class SubspaceProductAllocationEntity { + @Column({ + type: 'uuid', + default: () => 'gen_random_uuid()', + nullable: false, + }) + public uuid: string; + + @ManyToOne(() => SubspaceEntity, (subspace) => subspace.productAllocations, { + nullable: false, + onDelete: 'CASCADE', + }) + public subspace: SubspaceEntity; + + @ManyToOne(() => SubspaceModelProductAllocationEntity, { + nullable: true, + onDelete: 'SET NULL', + }) + public inheritedFromModel?: SubspaceModelProductAllocationEntity; + + @ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' }) + public product: ProductEntity; + + @Column({ type: 'int', default: 1 }) + public allowedQuantity: number; + + @ManyToMany(() => NewTagEntity) + @JoinTable({ name: 'subspace_product_tags' }) + public allowedTags: NewTagEntity[]; +} diff --git a/libs/common/src/modules/space/entities/subspace/subspace.entity.ts b/libs/common/src/modules/space/entities/subspace/subspace.entity.ts index c7247cc..3227bb7 100644 --- a/libs/common/src/modules/space/entities/subspace/subspace.entity.ts +++ b/libs/common/src/modules/space/entities/subspace/subspace.entity.ts @@ -5,6 +5,7 @@ import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm'; import { SubspaceDto } from '../../dtos'; import { SpaceEntity } from '../space.entity'; import { TagEntity } from '../tag.entity'; +import { SubspaceProductAllocationEntity } from './subspace-product-allocation.entity'; @Entity({ name: 'subspace' }) export class SubspaceEntity extends AbstractEntity { @@ -45,6 +46,13 @@ export class SubspaceEntity extends AbstractEntity { @OneToMany(() => TagEntity, (tag) => tag.subspace) tags: TagEntity[]; + @OneToMany( + () => SubspaceProductAllocationEntity, + (allocation) => allocation.subspace, + { cascade: true }, + ) + public productAllocations: SubspaceProductAllocationEntity[]; + constructor(partial: Partial) { super(); Object.assign(this, partial);