added product allocation for subspace and space model

This commit is contained in:
hannathkadher
2025-02-10 12:59:06 +04:00
parent 4c7208cd5f
commit 219974385a
7 changed files with 132 additions and 3 deletions

View File

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

View File

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

View File

@ -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<SpaceModelDto> {
@ -51,6 +52,13 @@ export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
@OneToMany(() => TagModel, (tag) => tag.spaceModel)
tags: TagModel[];
@OneToMany(
() => SpaceModelProductAllocation,
(allocation) => allocation.spaceModel,
{ cascade: true },
)
public productAllocations: SpaceModelProductAllocation[];
constructor(partial: Partial<SpaceModelEntity>) {
super();
Object.assign(this, partial);

View File

@ -1,2 +1,2 @@
export * from './subspace-model.entity';
export * from './subspace-model-product-allocation.entity';

View File

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

View File

@ -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<SubSpaceModelDto> {
@ -42,4 +43,11 @@ export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
@OneToMany(() => TagModel, (tag) => tag.subspaceModel)
tags: TagModel[];
@OneToMany(
() => SubspaceModelProductAllocation,
(allocation) => allocation.subspaceModel,
{ cascade: true },
)
public productAllocations: SubspaceModelProductAllocation[];
}

View File

@ -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<SpaceModelEntity> {
@ -21,3 +27,17 @@ export class TagModelRepository extends Repository<TagModel> {
super(TagModel, dataSource.createEntityManager());
}
}
@Injectable()
export class SpaceModelProductAllocationRepoitory extends Repository<SpaceModelProductAllocation> {
constructor(private dataSource: DataSource) {
super(SpaceModelProductAllocation, dataSource.createEntityManager());
}
}
@Injectable()
export class SubspaceModelProductAllocationRepoitory extends Repository<SubspaceModelProductAllocation> {
constructor(private dataSource: DataSource) {
super(SubspaceModelProductAllocation, dataSource.createEntityManager());
}
}