mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:24:55 +00:00
fix issue on deleting subspace
This commit is contained in:
@ -88,7 +88,13 @@ export class SpaceModelProductAllocationService {
|
||||
}
|
||||
|
||||
if (isTagNeeded) {
|
||||
await this.validateTagWithinSpaceModel(queryRunner, tag, spaceModel);
|
||||
const hasTags = await this.validateTagWithinSpaceModel(
|
||||
queryRunner,
|
||||
tag,
|
||||
spaceModel,
|
||||
);
|
||||
|
||||
if (hasTags) continue;
|
||||
|
||||
let allocation = existingAllocations.get(tag.product.uuid);
|
||||
if (!allocation) {
|
||||
@ -384,11 +390,16 @@ export class SpaceModelProductAllocationService {
|
||||
queryRunner: QueryRunner,
|
||||
tag: NewTagEntity,
|
||||
spaceModel: SpaceModelEntity,
|
||||
) {
|
||||
): Promise<boolean> {
|
||||
const existingAllocationsForProduct = await queryRunner.manager.find(
|
||||
SpaceModelProductAllocationEntity,
|
||||
{
|
||||
where: { spaceModel, product: tag.product },
|
||||
where: {
|
||||
spaceModel: {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
product: tag.product,
|
||||
},
|
||||
relations: ['tags'],
|
||||
},
|
||||
);
|
||||
@ -404,11 +415,9 @@ export class SpaceModelProductAllocationService {
|
||||
);
|
||||
|
||||
if (isDuplicateTag) {
|
||||
throw new HttpException(
|
||||
`Tag ${tag.uuid} is already allocated to product ${tag.product.uuid} within this space (${spaceModel.uuid}).`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {
|
||||
|
||||
@ -255,7 +255,6 @@ export class SubspaceModelProductAllocationService {
|
||||
|
||||
return deletedAllocations;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw this.handleError(error, `Failed to delete tags in subspace model`);
|
||||
}
|
||||
}
|
||||
@ -309,12 +308,17 @@ export class SubspaceModelProductAllocationService {
|
||||
SubspaceModelProductAllocationEntity,
|
||||
{
|
||||
where: {
|
||||
subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
|
||||
tags: {
|
||||
uuid: deletedTag.tagUuid,
|
||||
},
|
||||
relations: ['tags', 'product', 'subspace'],
|
||||
subspaceModel: {
|
||||
uuid: subspaceDto.subspaceModel.uuid,
|
||||
},
|
||||
},
|
||||
relations: ['tags', 'product', 'subspaceModel'],
|
||||
},
|
||||
);
|
||||
|
||||
if (allocation) {
|
||||
const isCommonTag = allocation.tags.some(
|
||||
(tag) => tag.uuid === deletedTag.tagUuid,
|
||||
);
|
||||
@ -374,6 +378,7 @@ export class SubspaceModelProductAllocationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (
|
||||
subspaceDto !== subspaceModel &&
|
||||
subspaceDto.action === ModifyAction.DELETE
|
||||
|
||||
@ -103,13 +103,18 @@ export class SubSpaceModelService {
|
||||
projectUuid: string,
|
||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||
) {
|
||||
try {
|
||||
if (!dtos || dtos.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
|
||||
const combinedDtos = dtos.filter((dto) => dto.action !== ModifyAction.ADD);
|
||||
const deleteDtos = dtos.filter((dto) => dto.action === ModifyAction.DELETE);
|
||||
const combinedDtos = dtos.filter(
|
||||
(dto) => dto.action !== ModifyAction.ADD,
|
||||
);
|
||||
const deleteDtos = dtos.filter(
|
||||
(dto) => dto.action === ModifyAction.DELETE,
|
||||
);
|
||||
|
||||
const updatedModels = await this.updateSubspaceModel(
|
||||
combinedDtos,
|
||||
@ -122,6 +127,7 @@ export class SubSpaceModelService {
|
||||
queryRunner,
|
||||
);
|
||||
const combineModels = [...addedModels, ...updatedModels];
|
||||
|
||||
await this.productAllocationService.updateAllocations(
|
||||
combineModels,
|
||||
projectUuid,
|
||||
@ -129,12 +135,25 @@ export class SubSpaceModelService {
|
||||
spaceModel,
|
||||
spaceTagUpdateDtos,
|
||||
);
|
||||
await this.deleteSubspaceModels(deleteDtos, queryRunner);
|
||||
|
||||
await this.deleteSubspaceModels(deleteDtos, queryRunner, spaceModel);
|
||||
} catch (error) {
|
||||
console.error('Error in modifySubspaceModels:', error);
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
message: 'An error occurred while modifying subspace models',
|
||||
error: error.message,
|
||||
},
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteSubspaceModels(
|
||||
deleteDtos: ModifySubspaceModelDto[],
|
||||
queryRunner: QueryRunner,
|
||||
spaceModel: SpaceModelEntity,
|
||||
) {
|
||||
try {
|
||||
if (!deleteDtos || deleteDtos.length === 0) {
|
||||
@ -164,6 +183,7 @@ export class SubSpaceModelService {
|
||||
const allocationsToRemove = subspaces.flatMap(
|
||||
(subspace) => subspace.productAllocations,
|
||||
);
|
||||
|
||||
if (allocationsToRemove.length > 0) {
|
||||
const spaceAllocationsMap = new Map<
|
||||
string,
|
||||
@ -173,7 +193,6 @@ export class SubSpaceModelService {
|
||||
for (const allocation of allocationsToRemove) {
|
||||
const product = allocation.product;
|
||||
const tags = allocation.tags;
|
||||
const spaceModel = allocation.subspaceModel.spaceModel;
|
||||
|
||||
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
|
||||
|
||||
@ -230,12 +249,13 @@ export class SubSpaceModelService {
|
||||
.delete()
|
||||
.from('subspace_model_product_tags')
|
||||
.where(
|
||||
'subspace_model_product_allocation_id NOT IN ' +
|
||||
'subspace_model_product_allocation_uuid NOT IN (' +
|
||||
queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.select('uuid')
|
||||
.select('allocation.uuid')
|
||||
.from(SubspaceModelProductAllocationEntity, 'allocation')
|
||||
.getQuery(),
|
||||
.getQuery() +
|
||||
')',
|
||||
)
|
||||
.execute();
|
||||
|
||||
@ -418,6 +438,7 @@ export class SubSpaceModelService {
|
||||
where: { uuid: In(subspaceUuids) },
|
||||
relations: [
|
||||
'productAllocations',
|
||||
'productAllocations.product',
|
||||
'productAllocations.tags',
|
||||
'spaceModel',
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user