mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Merge branch 'dev'
This commit is contained in:
@ -13,8 +13,7 @@ import {
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
TagModel,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { DataSource, In, QueryRunner } from 'typeorm';
|
||||
import { SubSpaceService } from 'src/space/services';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { TagService } from 'src/space/services/tag';
|
||||
import {
|
||||
ISingleSubspaceModel,
|
||||
@ -30,8 +29,6 @@ export class PropogateUpdateSpaceModelHandler
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly subspaceRepository: SubspaceRepository,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly subSpaceService: SubSpaceService,
|
||||
private readonly tagService: TagService,
|
||||
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
|
||||
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
|
||||
@ -40,7 +37,7 @@ export class PropogateUpdateSpaceModelHandler
|
||||
|
||||
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
||||
const { subspaceModels, spaces } = command.param;
|
||||
|
||||
try {
|
||||
if (!subspaceModels || subspaceModels.length === 0) return;
|
||||
if (!spaces || spaces.length === 0) return;
|
||||
|
||||
@ -49,8 +46,13 @@ export class PropogateUpdateSpaceModelHandler
|
||||
await this.addSubspaceModel(subspaceModel, spaces);
|
||||
} else if (subspaceModel.action === ModifyAction.DELETE) {
|
||||
await this.deleteSubspaceModel(subspaceModel, spaces);
|
||||
} else if (subspaceModel.action === ModifyAction.UPDATE) {
|
||||
await this.updateSubspaceModel(subspaceModel);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error processing subspaceModel updates`, error);
|
||||
}
|
||||
}
|
||||
|
||||
async addSubspaceModel(
|
||||
@ -106,7 +108,9 @@ export class PropogateUpdateSpaceModelHandler
|
||||
],
|
||||
});
|
||||
|
||||
if (!subspaces.length) return;
|
||||
if (!subspaces.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
|
||||
subspace.productAllocations.map((allocation) => allocation.uuid),
|
||||
@ -122,7 +126,10 @@ export class PropogateUpdateSpaceModelHandler
|
||||
);
|
||||
|
||||
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
|
||||
if (!relocatedAllocations.length) return;
|
||||
|
||||
if (!relocatedAllocations.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const space of spaces) {
|
||||
for (const { allocation, tags = [] } of relocatedAllocations) {
|
||||
@ -152,6 +159,15 @@ export class PropogateUpdateSpaceModelHandler
|
||||
}
|
||||
}
|
||||
|
||||
async updateSubspaceModel(subspaceModel: ISingleSubspaceModel) {
|
||||
return this.subspaceRepository.update(
|
||||
{
|
||||
subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
|
||||
disabled: false,
|
||||
},
|
||||
{ subspaceName: subspaceModel.subspaceModel.subspaceName },
|
||||
);
|
||||
}
|
||||
async updateSubspaceModels(
|
||||
subspaceModels: UpdatedSubspaceModelPayload[],
|
||||
queryRunner: QueryRunner,
|
||||
|
@ -36,6 +36,13 @@ export class PropogateDeleteSpaceModelHandler
|
||||
|
||||
for (const space of spaces) {
|
||||
try {
|
||||
await queryRunner.manager.update(
|
||||
this.spaceRepository.target,
|
||||
{ uuid: space.uuid },
|
||||
{
|
||||
spaceModel: null,
|
||||
},
|
||||
);
|
||||
await this.spaceService.unlinkSpaceFromModel(space, queryRunner);
|
||||
} catch (innerError) {
|
||||
this.logger.error(
|
||||
|
@ -440,11 +440,12 @@ export class SpaceModelService {
|
||||
queryRunner?: QueryRunner, // Make queryRunner optional
|
||||
): Promise<void> {
|
||||
try {
|
||||
space.spaceModel = spaceModel;
|
||||
if (queryRunner) {
|
||||
await queryRunner.manager.save(SpaceEntity, space);
|
||||
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
||||
spaceModel,
|
||||
});
|
||||
} else {
|
||||
await this.spaceRepository.save(space);
|
||||
await this.spaceRepository.update(space.uuid, { spaceModel });
|
||||
}
|
||||
const spaceProductAllocations = spaceModel.productAllocations.map(
|
||||
(modelAllocation) =>
|
||||
|
@ -281,7 +281,7 @@ export class SubspaceModelProductAllocationService {
|
||||
queryRunner: QueryRunner,
|
||||
spaceModel: SpaceModelEntity,
|
||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||
) {
|
||||
): Promise<IUpdatedAllocations[]> {
|
||||
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
|
||||
const updatedAllocations: IUpdatedAllocations[] = [];
|
||||
|
||||
|
@ -145,6 +145,7 @@ export class SubSpaceModelService {
|
||||
);
|
||||
const combineModels = [...createdSubspaces, ...updatedSubspaces];
|
||||
|
||||
const updatedAllocations =
|
||||
await this.productAllocationService.updateAllocations(
|
||||
combineModels,
|
||||
projectUuid,
|
||||
|
@ -45,14 +45,15 @@ export class ValidationService {
|
||||
await this.communityService.validateProject(projectUuid);
|
||||
const spaces = await this.spaceRepository.find({
|
||||
where: { uuid: In(spacesUuids), disabled: false },
|
||||
relations: ['devices', 'subspaces', 'productAllocations'],
|
||||
relations: ['devices', 'subspaces', 'productAllocations', 'spaceModel'],
|
||||
});
|
||||
|
||||
const hasInvalidSpaces = spaces.some(
|
||||
(space) =>
|
||||
space.devices.length > 0 ||
|
||||
space.subspaces.length > 0 ||
|
||||
space.productAllocations.length > 0,
|
||||
space.productAllocations.length > 0 ||
|
||||
space.spaceModel,
|
||||
);
|
||||
|
||||
if (hasInvalidSpaces) {
|
||||
@ -176,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,6 +327,7 @@ export class SubSpaceService {
|
||||
if (!subspaceDtos || subspaceDtos.length === 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const addedSubspaces = [];
|
||||
const updatedSubspaces = [];
|
||||
|
||||
@ -374,6 +375,12 @@ export class SubSpaceService {
|
||||
spaceTagUpdateDtos,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
`An error occurred while modifying subspaces: ${error.message}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async unlinkModels(
|
||||
|
Reference in New Issue
Block a user