mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 00:34:54 +00:00
fix issues
This commit is contained in:
@ -177,17 +177,36 @@ export class ValidationService {
|
||||
}
|
||||
|
||||
async validateSpaceModel(spaceModelUuid: string): Promise<SpaceModelEntity> {
|
||||
const spaceModel = await this.spaceModelRepository.findOne({
|
||||
where: { uuid: spaceModelUuid },
|
||||
relations: [
|
||||
const queryBuilder = this.spaceModelRepository
|
||||
.createQueryBuilder('spaceModel')
|
||||
.leftJoinAndSelect(
|
||||
'spaceModel.subspaceModels',
|
||||
'subspaceModels',
|
||||
'subspaceModels.disabled = :disabled',
|
||||
{ disabled: false },
|
||||
)
|
||||
.leftJoinAndSelect(
|
||||
'subspaceModels.productAllocations',
|
||||
'subspaceModels.productAllocations.tags',
|
||||
'subspaceModels.productAllocations.product',
|
||||
'subspaceProductAllocations',
|
||||
)
|
||||
.leftJoinAndSelect(
|
||||
'subspaceProductAllocations.tags',
|
||||
'subspaceAllocationTags',
|
||||
)
|
||||
.leftJoinAndSelect(
|
||||
'subspaceProductAllocations.product',
|
||||
'subspaceAllocationProduct',
|
||||
)
|
||||
.leftJoinAndSelect('spaceModel.productAllocations', 'productAllocations')
|
||||
.leftJoinAndSelect(
|
||||
'productAllocations.product',
|
||||
'productAllocations.tags',
|
||||
],
|
||||
});
|
||||
'productAllocationProduct',
|
||||
)
|
||||
.leftJoinAndSelect('productAllocations.tags', 'productAllocationTags')
|
||||
.andWhere('spaceModel.disabled = :disabled', { disabled: false })
|
||||
.where('spaceModel.uuid = :uuid', { uuid: spaceModelUuid });
|
||||
|
||||
const spaceModel = await queryBuilder.getOne();
|
||||
|
||||
if (!spaceModel) {
|
||||
throw new HttpException(
|
||||
|
||||
@ -427,6 +427,8 @@ export class SpaceService {
|
||||
const { communityUuid, spaceUuid, projectUuid } = params;
|
||||
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
||||
const hasTags = updateSpaceDto.tags?.length > 0;
|
||||
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
@ -450,11 +452,19 @@ export class SpaceService {
|
||||
}
|
||||
|
||||
if (space.spaceModel && !updateSpaceDto.spaceModelUuid) {
|
||||
space.spaceModel = null;
|
||||
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
||||
spaceModel: null,
|
||||
});
|
||||
await this.unlinkSpaceFromModel(space, queryRunner);
|
||||
}
|
||||
|
||||
this.updateSpaceProperties(space, updateSpaceDto);
|
||||
await this.updateSpaceProperties(space, updateSpaceDto, queryRunner);
|
||||
|
||||
if (hasSubspace || hasTags) {
|
||||
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
||||
spaceModel: null,
|
||||
});
|
||||
}
|
||||
|
||||
if (updateSpaceDto.spaceModelUuid) {
|
||||
const spaceModel = await this.validationService.validateSpaceModel(
|
||||
@ -478,6 +488,7 @@ export class SpaceService {
|
||||
project,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
await this.spaceModelService.linkToSpace(
|
||||
space,
|
||||
spaceModel,
|
||||
@ -485,14 +496,9 @@ export class SpaceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
||||
|
||||
if (hasSubspace) {
|
||||
space.spaceModel = null;
|
||||
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
|
||||
}
|
||||
await queryRunner.manager.save(space);
|
||||
|
||||
if (hasSubspace) {
|
||||
await this.subSpaceService.modifySubSpace(
|
||||
@ -513,6 +519,7 @@ export class SpaceService {
|
||||
updateSpaceDto.subspace,
|
||||
);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
@ -560,16 +567,23 @@ export class SpaceService {
|
||||
}
|
||||
}
|
||||
|
||||
private updateSpaceProperties(
|
||||
private async updateSpaceProperties(
|
||||
space: SpaceEntity,
|
||||
updateSpaceDto: UpdateSpaceDto,
|
||||
): void {
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const { spaceName, x, y, icon } = updateSpaceDto;
|
||||
|
||||
if (spaceName) space.spaceName = spaceName;
|
||||
if (x) space.x = x;
|
||||
if (y) space.y = y;
|
||||
if (icon) space.icon = icon;
|
||||
const updateFields: Partial<SpaceEntity> = {};
|
||||
|
||||
if (spaceName) updateFields.spaceName = spaceName;
|
||||
if (x !== undefined) updateFields.x = x;
|
||||
if (y !== undefined) updateFields.y = y;
|
||||
if (icon) updateFields.icon = icon;
|
||||
|
||||
if (Object.keys(updateFields).length > 0) {
|
||||
await queryRunner.manager.update(SpaceEntity, space.uuid, updateFields);
|
||||
}
|
||||
}
|
||||
|
||||
async getSpacesHierarchyForSpace(
|
||||
|
||||
@ -327,51 +327,58 @@ export class SubSpaceService {
|
||||
if (!subspaceDtos || subspaceDtos.length === 0) {
|
||||
return;
|
||||
}
|
||||
const addedSubspaces = [];
|
||||
const updatedSubspaces = [];
|
||||
try {
|
||||
const addedSubspaces = [];
|
||||
const updatedSubspaces = [];
|
||||
|
||||
for (const subspace of subspaceDtos) {
|
||||
switch (subspace.action) {
|
||||
case ModifyAction.ADD:
|
||||
const addedSubspace = await this.handleAddAction(
|
||||
subspace,
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
if (addedSubspace) addedSubspaces.push(addedSubspace);
|
||||
for (const subspace of subspaceDtos) {
|
||||
switch (subspace.action) {
|
||||
case ModifyAction.ADD:
|
||||
const addedSubspace = await this.handleAddAction(
|
||||
subspace,
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
if (addedSubspace) addedSubspaces.push(addedSubspace);
|
||||
|
||||
break;
|
||||
case ModifyAction.UPDATE:
|
||||
const updatedSubspace = await this.handleUpdateAction(
|
||||
subspace,
|
||||
queryRunner,
|
||||
);
|
||||
if (updatedSubspace) {
|
||||
updatedSubspaces.push(updatedSubspace);
|
||||
}
|
||||
break;
|
||||
case ModifyAction.DELETE:
|
||||
await this.handleDeleteAction(subspace, queryRunner);
|
||||
break;
|
||||
default:
|
||||
throw new HttpException(
|
||||
`Invalid action "${subspace.action}".`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
break;
|
||||
case ModifyAction.UPDATE:
|
||||
const updatedSubspace = await this.handleUpdateAction(
|
||||
subspace,
|
||||
queryRunner,
|
||||
);
|
||||
if (updatedSubspace) {
|
||||
updatedSubspaces.push(updatedSubspace);
|
||||
}
|
||||
break;
|
||||
case ModifyAction.DELETE:
|
||||
await this.handleDeleteAction(subspace, queryRunner);
|
||||
break;
|
||||
default:
|
||||
throw new HttpException(
|
||||
`Invalid action "${subspace.action}".`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
|
||||
(subspace) => subspace !== undefined,
|
||||
);
|
||||
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
|
||||
(subspace) => subspace !== undefined,
|
||||
);
|
||||
|
||||
if (combinedSubspaces.length > 0) {
|
||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
|
||||
combinedSubspaces,
|
||||
projectUuid,
|
||||
queryRunner,
|
||||
space,
|
||||
spaceTagUpdateDtos,
|
||||
if (combinedSubspaces.length > 0) {
|
||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
|
||||
combinedSubspaces,
|
||||
projectUuid,
|
||||
queryRunner,
|
||||
space,
|
||||
spaceTagUpdateDtos,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
`An error occurred while modifying subspaces: ${error.message}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user