mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:24:53 +00:00
fixed delete
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class CreateTagModelDto {
|
export class CreateTagModelDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -10,6 +10,14 @@ export class CreateTagModelDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
tag: string;
|
tag: string;
|
||||||
|
|
||||||
|
@ApiPropertyOptional({
|
||||||
|
description: 'UUID of the tag model (required for update/delete)',
|
||||||
|
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
uuid?: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'ID of the product associated with the tag',
|
description: 'ID of the product associated with the tag',
|
||||||
example: '123e4567-e89b-12d3-a456-426614174000',
|
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||||
|
|||||||
@ -89,6 +89,8 @@ export class SpaceModelService {
|
|||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(JSON.stringify(createSpaceModelDto));
|
||||||
|
|
||||||
await queryRunner.rollbackTransaction();
|
await queryRunner.rollbackTransaction();
|
||||||
|
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
@ -177,7 +179,6 @@ export class SpaceModelService {
|
|||||||
|
|
||||||
async update(dto: UpdateSpaceModelDto, param: SpaceModelParam) {
|
async update(dto: UpdateSpaceModelDto, param: SpaceModelParam) {
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
|
||||||
await this.validateProject(param.projectUuid);
|
await this.validateProject(param.projectUuid);
|
||||||
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
|
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
@ -201,6 +202,12 @@ export class SpaceModelService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const spaceTagsAfterMove = this.tagModelService.getSubspaceTagsToBeAdded(
|
||||||
|
dto.tags,
|
||||||
|
dto.subspaceModels,
|
||||||
|
);
|
||||||
|
console.log(spaceTagsAfterMove);
|
||||||
|
|
||||||
if (dto.subspaceModels) {
|
if (dto.subspaceModels) {
|
||||||
modifiedSubspaceModels =
|
modifiedSubspaceModels =
|
||||||
await this.subSpaceModelService.modifySubSpaceModels(
|
await this.subSpaceModelService.modifySubSpaceModels(
|
||||||
@ -212,9 +219,10 @@ export class SpaceModelService {
|
|||||||
|
|
||||||
if (dto.tags) {
|
if (dto.tags) {
|
||||||
modifiedTagsModelPayload = await this.tagModelService.modifyTags(
|
modifiedTagsModelPayload = await this.tagModelService.modifyTags(
|
||||||
dto.tags,
|
spaceTagsAfterMove,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,8 +243,8 @@ export class SpaceModelService {
|
|||||||
message: 'SpaceModel updated successfully',
|
message: 'SpaceModel updated successfully',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(JSON.stringify(dto));
|
||||||
await queryRunner.rollbackTransaction();
|
await queryRunner.rollbackTransaction();
|
||||||
|
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,8 +50,7 @@ export class SubSpaceModelService {
|
|||||||
const otherDtoTags = subSpaceModelDtos
|
const otherDtoTags = subSpaceModelDtos
|
||||||
.filter((_, i) => i !== index)
|
.filter((_, i) => i !== index)
|
||||||
.flatMap((otherDto) => otherDto.tags || []);
|
.flatMap((otherDto) => otherDto.tags || []);
|
||||||
|
if (dto.tags && dto.tags.length > 0) {
|
||||||
if (dto.tags?.length) {
|
|
||||||
subspace.tags = await this.tagModelService.createTags(
|
subspace.tags = await this.tagModelService.createTags(
|
||||||
dto.tags,
|
dto.tags,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
@ -174,6 +173,7 @@ export class SubSpaceModelService {
|
|||||||
const createTagDtos: CreateTagModelDto[] =
|
const createTagDtos: CreateTagModelDto[] =
|
||||||
subspace.tags?.map((tag) => ({
|
subspace.tags?.map((tag) => ({
|
||||||
tag: tag.tag,
|
tag: tag.tag,
|
||||||
|
uuid: tag.uuid,
|
||||||
productUuid: tag.productUuid,
|
productUuid: tag.productUuid,
|
||||||
})) || [];
|
})) || [];
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,11 @@ import {
|
|||||||
} from '@app/common/modules/space-model/entities';
|
} from '@app/common/modules/space-model/entities';
|
||||||
import { SubspaceModelEntity } from '@app/common/modules/space-model/entities';
|
import { SubspaceModelEntity } from '@app/common/modules/space-model/entities';
|
||||||
import { TagModelRepository } from '@app/common/modules/space-model';
|
import { TagModelRepository } from '@app/common/modules/space-model';
|
||||||
import { CreateTagModelDto, ModifyTagModelDto } from '../dtos';
|
import {
|
||||||
|
CreateTagModelDto,
|
||||||
|
ModifySubspaceModelDto,
|
||||||
|
ModifyTagModelDto,
|
||||||
|
} from '../dtos';
|
||||||
import { ProductService } from 'src/product/services';
|
import { ProductService } from 'src/product/services';
|
||||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||||
import { ModifiedTagsModelPayload } from '../interfaces';
|
import { ModifiedTagsModelPayload } from '../interfaces';
|
||||||
@ -39,11 +43,55 @@ export class TagModelService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tagEntitiesToCreate = tags.filter((tagDto) => tagDto.uuid === null);
|
||||||
|
const tagEntitiesToUpdate = tags.filter((tagDto) => tagDto.uuid !== null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const createdTags = await this.bulkSaveTags(
|
||||||
|
tagEntitiesToCreate,
|
||||||
|
queryRunner,
|
||||||
|
spaceModel,
|
||||||
|
subspaceModel,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update existing tags
|
||||||
|
const updatedTags = await this.moveTags(
|
||||||
|
tagEntitiesToUpdate,
|
||||||
|
queryRunner,
|
||||||
|
spaceModel,
|
||||||
|
subspaceModel,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Combine created and updated tags
|
||||||
|
return [...createdTags, ...updatedTags];
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new HttpException(
|
||||||
|
`Failed to create tag models due to an unexpected error.: ${error}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async bulkSaveTags(
|
||||||
|
tags: CreateTagModelDto[],
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
spaceModel?: SpaceModelEntity,
|
||||||
|
subspaceModel?: SubspaceModelEntity,
|
||||||
|
): Promise<TagModel[]> {
|
||||||
|
if (!tags.length) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
const tagEntities = await Promise.all(
|
const tagEntities = await Promise.all(
|
||||||
tags.map(async (tagDto) =>
|
tags.map((tagDto) =>
|
||||||
this.prepareTagEntity(tagDto, queryRunner, spaceModel, subspaceModel),
|
this.prepareTagEntity(tagDto, queryRunner, spaceModel, subspaceModel),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await queryRunner.manager.save(tagEntities);
|
return await queryRunner.manager.save(tagEntities);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -52,12 +100,46 @@ export class TagModelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Failed to save tag models due to an unexpected error.',
|
`Failed to save tag models due to an unexpected error: ${error.message}`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async moveTags(
|
||||||
|
tags: CreateTagModelDto[],
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
spaceModel?: SpaceModelEntity,
|
||||||
|
subspaceModel?: SubspaceModelEntity,
|
||||||
|
): Promise<TagModel[]> {
|
||||||
|
if (!tags.length) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return await Promise.all(
|
||||||
|
tags.map(async (tagDto) => {
|
||||||
|
const tag = await this.getTagByUuid(tagDto.uuid);
|
||||||
|
if (!tag) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Tag with UUID ${tagDto.uuid} not found.`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subspaceModel && subspaceModel.spaceModel) {
|
||||||
|
await queryRunner.manager.update(
|
||||||
|
this.tagModelRepository.target,
|
||||||
|
{ uuid: tag.uuid },
|
||||||
|
{ subspaceModel },
|
||||||
|
);
|
||||||
|
tag.subspaceModel = subspaceModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
async updateTag(
|
async updateTag(
|
||||||
tag: ModifyTagModelDto,
|
tag: ModifyTagModelDto,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
@ -153,6 +235,7 @@ export class TagModelService {
|
|||||||
if (tag.action === ModifyAction.ADD) {
|
if (tag.action === ModifyAction.ADD) {
|
||||||
const createTagDto: CreateTagModelDto = {
|
const createTagDto: CreateTagModelDto = {
|
||||||
tag: tag.tag as string,
|
tag: tag.tag as string,
|
||||||
|
uuid: tag.uuid,
|
||||||
productUuid: tag.productUuid as string,
|
productUuid: tag.productUuid as string,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -268,7 +351,6 @@ export class TagModelService {
|
|||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return queryRunner.manager.create(TagModel, {
|
return queryRunner.manager.create(TagModel, {
|
||||||
tag: tagDto.tag,
|
tag: tagDto.tag,
|
||||||
product: product.data,
|
product: product.data,
|
||||||
@ -333,4 +415,38 @@ export class TagModelService {
|
|||||||
|
|
||||||
return existingTag;
|
return existingTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSubspaceTagsToBeAdded(
|
||||||
|
spaceTags: ModifyTagModelDto[],
|
||||||
|
subspaceModels: ModifySubspaceModelDto[],
|
||||||
|
): ModifyTagModelDto[] {
|
||||||
|
if (!subspaceModels || subspaceModels.length === 0) {
|
||||||
|
return spaceTags;
|
||||||
|
}
|
||||||
|
|
||||||
|
const spaceTagsToDelete = spaceTags.filter(
|
||||||
|
(tag) => tag.action === 'delete',
|
||||||
|
);
|
||||||
|
|
||||||
|
const tagsToAdd = subspaceModels.flatMap(
|
||||||
|
(subspace) => subspace.tags?.filter((tag) => tag.action === 'add') || [],
|
||||||
|
);
|
||||||
|
|
||||||
|
const commonTagUuids = new Set(
|
||||||
|
tagsToAdd
|
||||||
|
.filter((tagToAdd) =>
|
||||||
|
spaceTagsToDelete.some(
|
||||||
|
(tagToDelete) => tagToAdd.uuid === tagToDelete.uuid,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.map((tag) => tag.uuid),
|
||||||
|
);
|
||||||
|
|
||||||
|
const remainingTags = spaceTags.filter(
|
||||||
|
(tag) =>
|
||||||
|
!tag.uuid || commonTagUuids.size === 0 || commonTagUuids.has(tag.uuid),
|
||||||
|
);
|
||||||
|
|
||||||
|
return remainingTags;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user