new bulk save tags

This commit is contained in:
hannathkadher
2025-02-15 11:33:18 +04:00
parent 9eb9b92be3
commit e276d4651d
3 changed files with 84 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsArray, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { CreateTagDto } from './tags.dto';
export class BulkCreateTagsDto {
@ApiProperty({
description: 'Project UUID for which the tags are being created',
example: '760e8400-e29b-41d4-a716-446655440001',
})
@IsNotEmpty()
@IsString()
projectUuid: string;
@ApiProperty({
description: 'List of tags to be created',
type: [CreateTagDto],
})
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateTagDto)
tags: CreateTagDto[];
}
+1
View File
@@ -1 +1,2 @@
export * from './tags.dto';
export * from './bulk-create-tag.dto';
+60
View File
@@ -13,6 +13,8 @@ import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { NewTagEntity } from '@app/common/modules/tag';
import { GetTagsParam } from '../dtos/get-tags.param';
import { BulkCreateTagsDto } from '../dtos';
import { In } from 'typeorm';
@Injectable()
export class TagService {
@@ -21,6 +23,7 @@ export class TagService {
private readonly productRepository: ProductRepository,
private readonly projectRepository: ProjectRepository,
) {}
async getTagsByProjectUuid(params: GetTagsParam): Promise<BaseResponseDto> {
const { projectUuid } = params;
await this.getProjectByUuid(projectUuid);
@@ -58,6 +61,36 @@ export class TagService {
});
}
async bulkCreateTags(dto: BulkCreateTagsDto): Promise<BaseResponseDto> {
const { projectUuid, tags } = dto;
const project = await this.getProjectByUuid(projectUuid);
const productUuids = tags.map((tag) => tag.productUuid);
const productMap = await this.getProductMap(productUuids);
const existingTagNames = await this.getExistingTagNames(projectUuid);
const newTags: NewTagEntity[] = tags
.filter((tag) => !existingTagNames.has(tag.name))
.map((tag) =>
this.tagRepository.create({
name: tag.name,
product: productMap.get(tag.productUuid),
project,
}),
);
if (newTags.length > 0) {
await this.tagRepository.save(newTags);
}
return new SuccessResponseDto({
message: `Tags processed successfully`,
data: newTags,
});
}
private async getProductByUuid(uuid: string): Promise<ProductEntity> {
const product = await this.productRepository.findOne({ where: { uuid } });
if (!product) {
@@ -88,4 +121,31 @@ export class TagService {
);
}
}
private async getProductMap(
uuids: string[],
): Promise<Map<string, ProductEntity>> {
const products = await this.productRepository.find({
where: { uuid: In(uuids) },
});
if (products.length !== uuids.length) {
const foundUuids = new Set(products.map((p) => p.uuid));
const missingUuids = uuids.filter((id) => !foundUuids.has(id));
throw new NotFoundException(
`Products not found for UUIDs: ${missingUuids.join(', ')}`,
);
}
return;
}
private async getExistingTagNames(projectUuid: string): Promise<Set<string>> {
const tags = await this.tagRepository.find({
where: { project: { uuid: projectUuid } },
select: ['name'],
});
return new Set(tags.map((tag) => tag.name));
}
}