update in tag service

This commit is contained in:
hannathkadher
2024-12-24 11:33:58 +04:00
parent 1e47fffc0a
commit cb2778dce5
22 changed files with 705 additions and 117 deletions
@@ -13,7 +13,7 @@ export class CreateSubspaceModelDto {
subspaceName: string;
@ApiProperty({
description: 'List of tags associated with the subspace',
description: 'List of tag models associated with the subspace',
type: [CreateTagModelDto],
})
@IsArray()
@@ -3,7 +3,7 @@ import { IsNotEmpty, IsString } from 'class-validator';
export class CreateTagModelDto {
@ApiProperty({
description: 'Tag associated with the space or subspace',
description: 'Tag models associated with the space or subspace models',
example: 'Temperature Control',
})
@IsNotEmpty()
@@ -11,7 +11,7 @@ export class ModifyTagModelDto {
action: ModifyAction;
@ApiPropertyOptional({
description: 'UUID of the tag (required for update/delete)',
description: 'UUID of the tag model (required for update/delete)',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@@ -19,7 +19,7 @@ export class ModifyTagModelDto {
uuid?: string;
@ApiPropertyOptional({
description: 'Name of the tag (required for add/update)',
description: 'Name of the tag model (required for add/update)',
example: 'Temperature Sensor',
})
@IsOptional()
@@ -152,6 +152,14 @@ export class SpaceModelService {
queryRunner,
);
}
if (dto.tags) {
await this.tagModelService.modifyTags(
dto.tags,
queryRunner,
spaceModel,
);
}
await queryRunner.commitTransaction();
return new SuccessResponseDto({
@@ -4,16 +4,9 @@ import {
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
CreateSubspaceModelDto,
UpdateSubspaceModelDto,
CreateTagModelDto,
} from '../../dtos';
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
import { QueryRunner } from 'typeorm';
import {
IDeletedSubsaceModelInterface,
IUpdateSubspaceModelInterface,
} from 'src/space-model/interfaces';
import { IDeletedSubsaceModelInterface } from 'src/space-model/interfaces';
import {
DeleteSubspaceModelDto,
ModifySubspaceModelDto,
@@ -63,33 +56,6 @@ export class SubSpaceModelService {
return savedSubspaces;
}
async updateSubspaceModels(
updateDtos: UpdateSubspaceModelDto[],
queryRunner: QueryRunner,
): Promise<IUpdateSubspaceModelInterface[]> {
const updateResults: IUpdateSubspaceModelInterface[] = [];
for (const dto of updateDtos) {
await this.findOne(dto.subspaceUuid);
const updateResult: IUpdateSubspaceModelInterface = {
uuid: dto.subspaceUuid,
};
if (dto.subspaceName) {
await this.updateSubspaceName(
dto.subspaceUuid,
dto.subspaceName,
queryRunner,
);
updateResult.subspaceName = dto.subspaceName;
}
updateResults.push(updateResult);
}
return updateResults;
}
async deleteSubspaceModels(
deleteDtos: DeleteSubspaceModelDto[],
queryRunner: QueryRunner,
@@ -167,22 +133,23 @@ export class SubSpaceModelService {
}
private async handleUpdateAction(
subspace: ModifySubspaceModelDto,
modifyDto: ModifySubspaceModelDto,
queryRunner: QueryRunner,
): Promise<void> {
const existingSubspace = await this.findOne(subspace.uuid);
const subspace = await this.findOne(modifyDto.uuid);
if (subspace.subspaceName) {
existingSubspace.subspaceName = subspace.subspaceName;
await queryRunner.manager.save(existingSubspace);
}
await this.updateSubspaceName(
queryRunner,
subspace,
modifyDto.subspaceName,
);
if (subspace.tags?.length) {
if (modifyDto.tags?.length) {
await this.tagModelService.modifyTags(
subspace.tags,
modifyDto.tags,
queryRunner,
null,
existingSubspace,
subspace,
);
}
}
@@ -258,14 +225,13 @@ export class SubSpaceModelService {
}
private async updateSubspaceName(
uuid: string,
subspaceName: string,
queryRunner: QueryRunner,
subSpaceModel: SubspaceModelEntity,
subspaceName?: string,
): Promise<void> {
await queryRunner.manager.update(
this.subspaceModelRepository.target,
{ uuid },
{ subspaceName },
);
if (subspaceName) {
subSpaceModel.subspaceName = subspaceName;
await queryRunner.manager.save(subSpaceModel);
}
}
}
@@ -169,7 +169,7 @@ export class TagModelService {
}
throw new HttpException(
`An error occurred while modifying tags: ${error.message}`,
`An error occurred while modifying tag models: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@@ -236,7 +236,7 @@ export class TagModelService {
});
if (!tag) {
throw new HttpException(
`Tag with ID ${uuid} not found.`,
`Tag model with ID ${uuid} not found.`,
HttpStatus.NOT_FOUND,
);
}