mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-12-01 02:04:55 +00:00
return new SubspaceModels onn space update
This commit is contained in:
@ -1,6 +1,20 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsOptional, IsString } from 'class-validator';
|
import { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';
|
||||||
|
import { CreateSubspaceModelDto } from './create-subspace-model.dto';
|
||||||
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
|
export class UpdateSubspaceModelDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of subspaces to add',
|
||||||
|
type: [CreateSubspaceModelDto],
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => CreateSubspaceModelDto)
|
||||||
|
add?: CreateSubspaceModelDto[];
|
||||||
|
}
|
||||||
export class UpdateSpaceModelDto {
|
export class UpdateSpaceModelDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Updated name of the space model',
|
description: 'Updated name of the space model',
|
||||||
@ -9,4 +23,7 @@ export class UpdateSpaceModelDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
modelName?: string;
|
modelName?: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
subspaceModels?: UpdateSubspaceModelDto;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -146,6 +146,15 @@ export class SpaceModelService {
|
|||||||
|
|
||||||
await queryRunner.manager.save(spaceModel);
|
await queryRunner.manager.save(spaceModel);
|
||||||
|
|
||||||
|
if (dto.subspaceModels) {
|
||||||
|
const updatedSubspaces =
|
||||||
|
await this.subSpaceModelService.updateSubSpaceModels(
|
||||||
|
dto.subspaceModels,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import {
|
|||||||
SubspaceModelRepository,
|
SubspaceModelRepository,
|
||||||
} 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, UpdateSubspaceModelDto } from '../../dtos';
|
||||||
import { QueryRunner } from 'typeorm';
|
import { QueryRunner } from 'typeorm';
|
||||||
import { SubspaceProductModelService } from './subspace-product-model.service';
|
import { SubspaceProductModelService } from './subspace-product-model.service';
|
||||||
|
|
||||||
@ -29,19 +29,25 @@ export class SubSpaceModelService {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
await queryRunner.manager.save(subspaces);
|
const newSubspaces = await queryRunner.manager.save(subspaces);
|
||||||
|
|
||||||
await Promise.all(
|
const addedSubspaces = await Promise.all(
|
||||||
subSpaceModelDtos.map((dto, index) => {
|
subSpaceModelDtos.map(async (dto, index) => {
|
||||||
const subspaceModel = subspaces[index];
|
const subspaceModel = newSubspaces[index];
|
||||||
return this.subSpaceProducetModelService.createSubspaceProductModels(
|
const productModels =
|
||||||
dto.spaceProductModels,
|
await this.subSpaceProducetModelService.createSubspaceProductModels(
|
||||||
spaceModel,
|
dto.spaceProductModels,
|
||||||
|
spaceModel,
|
||||||
|
subspaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
return {
|
||||||
subspaceModel,
|
subspaceModel,
|
||||||
queryRunner,
|
productModels,
|
||||||
);
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
return addedSubspaces;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
@ -61,7 +67,6 @@ export class SubSpaceModelService {
|
|||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const incomingNames = subSpaceModelDtos.map((dto) => dto.subspaceName);
|
const incomingNames = subSpaceModelDtos.map((dto) => dto.subspaceName);
|
||||||
this.validateName(incomingNames);
|
this.validateName(incomingNames);
|
||||||
}
|
}
|
||||||
@ -85,4 +90,28 @@ export class SubSpaceModelService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateSubSpaceModels(
|
||||||
|
dto: UpdateSubspaceModelDto,
|
||||||
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
) {
|
||||||
|
const subspaces: { new?: any } = {};
|
||||||
|
try {
|
||||||
|
if (dto.add) {
|
||||||
|
const addedSubspaces = await this.createSubSpaceModels(
|
||||||
|
dto.add,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
subspaces.new = addedSubspaces;
|
||||||
|
}
|
||||||
|
return subspaces;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Failed to update SpaceModel',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|||||||
import { QueryRunner } from 'typeorm';
|
import { QueryRunner } from 'typeorm';
|
||||||
import {
|
import {
|
||||||
SpaceModelEntity,
|
SpaceModelEntity,
|
||||||
|
SubspaceProductItemModelEntity,
|
||||||
SubspaceProductItemModelRepository,
|
SubspaceProductItemModelRepository,
|
||||||
SubspaceProductModelEntity,
|
SubspaceProductModelEntity,
|
||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
@ -21,7 +22,7 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
|
|||||||
subspaceProductModel: SubspaceProductModelEntity,
|
subspaceProductModel: SubspaceProductModelEntity,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
) {
|
): Promise<SubspaceProductItemModelEntity[]> {
|
||||||
if (!subspaceProductModel) {
|
if (!subspaceProductModel) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'The spaceProductModel parameter is required but was not provided.',
|
'The spaceProductModel parameter is required but was not provided.',
|
||||||
@ -38,6 +39,7 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await queryRunner.manager.save(productItems);
|
await queryRunner.manager.save(productItems);
|
||||||
|
return productItems;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@ -27,6 +27,8 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
|||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
if (!spaceProductModelDtos?.length) return;
|
||||||
|
|
||||||
const productModels = await Promise.all(
|
const productModels = await Promise.all(
|
||||||
spaceProductModelDtos.map(async (dto) => {
|
spaceProductModelDtos.map(async (dto) => {
|
||||||
this.validateProductCount(dto);
|
this.validateProductCount(dto);
|
||||||
@ -44,17 +46,23 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
|||||||
|
|
||||||
const savedProductModels = await queryRunner.manager.save(productModels);
|
const savedProductModels = await queryRunner.manager.save(productModels);
|
||||||
|
|
||||||
await Promise.all(
|
const newProductModels = await Promise.all(
|
||||||
spaceProductModelDtos.map((dto, index) => {
|
spaceProductModelDtos.map(async (dto, index) => {
|
||||||
const savedModel = savedProductModels[index];
|
const savedModel = savedProductModels[index];
|
||||||
return this.subspaceProductItemModelService.createProdutItemModel(
|
const productItemModels =
|
||||||
dto.items,
|
await this.subspaceProductItemModelService.createProdutItemModel(
|
||||||
savedModel, // Pass the saved model
|
dto.items,
|
||||||
spaceModel,
|
savedModel,
|
||||||
queryRunner,
|
spaceModel,
|
||||||
);
|
queryRunner,
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
productModel: savedModel,
|
||||||
|
productItemModels,
|
||||||
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
return newProductModels;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user