service clean up

This commit is contained in:
hannathkadher
2024-12-23 08:53:01 +04:00
parent 4545b2e84c
commit 4b55c4e39c
9 changed files with 42 additions and 95 deletions

View File

@ -1 +0,0 @@
export * from './services';

View File

@ -1,79 +0,0 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { QueryRunner } from 'typeorm';
import { SpaceModelEntity } from '@app/common/modules/space-model';
import { CreateProductItemModelDto } from 'src/space-model/dtos';
export abstract class BaseProductItemService {
async validateTags(
itemModelDtos: CreateProductItemModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
): Promise<void> {
const incomingTags = new Set(
itemModelDtos.map((item) => item.tag).filter(Boolean),
);
const duplicateTags = itemModelDtos
.map((item) => item.tag)
.filter((tag, index, array) => array.indexOf(tag) !== index);
if (duplicateTags.length > 0) {
throw new HttpException(
`Duplicate tags found in the request: ${[...new Set(duplicateTags)].join(', ')}`,
HttpStatus.BAD_REQUEST,
);
}
const existingTagsQuery = `
SELECT DISTINCT tag
FROM (
SELECT spi.tag
FROM "subspace-product-item-model" spi
INNER JOIN "subspace-product-model" spm ON spi.subspace_product_model_uuid = spm.uuid
INNER JOIN "subspace-model" sm ON spm.subspace_model_uuid = sm.uuid
WHERE sm.space_model_uuid = $1
UNION
SELECT spi.tag
FROM "space-product-item-model" spi
INNER JOIN "space-product-model" spm ON spi.space_product_model_uuid = spm.uuid
WHERE spm.space_model_uuid = $1
) AS combined_tags;
`;
const existingTags = await queryRunner.manager.query(existingTagsQuery, [
spaceModel.uuid,
]);
const existingTagsSet = new Set(
existingTags.map((row: { tag: string }) => row.tag),
);
const conflictingTags = [...incomingTags].filter((tag) =>
existingTagsSet.has(tag),
);
if (conflictingTags.length > 0) {
throw new HttpException(
`Tags already exist in the model: ${conflictingTags.join(', ')}`,
HttpStatus.CONFLICT,
);
}
}
protected async saveProductItems<T>(
productItems: T[],
targetRepository: any,
queryRunner: QueryRunner,
): Promise<T[]> {
try {
const savedItem = await queryRunner.manager.save(
targetRepository,
productItems,
);
return savedItem;
} catch (error) {
throw new HttpException(
error.message || 'An error occurred while creating product items.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -1,10 +0,0 @@
import { ProductService } from '../../../product/services';
export abstract class BaseProductModelService {
constructor(private readonly productService: ProductService) {}
protected async getProduct(productId: string) {
const product = await this.productService.findOne(productId);
return product.data;
}
}

View File

@ -1,2 +0,0 @@
export * from './base-product-item-model.service';
export * from './base-product-model.service';

View File

@ -2,6 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
import { CreateTagModelDto } from './tag-model-dtos/create-tag-model.dto';
export class CreateSpaceModelDto {
@ApiProperty({
@ -20,4 +21,13 @@ export class CreateSpaceModelDto {
@ValidateNested({ each: true })
@Type(() => CreateSubspaceModelDto)
subspaceModels?: CreateSubspaceModelDto[];
@ApiProperty({
description: 'List of tags associated with the space model',
type: [CreateTagModelDto],
})
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateTagModelDto)
tags?: CreateTagModelDto[];
}

View File

@ -1,6 +1,4 @@
export * from './create-space-model.dto';
export * from './product-item-model-dtos';
export * from './product-model-dtos';
export * from './project-param.dto';
export * from './update-space-model.dto';
export * from './space-model-param';

View File

@ -1,5 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { IsArray, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { CreateTagModelDto } from '../tag-model-dtos/create-tag-model.dto';
import { Type } from 'class-transformer';
export class CreateSubspaceModelDto {
@ApiProperty({
@ -9,4 +11,13 @@ export class CreateSubspaceModelDto {
@IsNotEmpty()
@IsString()
subspaceName: string;
@ApiProperty({
description: 'List of tags associated with the subspace',
type: [CreateTagModelDto],
})
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateTagModelDto)
tags?: CreateTagModelDto[];
}

View File

@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class CreateTagModelDto {
@ApiProperty({
description: 'Tag associated with the space or subspace',
example: 'Temperature Control',
})
@IsNotEmpty()
@IsString()
tag: string;
@ApiProperty({
description: 'ID of the product associated with the tag',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsNotEmpty()
@IsString()
productUuid: string;
}