mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
cleaned space entities
This commit is contained in:
@ -12,6 +12,7 @@ import {
|
||||
SpaceEntity,
|
||||
SpaceLinkEntity,
|
||||
SubspaceEntity,
|
||||
TagEntity,
|
||||
} from '../modules/space/entities';
|
||||
import { UserSpaceEntity } from '../modules/user/entities';
|
||||
import { DeviceUserPermissionEntity } from '../modules/device/entities';
|
||||
@ -61,6 +62,7 @@ import {
|
||||
SpaceEntity,
|
||||
SpaceLinkEntity,
|
||||
SubspaceEntity,
|
||||
TagEntity,
|
||||
UserSpaceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
RoleTypeEntity,
|
||||
|
@ -3,7 +3,7 @@ import { ProductDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { TagModel } from '../../space-model';
|
||||
|
||||
import { TagEntity } from '../../space/entities/tag.entity';
|
||||
@Entity({ name: 'product' })
|
||||
export class ProductEntity extends AbstractEntity<ProductDto> {
|
||||
@Column({
|
||||
@ -30,6 +30,9 @@ export class ProductEntity extends AbstractEntity<ProductDto> {
|
||||
@OneToMany(() => TagModel, (tag) => tag.product)
|
||||
tagModels: TagModel[];
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.product)
|
||||
tags: TagEntity[];
|
||||
|
||||
@OneToMany(
|
||||
() => DeviceEntity,
|
||||
(devicesProductEntity) => devicesProductEntity.productDevice,
|
||||
|
@ -1,9 +1,17 @@
|
||||
import { Column, Entity, JoinColumn, ManyToOne, Unique } from 'typeorm';
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
Unique,
|
||||
} from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { TagModelDto } from '../dtos/tag-model.dto';
|
||||
import { SpaceModelEntity } from './space-model.entity';
|
||||
import { SubspaceModelEntity } from './subspace-model';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { TagEntity } from '../../space/entities/tag.entity';
|
||||
|
||||
@Entity({ name: 'tag_model' })
|
||||
@Unique(['tag', 'product', 'spaceModel', 'subspaceModel'])
|
||||
@ -18,13 +26,13 @@ export class TagModel extends AbstractEntity<TagModelDto> {
|
||||
product: ProductEntity;
|
||||
|
||||
@ManyToOne(() => SpaceModelEntity, (space) => space.tags, { nullable: true })
|
||||
@JoinColumn({ name: 'space_id' })
|
||||
@JoinColumn({ name: 'space_model_id' })
|
||||
spaceModel: SpaceModelEntity;
|
||||
|
||||
@ManyToOne(() => SubspaceModelEntity, (subspace) => subspace.tags, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'subspace_id' })
|
||||
@JoinColumn({ name: 'subspace_model_id' })
|
||||
subspaceModel: SubspaceModelEntity;
|
||||
|
||||
@Column({
|
||||
@ -32,4 +40,7 @@ export class TagModel extends AbstractEntity<TagModelDto> {
|
||||
default: false,
|
||||
})
|
||||
public disabled: boolean;
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.model)
|
||||
tags: TagEntity[];
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
export * from './space.dto';
|
||||
export * from './subspace.dto';
|
||||
export * from './tag.dto';
|
||||
|
21
libs/common/src/modules/space/dtos/tag.dto.ts
Normal file
21
libs/common/src/modules/space/dtos/tag.dto.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class TagDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public name: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public productUuid: string;
|
||||
|
||||
@IsString()
|
||||
spaceUuid: string;
|
||||
|
||||
@IsString()
|
||||
subspaceUuid: string;
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
export * from './space.entity';
|
||||
export * from './subspace';
|
||||
export * from './space-link.entity';
|
||||
export * from './tag.entity';
|
||||
|
@ -16,6 +16,7 @@ import { SpaceLinkEntity } from './space-link.entity';
|
||||
import { SceneEntity } from '../../scene/entities';
|
||||
import { SpaceModelEntity } from '../../space-model';
|
||||
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||
import { TagEntity } from './tag.entity';
|
||||
|
||||
@Entity({ name: 'space' })
|
||||
@Unique(['invitationCode'])
|
||||
@ -111,6 +112,10 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
(inviteUserSpace) => inviteUserSpace.space,
|
||||
)
|
||||
invitedUsers: InviteUserSpaceEntity[];
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.space)
|
||||
tags: TagEntity[];
|
||||
|
||||
constructor(partial: Partial<SpaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -4,6 +4,7 @@ import { SubspaceModelEntity } from '@app/common/modules/space-model';
|
||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
||||
import { SubspaceDto } from '../../dtos';
|
||||
import { SpaceEntity } from '../space.entity';
|
||||
import { TagEntity } from '../tag.entity';
|
||||
|
||||
@Entity({ name: 'subspace' })
|
||||
export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
|
||||
@ -40,6 +41,9 @@ export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
|
||||
@JoinColumn({ name: 'subspace_model_uuid' })
|
||||
subSpaceModel?: SubspaceModelEntity;
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.subspace)
|
||||
tags: TagEntity[];
|
||||
|
||||
constructor(partial: Partial<SubspaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
39
libs/common/src/modules/space/entities/tag.entity.ts
Normal file
39
libs/common/src/modules/space/entities/tag.entity.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { Entity, Column, ManyToOne, JoinColumn, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { TagDto } from '../dtos';
|
||||
import { TagModel } from '../../space-model/entities/tag-model.entity';
|
||||
import { SpaceEntity } from './space.entity';
|
||||
import { SubspaceEntity } from './subspace';
|
||||
|
||||
@Entity({ name: 'tag' })
|
||||
@Unique(['tag', 'product', 'space', 'subspace'])
|
||||
export class TagEntity extends AbstractEntity<TagDto> {
|
||||
@Column({ type: 'varchar', length: 255 })
|
||||
tag: string;
|
||||
|
||||
@ManyToOne(() => TagModel, (model) => model.tags, {
|
||||
nullable: true,
|
||||
})
|
||||
model: TagModel;
|
||||
|
||||
@ManyToOne(() => ProductEntity, (product) => product.tags, {
|
||||
nullable: false,
|
||||
})
|
||||
product: ProductEntity;
|
||||
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.tags, { nullable: true })
|
||||
space: SpaceEntity;
|
||||
|
||||
@ManyToOne(() => SubspaceEntity, (subspace) => subspace.tags, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'subspace_id' })
|
||||
subspace: SubspaceEntity;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: false,
|
||||
})
|
||||
public disabled: boolean;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SpaceEntity, SpaceLinkEntity } from '../entities';
|
||||
import { SpaceEntity, SpaceLinkEntity, TagEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceRepository extends Repository<SpaceEntity> {
|
||||
@ -15,3 +15,10 @@ export class SpaceLinkRepository extends Repository<SpaceLinkEntity> {
|
||||
super(SpaceLinkEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TagRepository extends Repository<TagEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(TagEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SpaceEntity, SubspaceEntity } from './entities';
|
||||
import { SpaceEntity, SubspaceEntity, TagEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([SpaceEntity, SubspaceEntity])],
|
||||
imports: [TypeOrmModule.forFeature([SpaceEntity, SubspaceEntity, TagEntity])],
|
||||
})
|
||||
export class SpaceRepositoryModule {}
|
||||
|
@ -9,7 +9,6 @@ import {
|
||||
AddSpaceDto,
|
||||
CommunitySpaceParam,
|
||||
GetSpaceParam,
|
||||
ProductAssignmentDto,
|
||||
UpdateSpaceDto,
|
||||
} from '../dtos';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
@ -37,8 +36,7 @@ export class SpaceService {
|
||||
addSpaceDto: AddSpaceDto,
|
||||
params: CommunitySpaceParam,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { parentUuid, direction, products, spaceModelUuid, subspaces } =
|
||||
addSpaceDto;
|
||||
const { parentUuid, direction, spaceModelUuid, subspaces } = addSpaceDto;
|
||||
const { communityUuid, projectUuid } = params;
|
||||
|
||||
if (addSpaceDto.spaceName === ORPHAN_SPACE_NAME) {
|
||||
@ -58,7 +56,7 @@ export class SpaceService {
|
||||
projectUuid,
|
||||
);
|
||||
|
||||
this.validateSpaceCreation(spaceModelUuid, products, subspaces);
|
||||
this.validateSpaceCreation(spaceModelUuid);
|
||||
|
||||
const parent = parentUuid
|
||||
? await this.validationService.validateSpace(parentUuid)
|
||||
@ -246,7 +244,7 @@ export class SpaceService {
|
||||
}
|
||||
|
||||
// If a parentId is provided, check if the parent exists
|
||||
const { parentUuid, products } = updateSpaceDto;
|
||||
const { parentUuid } = updateSpaceDto;
|
||||
const parent = parentUuid
|
||||
? await this.validationService.validateSpace(parentUuid)
|
||||
: null;
|
||||
@ -257,8 +255,6 @@ export class SpaceService {
|
||||
// Save the updated space
|
||||
await queryRunner.manager.save(space);
|
||||
|
||||
if (products && products.length > 0) {
|
||||
}
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
@ -370,12 +366,8 @@ export class SpaceService {
|
||||
return rootSpaces;
|
||||
}
|
||||
|
||||
private validateSpaceCreation(
|
||||
spaceModelUuid?: string,
|
||||
products?: ProductAssignmentDto[],
|
||||
subSpaces?: CreateSubspaceModelDto[],
|
||||
) {
|
||||
if (spaceModelUuid && (products?.length || subSpaces?.length)) {
|
||||
private validateSpaceCreation(spaceModelUuid?: string) {
|
||||
if (spaceModelUuid) {
|
||||
throw new HttpException(
|
||||
'For space creation choose either space model or products and subspace',
|
||||
HttpStatus.CONFLICT,
|
||||
|
Reference in New Issue
Block a user