mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 20:14:54 +00:00
added subspace delete
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import { ICommand } from '@nestjs/cqrs';
|
||||
import { UpdatedSubspaceModelInterface } from '../interfaces';
|
||||
import { IModifySubspaceModelInterface } from '../interfaces';
|
||||
|
||||
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 { Type } from 'class-transformer';
|
||||
import { UpdateSubspaceModelDto } from './update-subspace-model.dto';
|
||||
import { DeleteSubspaceModelDto } from './subspaces-model-dtos';
|
||||
|
||||
export class UpdateSubspacesModelDto {
|
||||
@ApiProperty({
|
||||
@ -26,6 +27,17 @@ export class UpdateSubspacesModelDto {
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => 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 {
|
||||
|
||||
@ -18,6 +18,10 @@ export class UpdateSubspaceModelDto {
|
||||
@IsString()
|
||||
subspaceName?: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
subspaceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'List of products included in the model',
|
||||
type: [CreateSpaceProductModelDto],
|
||||
|
||||
@ -14,6 +14,17 @@ export interface ProductModelInterface {
|
||||
productItemModels: SubspaceProductItemModelEntity[];
|
||||
}
|
||||
|
||||
export interface UpdatedSubspaceModelInterface {
|
||||
export interface IModifySubspaceModelInterface {
|
||||
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 { ProjectService } from 'src/project/services';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { UpdatedSubspaceModelInterface } from '../interfaces';
|
||||
import { IModifySubspaceModelInterface } from '../interfaces';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { PropogateSubspaceCommand } from '../commands';
|
||||
|
||||
@ -146,7 +146,7 @@ export class SpaceModelService {
|
||||
await queryRunner.startTransaction();
|
||||
try {
|
||||
const { modelName } = dto;
|
||||
let updatedSubspaces: UpdatedSubspaceModelInterface;
|
||||
let updatedSubspaces: IModifySubspaceModelInterface;
|
||||
if (modelName) spaceModel.modelName = modelName;
|
||||
|
||||
await queryRunner.manager.save(spaceModel);
|
||||
|
||||
@ -3,10 +3,19 @@ import {
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSubspaceModelDto, UpdateSubspacesModelDto } from '../../dtos';
|
||||
import {
|
||||
CreateSubspaceModelDto,
|
||||
UpdateSubspaceModelDto,
|
||||
UpdateSubspacesModelDto,
|
||||
} from '../../dtos';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
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()
|
||||
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[]) {
|
||||
if (subSpaceModelDtos.length === 0) {
|
||||
throw new HttpException(
|
||||
@ -97,7 +179,7 @@ export class SubSpaceModelService {
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
const subspaces: UpdatedSubspaceModelInterface = {};
|
||||
const subspaces: IModifySubspaceModelInterface = {};
|
||||
try {
|
||||
if (dto.add) {
|
||||
const addedSubspaces = await this.createSubSpaceModels(
|
||||
@ -107,7 +189,11 @@ export class SubSpaceModelService {
|
||||
);
|
||||
subspaces.new = addedSubspaces;
|
||||
} else if (dto.update) {
|
||||
|
||||
const updatedSubspaces = await this.updateSubspaceModels(
|
||||
dto.update,
|
||||
queryRunner,
|
||||
);
|
||||
subspaces.update = updatedSubspaces;
|
||||
}
|
||||
return subspaces;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user