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) {
|
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);
|
let allocation = existingAllocations.get(tag.product.uuid);
|
||||||
if (!allocation) {
|
if (!allocation) {
|
||||||
@ -384,11 +390,16 @@ export class SpaceModelProductAllocationService {
|
|||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
tag: NewTagEntity,
|
tag: NewTagEntity,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
) {
|
): Promise<boolean> {
|
||||||
const existingAllocationsForProduct = await queryRunner.manager.find(
|
const existingAllocationsForProduct = await queryRunner.manager.find(
|
||||||
SpaceModelProductAllocationEntity,
|
SpaceModelProductAllocationEntity,
|
||||||
{
|
{
|
||||||
where: { spaceModel, product: tag.product },
|
where: {
|
||||||
|
spaceModel: {
|
||||||
|
uuid: spaceModel.uuid,
|
||||||
|
},
|
||||||
|
product: tag.product,
|
||||||
|
},
|
||||||
relations: ['tags'],
|
relations: ['tags'],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -404,11 +415,9 @@ export class SpaceModelProductAllocationService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (isDuplicateTag) {
|
if (isDuplicateTag) {
|
||||||
throw new HttpException(
|
return true;
|
||||||
`Tag ${tag.uuid} is already allocated to product ${tag.product.uuid} within this space (${spaceModel.uuid}).`,
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {
|
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {
|
||||||
|
|||||||
@ -255,7 +255,6 @@ export class SubspaceModelProductAllocationService {
|
|||||||
|
|
||||||
return deletedAllocations;
|
return deletedAllocations;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
|
||||||
throw this.handleError(error, `Failed to delete tags in subspace model`);
|
throw this.handleError(error, `Failed to delete tags in subspace model`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -309,68 +308,74 @@ export class SubspaceModelProductAllocationService {
|
|||||||
SubspaceModelProductAllocationEntity,
|
SubspaceModelProductAllocationEntity,
|
||||||
{
|
{
|
||||||
where: {
|
where: {
|
||||||
subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
|
tags: {
|
||||||
|
uuid: deletedTag.tagUuid,
|
||||||
|
},
|
||||||
|
subspaceModel: {
|
||||||
|
uuid: subspaceDto.subspaceModel.uuid,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
relations: ['tags', 'product', 'subspace'],
|
relations: ['tags', 'product', 'subspaceModel'],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
if (allocation) {
|
||||||
const isCommonTag = allocation.tags.some(
|
const isCommonTag = allocation.tags.some(
|
||||||
(tag) => tag.uuid === deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (allocation && isCommonTag) {
|
|
||||||
const tagEntity = allocation.tags.find(
|
|
||||||
(tag) => tag.uuid === deletedTag.tagUuid,
|
(tag) => tag.uuid === deletedTag.tagUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
allocation.tags = allocation.tags.filter(
|
if (allocation && isCommonTag) {
|
||||||
(tag) => tag.uuid !== deletedTag.tagUuid,
|
const tagEntity = allocation.tags.find(
|
||||||
);
|
(tag) => tag.uuid === deletedTag.tagUuid,
|
||||||
|
);
|
||||||
|
|
||||||
await queryRunner.manager.save(allocation);
|
allocation.tags = allocation.tags.filter(
|
||||||
|
(tag) => tag.uuid !== deletedTag.tagUuid,
|
||||||
|
);
|
||||||
|
|
||||||
const productAllocationExistInSubspace =
|
await queryRunner.manager.save(allocation);
|
||||||
await queryRunner.manager.findOne(
|
|
||||||
SubspaceModelProductAllocationEntity,
|
const productAllocationExistInSubspace =
|
||||||
{
|
await queryRunner.manager.findOne(
|
||||||
where: {
|
SubspaceModelProductAllocationEntity,
|
||||||
subspaceModel: {
|
{
|
||||||
uuid: subspaceDto.subspaceModel.uuid,
|
where: {
|
||||||
|
subspaceModel: {
|
||||||
|
uuid: subspaceDto.subspaceModel.uuid,
|
||||||
|
},
|
||||||
|
product: { uuid: allocation.product.uuid },
|
||||||
},
|
},
|
||||||
product: { uuid: allocation.product.uuid },
|
relations: ['tags'],
|
||||||
},
|
},
|
||||||
relations: ['tags'],
|
);
|
||||||
},
|
|
||||||
|
if (productAllocationExistInSubspace) {
|
||||||
|
productAllocationExistInSubspace.tags.push(tagEntity);
|
||||||
|
await queryRunner.manager.save(
|
||||||
|
productAllocationExistInSubspace,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const newProductAllocation = queryRunner.manager.create(
|
||||||
|
SubspaceModelProductAllocationEntity,
|
||||||
|
{
|
||||||
|
subspaceModel: subspaceModel.subspaceModel,
|
||||||
|
product: allocation.product,
|
||||||
|
tags: [tagEntity],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.manager.save(newProductAllocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the tag from processedTags to prevent duplication
|
||||||
|
processedTags = processedTags.filter(
|
||||||
|
(tag) => tag.uuid !== deletedTag.tagUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (productAllocationExistInSubspace) {
|
// Remove the tag from subspaceDto.tags to ensure it's not processed again while processing other dtos.
|
||||||
productAllocationExistInSubspace.tags.push(tagEntity);
|
subspaceDto.tags = subspaceDto.tags.filter(
|
||||||
await queryRunner.manager.save(
|
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
|
||||||
productAllocationExistInSubspace,
|
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
const newProductAllocation = queryRunner.manager.create(
|
|
||||||
SubspaceModelProductAllocationEntity,
|
|
||||||
{
|
|
||||||
subspaceModel: subspaceModel.subspaceModel,
|
|
||||||
product: allocation.product,
|
|
||||||
tags: [tagEntity],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await queryRunner.manager.save(newProductAllocation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the tag from processedTags to prevent duplication
|
|
||||||
processedTags = processedTags.filter(
|
|
||||||
(tag) => tag.uuid !== deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove the tag from subspaceDto.tags to ensure it's not processed again while processing other dtos.
|
|
||||||
subspaceDto.tags = subspaceDto.tags.filter(
|
|
||||||
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -103,38 +103,57 @@ export class SubSpaceModelService {
|
|||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||||
) {
|
) {
|
||||||
if (!dtos || dtos.length === 0) {
|
try {
|
||||||
return;
|
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 updatedModels = await this.updateSubspaceModel(
|
||||||
|
combinedDtos,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
const addedModels = await this.handleAddAction(
|
||||||
|
addDtos,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
const combineModels = [...addedModels, ...updatedModels];
|
||||||
|
|
||||||
|
await this.productAllocationService.updateAllocations(
|
||||||
|
combineModels,
|
||||||
|
projectUuid,
|
||||||
|
queryRunner,
|
||||||
|
spaceModel,
|
||||||
|
spaceTagUpdateDtos,
|
||||||
|
);
|
||||||
|
|
||||||
|
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,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 updatedModels = await this.updateSubspaceModel(
|
|
||||||
combinedDtos,
|
|
||||||
spaceModel,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
const addedModels = await this.handleAddAction(
|
|
||||||
addDtos,
|
|
||||||
spaceModel,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
const combineModels = [...addedModels, ...updatedModels];
|
|
||||||
await this.productAllocationService.updateAllocations(
|
|
||||||
combineModels,
|
|
||||||
projectUuid,
|
|
||||||
queryRunner,
|
|
||||||
spaceModel,
|
|
||||||
spaceTagUpdateDtos,
|
|
||||||
);
|
|
||||||
await this.deleteSubspaceModels(deleteDtos, queryRunner);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteSubspaceModels(
|
async deleteSubspaceModels(
|
||||||
deleteDtos: ModifySubspaceModelDto[],
|
deleteDtos: ModifySubspaceModelDto[],
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
|
spaceModel: SpaceModelEntity,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
if (!deleteDtos || deleteDtos.length === 0) {
|
if (!deleteDtos || deleteDtos.length === 0) {
|
||||||
@ -164,6 +183,7 @@ export class SubSpaceModelService {
|
|||||||
const allocationsToRemove = subspaces.flatMap(
|
const allocationsToRemove = subspaces.flatMap(
|
||||||
(subspace) => subspace.productAllocations,
|
(subspace) => subspace.productAllocations,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (allocationsToRemove.length > 0) {
|
if (allocationsToRemove.length > 0) {
|
||||||
const spaceAllocationsMap = new Map<
|
const spaceAllocationsMap = new Map<
|
||||||
string,
|
string,
|
||||||
@ -173,7 +193,6 @@ export class SubSpaceModelService {
|
|||||||
for (const allocation of allocationsToRemove) {
|
for (const allocation of allocationsToRemove) {
|
||||||
const product = allocation.product;
|
const product = allocation.product;
|
||||||
const tags = allocation.tags;
|
const tags = allocation.tags;
|
||||||
const spaceModel = allocation.subspaceModel.spaceModel;
|
|
||||||
|
|
||||||
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
|
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
|
||||||
|
|
||||||
@ -230,12 +249,13 @@ export class SubSpaceModelService {
|
|||||||
.delete()
|
.delete()
|
||||||
.from('subspace_model_product_tags')
|
.from('subspace_model_product_tags')
|
||||||
.where(
|
.where(
|
||||||
'subspace_model_product_allocation_id NOT IN ' +
|
'subspace_model_product_allocation_uuid NOT IN (' +
|
||||||
queryRunner.manager
|
queryRunner.manager
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.select('uuid')
|
.select('allocation.uuid')
|
||||||
.from(SubspaceModelProductAllocationEntity, 'allocation')
|
.from(SubspaceModelProductAllocationEntity, 'allocation')
|
||||||
.getQuery(),
|
.getQuery() +
|
||||||
|
')',
|
||||||
)
|
)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
@ -418,6 +438,7 @@ export class SubSpaceModelService {
|
|||||||
where: { uuid: In(subspaceUuids) },
|
where: { uuid: In(subspaceUuids) },
|
||||||
relations: [
|
relations: [
|
||||||
'productAllocations',
|
'productAllocations',
|
||||||
|
'productAllocations.product',
|
||||||
'productAllocations.tags',
|
'productAllocations.tags',
|
||||||
'spaceModel',
|
'spaceModel',
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user