mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
service clean up
This commit is contained in:
@ -1 +0,0 @@
|
|||||||
export * from './services';
|
|
@ -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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,2 +0,0 @@
|
|||||||
export * from './base-product-item-model.service';
|
|
||||||
export * from './base-product-model.service';
|
|
@ -2,6 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
|
|||||||
import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator';
|
import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
|
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
|
||||||
|
import { CreateTagModelDto } from './tag-model-dtos/create-tag-model.dto';
|
||||||
|
|
||||||
export class CreateSpaceModelDto {
|
export class CreateSpaceModelDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -20,4 +21,13 @@ export class CreateSpaceModelDto {
|
|||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => CreateSubspaceModelDto)
|
@Type(() => CreateSubspaceModelDto)
|
||||||
subspaceModels?: CreateSubspaceModelDto[];
|
subspaceModels?: CreateSubspaceModelDto[];
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of tags associated with the space model',
|
||||||
|
type: [CreateTagModelDto],
|
||||||
|
})
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => CreateTagModelDto)
|
||||||
|
tags?: CreateTagModelDto[];
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
export * from './create-space-model.dto';
|
export * from './create-space-model.dto';
|
||||||
export * from './product-item-model-dtos';
|
|
||||||
export * from './product-model-dtos';
|
|
||||||
export * from './project-param.dto';
|
export * from './project-param.dto';
|
||||||
export * from './update-space-model.dto';
|
export * from './update-space-model.dto';
|
||||||
export * from './space-model-param';
|
export * from './space-model-param';
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
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 {
|
export class CreateSubspaceModelDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -9,4 +11,13 @@ export class CreateSubspaceModelDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
subspaceName: string;
|
subspaceName: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of tags associated with the subspace',
|
||||||
|
type: [CreateTagModelDto],
|
||||||
|
})
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => CreateTagModelDto)
|
||||||
|
tags?: CreateTagModelDto[];
|
||||||
}
|
}
|
||||||
|
20
src/space-model/dtos/tag-model-dtos/create-tag-model.dto.ts
Normal file
20
src/space-model/dtos/tag-model-dtos/create-tag-model.dto.ts
Normal 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;
|
||||||
|
}
|
0
src/space-model/dtos/tag-model-dtos/index.ts
Normal file
0
src/space-model/dtos/tag-model-dtos/index.ts
Normal file
Reference in New Issue
Block a user