mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 10:44:54 +00:00
added subspace delete
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import { ICommand } from '@nestjs/cqrs';
|
import { ICommand } from '@nestjs/cqrs';
|
||||||
import { UpdatedSubspaceModelInterface } from '../interfaces';
|
import { IModifySubspaceModelInterface } from '../interfaces';
|
||||||
|
|
||||||
export class PropogateSubspaceCommand implements ICommand {
|
export class PropogateSubspaceCommand implements ICommand {
|
||||||
constructor(public readonly param: UpdatedSubspaceModelInterface) {}
|
constructor(public readonly param: IModifySubspaceModelInterface) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class DeleteSubspaceModelDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Uuid of the subspace model need to be deleted',
|
||||||
|
example: '982fc3a3-64dc-4afb-a5b5-65ee8fef0424',
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
subspaceUuid: string;
|
||||||
|
}
|
||||||
1
src/space-model/dtos/subspaces-model-dtos/index.ts
Normal file
1
src/space-model/dtos/subspaces-model-dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './delete-subspace-model.dto';
|
||||||
@ -3,6 +3,7 @@ import { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';
|
|||||||
import { CreateSubspaceModelDto } from './create-subspace-model.dto';
|
import { CreateSubspaceModelDto } from './create-subspace-model.dto';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import { UpdateSubspaceModelDto } from './update-subspace-model.dto';
|
import { UpdateSubspaceModelDto } from './update-subspace-model.dto';
|
||||||
|
import { DeleteSubspaceModelDto } from './subspaces-model-dtos';
|
||||||
|
|
||||||
export class UpdateSubspacesModelDto {
|
export class UpdateSubspacesModelDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -26,6 +27,17 @@ export class UpdateSubspacesModelDto {
|
|||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => UpdateSubspaceModelDto)
|
@Type(() => UpdateSubspaceModelDto)
|
||||||
update?: UpdateSubspaceModelDto[];
|
update?: UpdateSubspaceModelDto[];
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of subspaces to delete',
|
||||||
|
type: [DeleteSubspaceModelDto],
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => DeleteSubspaceModelDto)
|
||||||
|
delete?: DeleteSubspaceModelDto[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateSpaceModelDto {
|
export class UpdateSpaceModelDto {
|
||||||
|
|||||||
@ -18,6 +18,10 @@ export class UpdateSubspaceModelDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
subspaceName?: string;
|
subspaceName?: string;
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
subspaceUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'List of products included in the model',
|
description: 'List of products included in the model',
|
||||||
type: [CreateSpaceProductModelDto],
|
type: [CreateSpaceProductModelDto],
|
||||||
|
|||||||
@ -14,6 +14,17 @@ export interface ProductModelInterface {
|
|||||||
productItemModels: SubspaceProductItemModelEntity[];
|
productItemModels: SubspaceProductItemModelEntity[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdatedSubspaceModelInterface {
|
export interface IModifySubspaceModelInterface {
|
||||||
new?: AddSubspaceModelInterface[];
|
new?: AddSubspaceModelInterface[];
|
||||||
|
update?: IUpdateSubspaceModelInterface[];
|
||||||
|
delete?: IDeletedSubsaceModelInterface[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IUpdateSubspaceModelInterface {
|
||||||
|
subspaceName?: string;
|
||||||
|
uuid: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IDeletedSubsaceModelInterface {
|
||||||
|
uuid: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
|
|||||||
import { SpaceModelParam } from '../dtos/space-model-param';
|
import { SpaceModelParam } from '../dtos/space-model-param';
|
||||||
import { ProjectService } from 'src/project/services';
|
import { ProjectService } from 'src/project/services';
|
||||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||||
import { UpdatedSubspaceModelInterface } from '../interfaces';
|
import { IModifySubspaceModelInterface } from '../interfaces';
|
||||||
import { CommandBus } from '@nestjs/cqrs';
|
import { CommandBus } from '@nestjs/cqrs';
|
||||||
import { PropogateSubspaceCommand } from '../commands';
|
import { PropogateSubspaceCommand } from '../commands';
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ export class SpaceModelService {
|
|||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
try {
|
try {
|
||||||
const { modelName } = dto;
|
const { modelName } = dto;
|
||||||
let updatedSubspaces: UpdatedSubspaceModelInterface;
|
let updatedSubspaces: IModifySubspaceModelInterface;
|
||||||
if (modelName) spaceModel.modelName = modelName;
|
if (modelName) spaceModel.modelName = modelName;
|
||||||
|
|
||||||
await queryRunner.manager.save(spaceModel);
|
await queryRunner.manager.save(spaceModel);
|
||||||
|
|||||||
@ -3,10 +3,19 @@ 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, UpdateSubspacesModelDto } from '../../dtos';
|
import {
|
||||||
|
CreateSubspaceModelDto,
|
||||||
|
UpdateSubspaceModelDto,
|
||||||
|
UpdateSubspacesModelDto,
|
||||||
|
} from '../../dtos';
|
||||||
import { QueryRunner } from 'typeorm';
|
import { QueryRunner } from 'typeorm';
|
||||||
import { SubspaceProductModelService } from './subspace-product-model.service';
|
import { SubspaceProductModelService } from './subspace-product-model.service';
|
||||||
import { UpdatedSubspaceModelInterface } from 'src/space-model/interfaces';
|
import {
|
||||||
|
IDeletedSubsaceModelInterface,
|
||||||
|
IModifySubspaceModelInterface,
|
||||||
|
IUpdateSubspaceModelInterface,
|
||||||
|
} from 'src/space-model/interfaces';
|
||||||
|
import { DeleteSubspaceModelDto } from 'src/space-model/dtos/subspaces-model-dtos';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SubSpaceModelService {
|
export class SubSpaceModelService {
|
||||||
@ -61,6 +70,79 @@ export class SubSpaceModelService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateSubspaceModels(
|
||||||
|
updateDtos: UpdateSubspaceModelDto[],
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
) {
|
||||||
|
const updateResults: IUpdateSubspaceModelInterface[] = [];
|
||||||
|
|
||||||
|
for (const dto of updateDtos) {
|
||||||
|
try {
|
||||||
|
const subspaceModel = await this.findOne(dto.subspaceUuid);
|
||||||
|
const updateResult: IUpdateSubspaceModelInterface = {
|
||||||
|
uuid: dto.subspaceUuid,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (dto.subspaceName) {
|
||||||
|
subspaceModel.subspaceName = dto.subspaceName;
|
||||||
|
await queryRunner.manager.update(
|
||||||
|
this.subspaceModelRepository.target,
|
||||||
|
{ uuid: dto.subspaceUuid },
|
||||||
|
{ subspaceName: dto.subspaceName },
|
||||||
|
);
|
||||||
|
updateResult.subspaceName = dto.subspaceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateResults.push(updateResult);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`SubspaceModel with ${dto.subspaceUuid} not able to update ${error}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateResults;
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteSubspaceModels(
|
||||||
|
dtos: DeleteSubspaceModelDto[],
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
) {
|
||||||
|
const deleteResults: IDeletedSubsaceModelInterface[] = [];
|
||||||
|
try {
|
||||||
|
for (const dto of dtos) {
|
||||||
|
await this.findOne(dto.subspaceUuid);
|
||||||
|
|
||||||
|
await queryRunner.manager.update(
|
||||||
|
this.subspaceModelRepository.target,
|
||||||
|
{ uuid: dto.subspaceUuid },
|
||||||
|
{ disabled: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
deleteResults.push({ uuid: dto.subspaceUuid });
|
||||||
|
}
|
||||||
|
return deleteResults;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Bulk delete operation failed: ${error.message}`);
|
||||||
|
throw new Error('Bulk delete operation failed.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOne(subspaceUuid: string) {
|
||||||
|
const subspace = await this.subspaceModelRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: subspaceUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!subspace) {
|
||||||
|
throw new HttpException(
|
||||||
|
`SubspaceModel with ${subspaceUuid} not found`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return subspace;
|
||||||
|
}
|
||||||
|
|
||||||
private validateInputDtos(subSpaceModelDtos: CreateSubspaceModelDto[]) {
|
private validateInputDtos(subSpaceModelDtos: CreateSubspaceModelDto[]) {
|
||||||
if (subSpaceModelDtos.length === 0) {
|
if (subSpaceModelDtos.length === 0) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -97,7 +179,7 @@ export class SubSpaceModelService {
|
|||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
const subspaces: UpdatedSubspaceModelInterface = {};
|
const subspaces: IModifySubspaceModelInterface = {};
|
||||||
try {
|
try {
|
||||||
if (dto.add) {
|
if (dto.add) {
|
||||||
const addedSubspaces = await this.createSubSpaceModels(
|
const addedSubspaces = await this.createSubSpaceModels(
|
||||||
@ -107,7 +189,11 @@ export class SubSpaceModelService {
|
|||||||
);
|
);
|
||||||
subspaces.new = addedSubspaces;
|
subspaces.new = addedSubspaces;
|
||||||
} else if (dto.update) {
|
} else if (dto.update) {
|
||||||
|
const updatedSubspaces = await this.updateSubspaceModels(
|
||||||
|
dto.update,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
subspaces.update = updatedSubspaces;
|
||||||
}
|
}
|
||||||
return subspaces;
|
return subspaces;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user