mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 21:37:54 +00:00
added space delete
This commit is contained in:
@@ -199,6 +199,12 @@ export class SpaceModelService {
|
||||
);
|
||||
}
|
||||
|
||||
if (spaceModel.tags?.length) {
|
||||
const deleteSpaceTagsDtos = spaceModel.tags.map((tag) => tag.uuid);
|
||||
|
||||
await this.tagModelService.deleteTags(deleteSpaceTagsDtos, queryRunner);
|
||||
}
|
||||
|
||||
await queryRunner.manager.update(
|
||||
this.spaceModelRepository.target,
|
||||
{ uuid: param.spaceModelUuid },
|
||||
@@ -248,6 +254,7 @@ export class SpaceModelService {
|
||||
uuid,
|
||||
disabled: true,
|
||||
},
|
||||
relations: ['subspaceModels', 'tags'],
|
||||
});
|
||||
if (!spaceModel) {
|
||||
throw new HttpException('space model not found', HttpStatus.NOT_FOUND);
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
} from '@app/common/modules/space-model';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { IDeletedSubsaceModelInterface } from 'src/space-model/interfaces';
|
||||
import {
|
||||
DeleteSubspaceModelDto,
|
||||
@@ -27,7 +27,7 @@ export class SubSpaceModelService {
|
||||
queryRunner: QueryRunner,
|
||||
otherTags?: CreateTagModelDto[],
|
||||
): Promise<SubspaceModelEntity[]> {
|
||||
this.validateInputDtos(subSpaceModelDtos);
|
||||
this.validateInputDtos(subSpaceModelDtos, spaceModel);
|
||||
|
||||
const subspaces = subSpaceModelDtos.map((subspaceDto) =>
|
||||
queryRunner.manager.create(this.subspaceModelRepository.target, {
|
||||
@@ -41,13 +41,18 @@ export class SubSpaceModelService {
|
||||
await Promise.all(
|
||||
subSpaceModelDtos.map(async (dto, index) => {
|
||||
const subspace = savedSubspaces[index];
|
||||
|
||||
const otherDtoTags = subSpaceModelDtos
|
||||
.filter((_, i) => i !== index)
|
||||
.flatMap((otherDto) => otherDto.tags || []);
|
||||
|
||||
if (dto.tags?.length) {
|
||||
subspace.tags = await this.tagModelService.createTags(
|
||||
dto.tags,
|
||||
queryRunner,
|
||||
null,
|
||||
subspace,
|
||||
otherTags,
|
||||
[...(otherTags || []), ...otherDtoTags],
|
||||
);
|
||||
}
|
||||
}),
|
||||
@@ -194,34 +199,61 @@ export class SubSpaceModelService {
|
||||
return subspace;
|
||||
}
|
||||
|
||||
private validateInputDtos(subSpaceModelDtos: CreateSubspaceModelDto[]): void {
|
||||
private validateInputDtos(
|
||||
subSpaceModelDtos: CreateSubspaceModelDto[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
): void {
|
||||
if (subSpaceModelDtos.length === 0) {
|
||||
throw new HttpException(
|
||||
'Subspace models cannot be empty.',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
this.validateName(subSpaceModelDtos.map((dto) => dto.subspaceName));
|
||||
this.validateName(
|
||||
subSpaceModelDtos.map((dto) => dto.subspaceName),
|
||||
spaceModel,
|
||||
);
|
||||
}
|
||||
|
||||
private validateName(names: string[]): void {
|
||||
private async validateName(
|
||||
names: string[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
): Promise<void> {
|
||||
const seenNames = new Set<string>();
|
||||
const duplicateNames = new Set<string>();
|
||||
|
||||
for (const name of names) {
|
||||
if (seenNames.has(name)) {
|
||||
if (!seenNames.add(name)) {
|
||||
duplicateNames.add(name);
|
||||
} else {
|
||||
seenNames.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
if (duplicateNames.size > 0) {
|
||||
throw new HttpException(
|
||||
`Duplicate subspace names found: ${[...duplicateNames].join(', ')}`,
|
||||
`Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`,
|
||||
HttpStatus.CONFLICT,
|
||||
);
|
||||
}
|
||||
|
||||
const existingNames = await this.subspaceModelRepository.find({
|
||||
select: ['subspaceName'],
|
||||
where: {
|
||||
subspaceName: In([...seenNames]),
|
||||
spaceModel: {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existingNames.length > 0) {
|
||||
const existingNamesList = existingNames
|
||||
.map((e) => e.subspaceName)
|
||||
.join(', ');
|
||||
throw new HttpException(
|
||||
`Subspace model names already exist in the space: ${existingNamesList}`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async updateSubspaceName(
|
||||
|
||||
@@ -180,18 +180,24 @@ export class TagModelService {
|
||||
productUuid: string,
|
||||
spaceModel: SpaceModelEntity,
|
||||
): Promise<void> {
|
||||
const isTagInSpaceModel = await this.tagModelRepository.exists({
|
||||
where: { tag, spaceModel, product: { uuid: productUuid } },
|
||||
});
|
||||
const isTagInSubspaceModel = await this.tagModelRepository.exists({
|
||||
where: {
|
||||
tag,
|
||||
subspaceModel: { spaceModel },
|
||||
product: { uuid: productUuid },
|
||||
},
|
||||
const tagExists = await this.tagModelRepository.exists({
|
||||
where: [
|
||||
{
|
||||
tag,
|
||||
spaceModel: { uuid: spaceModel.uuid },
|
||||
product: { uuid: productUuid },
|
||||
disabled: false,
|
||||
},
|
||||
{
|
||||
tag,
|
||||
subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
|
||||
product: { uuid: productUuid },
|
||||
disabled: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (isTagInSpaceModel || isTagInSubspaceModel) {
|
||||
if (tagExists) {
|
||||
throw new HttpException(`Tag can't be reused`, HttpStatus.CONFLICT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user