mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
updated create space model with query runner
This commit is contained in:
@ -6,10 +6,12 @@ import { ProjectParam } from 'src/community/dtos';
|
|||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
import { SubSpaceModelService } from './subspace-model.service';
|
import { SubSpaceModelService } from './subspace-model.service';
|
||||||
import { SpaceProductModelService } from './space-product-model.service';
|
import { SpaceProductModelService } from './space-product-model.service';
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceModelService {
|
export class SpaceModelService {
|
||||||
constructor(
|
constructor(
|
||||||
|
private readonly dataSource: DataSource,
|
||||||
private readonly spaceModelRepository: SpaceModelRepository,
|
private readonly spaceModelRepository: SpaceModelRepository,
|
||||||
private readonly projectRepository: ProjectRepository,
|
private readonly projectRepository: ProjectRepository,
|
||||||
private readonly subSpaceModelService: SubSpaceModelService,
|
private readonly subSpaceModelService: SubSpaceModelService,
|
||||||
@ -24,6 +26,11 @@ export class SpaceModelService {
|
|||||||
createSpaceModelDto;
|
createSpaceModelDto;
|
||||||
const project = await this.validateProject(params.projectUuid);
|
const project = await this.validateProject(params.projectUuid);
|
||||||
|
|
||||||
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
|
||||||
|
await queryRunner.connect();
|
||||||
|
await queryRunner.startTransaction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isModelExist = await this.validateName(
|
const isModelExist = await this.validateName(
|
||||||
modelName,
|
modelName,
|
||||||
@ -40,17 +47,24 @@ export class SpaceModelService {
|
|||||||
modelName,
|
modelName,
|
||||||
project,
|
project,
|
||||||
});
|
});
|
||||||
const savedSpaceModel = await this.spaceModelRepository.save(spaceModel);
|
const savedSpaceModel = await queryRunner.manager.save(spaceModel);
|
||||||
|
|
||||||
await this.subSpaceModelService.createSubSpaceModels(
|
if (subspaceModels) {
|
||||||
subspaceModels,
|
await this.subSpaceModelService.createSubSpaceModels(
|
||||||
savedSpaceModel,
|
subspaceModels,
|
||||||
);
|
savedSpaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
await this.spaceProductModelService.createSpaceProductModels(
|
if (spaceProductModels) {
|
||||||
spaceProductModels,
|
await this.spaceProductModelService.createSpaceProductModels(
|
||||||
savedSpaceModel,
|
spaceProductModels,
|
||||||
);
|
savedSpaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await queryRunner.commitTransaction();
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: `Successfully created new space model with uuid ${savedSpaceModel.uuid}`,
|
message: `Successfully created new space model with uuid ${savedSpaceModel.uuid}`,
|
||||||
@ -58,6 +72,8 @@ export class SpaceModelService {
|
|||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
await queryRunner.rollbackTransaction();
|
||||||
|
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@ -66,6 +82,8 @@ export class SpaceModelService {
|
|||||||
error.message || `An unexpected error occurred`,
|
error.message || `An unexpected error occurred`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import {
|
|||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { CreateSpaceProductItemModelDto } from '../dtos';
|
import { CreateSpaceProductItemModelDto } from '../dtos';
|
||||||
|
import { QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceProductItemModelService {
|
export class SpaceProductItemModelService {
|
||||||
@ -16,41 +17,37 @@ export class SpaceProductItemModelService {
|
|||||||
itemModelDtos: CreateSpaceProductItemModelDto[],
|
itemModelDtos: CreateSpaceProductItemModelDto[],
|
||||||
spaceProductModel: SpaceProductModelEntity,
|
spaceProductModel: SpaceProductModelEntity,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
try {
|
await this.validateTags(itemModelDtos, spaceModel, queryRunner);
|
||||||
await this.validateTags(itemModelDtos, spaceModel);
|
|
||||||
|
|
||||||
for (const itemModelDto of itemModelDtos) {
|
try {
|
||||||
await this.create(itemModelDto, spaceProductModel, spaceModel);
|
const productItems = itemModelDtos.map((dto) =>
|
||||||
}
|
queryRunner.manager.create(this.spaceProductItemRepository.target, {
|
||||||
|
tag: dto.tag,
|
||||||
|
spaceProductModel,
|
||||||
|
spaceModel,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.manager.save(productItems);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || `An unexpected error occurred`,
|
error.message ||
|
||||||
|
'An unexpected error occurred while creating product items.',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(
|
private async validateTags(
|
||||||
itemModelDto: CreateSpaceProductItemModelDto,
|
|
||||||
spaceProductModel: SpaceProductModelEntity,
|
|
||||||
spaceModel: SpaceModelEntity,
|
|
||||||
) {
|
|
||||||
const productItem = this.spaceProductItemRepository.create({
|
|
||||||
tag: itemModelDto.tag,
|
|
||||||
spaceProductModel,
|
|
||||||
spaceModel,
|
|
||||||
});
|
|
||||||
await this.spaceProductItemRepository.save(productItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
async validateTags(
|
|
||||||
itemModelDtos: CreateSpaceProductItemModelDto[],
|
itemModelDtos: CreateSpaceProductItemModelDto[],
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
const incomingTags = itemModelDtos.map((item) => item.tag);
|
const incomingTags = itemModelDtos.map((item) => item.tag);
|
||||||
|
|
||||||
@ -59,15 +56,18 @@ export class SpaceProductItemModelService {
|
|||||||
);
|
);
|
||||||
if (duplicateTags.length > 0) {
|
if (duplicateTags.length > 0) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Duplicate tags found in request: ${duplicateTags.join(', ')}`,
|
`Duplicate tags found in the request: ${[...new Set(duplicateTags)].join(', ')}`,
|
||||||
HttpStatus.CONFLICT,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingTags = await this.spaceProductItemRepository.find({
|
const existingTags = await queryRunner.manager.find(
|
||||||
where: { spaceModel },
|
this.spaceProductItemRepository.target,
|
||||||
select: ['tag'],
|
{
|
||||||
});
|
where: { spaceModel },
|
||||||
|
select: ['tag'],
|
||||||
|
},
|
||||||
|
);
|
||||||
const existingTagSet = new Set(existingTags.map((item) => item.tag));
|
const existingTagSet = new Set(existingTags.map((item) => item.tag));
|
||||||
|
|
||||||
const conflictingTags = incomingTags.filter((tag) =>
|
const conflictingTags = incomingTags.filter((tag) =>
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|||||||
import { CreateSpaceProductModelDto } from '../dtos';
|
import { CreateSpaceProductModelDto } from '../dtos';
|
||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
import { SpaceProductItemModelService } from './space-product-item-model.service';
|
import { SpaceProductItemModelService } from './space-product-item-model.service';
|
||||||
|
import { QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceProductModelService {
|
export class SpaceProductModelService {
|
||||||
@ -18,60 +19,66 @@ export class SpaceProductModelService {
|
|||||||
async createSpaceProductModels(
|
async createSpaceProductModels(
|
||||||
spaceProductModelDtos: CreateSpaceProductModelDto[],
|
spaceProductModelDtos: CreateSpaceProductModelDto[],
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
for (const spaceProductModelDto of spaceProductModelDtos) {
|
const productModels = await Promise.all(
|
||||||
await this.create(spaceProductModelDto, spaceModel);
|
spaceProductModelDtos.map(async (dto) => {
|
||||||
}
|
this.validateProductCount(dto);
|
||||||
|
const product = await this.getProduct(dto.productId);
|
||||||
|
return queryRunner.manager.create(
|
||||||
|
this.spaceProductModelRepository.target,
|
||||||
|
{
|
||||||
|
product,
|
||||||
|
productCount: dto.productCount,
|
||||||
|
spaceModel,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const savedProductModels = await queryRunner.manager.save(productModels);
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
spaceProductModelDtos.map((dto, index) =>
|
||||||
|
this.spaceProductItemModelService.createProdutItemModel(
|
||||||
|
dto.items,
|
||||||
|
savedProductModels[index],
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || `An unexpected error occurred`,
|
error.message ||
|
||||||
|
'An unexpected error occurred while creating product models.',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(
|
private validateProductCount(dto: CreateSpaceProductModelDto) {
|
||||||
spaceProductModelDto: CreateSpaceProductModelDto,
|
const productItemCount = dto.items.length;
|
||||||
spaceModel: SpaceModelEntity,
|
if (dto.productCount !== productItemCount) {
|
||||||
) {
|
|
||||||
this.validateCount(spaceProductModelDto);
|
|
||||||
|
|
||||||
const product = await this.productRepository.findOneBy({
|
|
||||||
uuid: spaceProductModelDto.productId,
|
|
||||||
});
|
|
||||||
if (!product) {
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Product with ID ${spaceProductModelDto.productId} not found`,
|
`Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productId}.`,
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const spaceProductModel = this.spaceProductModelRepository.create({
|
|
||||||
product,
|
|
||||||
productCount: spaceProductModelDto.productCount,
|
|
||||||
spaceModel: spaceModel,
|
|
||||||
});
|
|
||||||
const newProductModel =
|
|
||||||
await this.spaceProductModelRepository.save(spaceProductModel);
|
|
||||||
|
|
||||||
await this.spaceProductItemModelService.createProdutItemModel(
|
|
||||||
spaceProductModelDto.items,
|
|
||||||
newProductModel,
|
|
||||||
spaceModel,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private validateCount(spaceProductModelDto: CreateSpaceProductModelDto) {
|
|
||||||
const productItemCount = spaceProductModelDto.items.length;
|
|
||||||
if (spaceProductModelDto.productCount !== productItemCount) {
|
|
||||||
throw new HttpException(
|
|
||||||
`Product count (${spaceProductModelDto.productCount}) does not match the number of items (${productItemCount}) for product ID ${spaceProductModelDto.productId}.`,
|
|
||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getProduct(productId: string) {
|
||||||
|
const product = await this.productRepository.findOneBy({ uuid: productId });
|
||||||
|
if (!product) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Product with ID ${productId} not found.`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return product;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import {
|
|||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { CreateSubspaceModelDto } from '../dtos';
|
import { CreateSubspaceModelDto } from '../dtos';
|
||||||
|
import { QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SubSpaceModelService {
|
export class SubSpaceModelService {
|
||||||
@ -14,16 +15,19 @@ export class SubSpaceModelService {
|
|||||||
async createSubSpaceModels(
|
async createSubSpaceModels(
|
||||||
subSpaceModelDtos: CreateSubspaceModelDto[],
|
subSpaceModelDtos: CreateSubspaceModelDto[],
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
|
this.validateInputDtos(subSpaceModelDtos);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.validateInputDtos(subSpaceModelDtos);
|
const subspaces = subSpaceModelDtos.map((subspaceDto) =>
|
||||||
for (const subspaceDto of subSpaceModelDtos) {
|
queryRunner.manager.create(this.subspaceModelRepository.target, {
|
||||||
const subspace = this.subspaceModelRepository.create({
|
|
||||||
subspaceName: subspaceDto.subspaceName,
|
subspaceName: subspaceDto.subspaceName,
|
||||||
spaceModel: spaceModel,
|
spaceModel: spaceModel,
|
||||||
});
|
}),
|
||||||
await this.subspaceModelRepository.save(subspace);
|
);
|
||||||
}
|
|
||||||
|
await queryRunner.manager.save(subspaces);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user