mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 23:27:31 +00:00
Merge branch 'dev'
This commit is contained in:
@ -13,8 +13,7 @@ import {
|
|||||||
SubspaceModelProductAllocationRepoitory,
|
SubspaceModelProductAllocationRepoitory,
|
||||||
TagModel,
|
TagModel,
|
||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import { DataSource, In, QueryRunner } from 'typeorm';
|
import { In, QueryRunner } from 'typeorm';
|
||||||
import { SubSpaceService } from 'src/space/services';
|
|
||||||
import { TagService } from 'src/space/services/tag';
|
import { TagService } from 'src/space/services/tag';
|
||||||
import {
|
import {
|
||||||
ISingleSubspaceModel,
|
ISingleSubspaceModel,
|
||||||
@ -30,8 +29,6 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly subspaceRepository: SubspaceRepository,
|
private readonly subspaceRepository: SubspaceRepository,
|
||||||
private readonly dataSource: DataSource,
|
|
||||||
private readonly subSpaceService: SubSpaceService,
|
|
||||||
private readonly tagService: TagService,
|
private readonly tagService: TagService,
|
||||||
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
|
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
|
||||||
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
|
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
|
||||||
@ -40,16 +37,21 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
|
|
||||||
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
||||||
const { subspaceModels, spaces } = command.param;
|
const { subspaceModels, spaces } = command.param;
|
||||||
|
try {
|
||||||
|
if (!subspaceModels || subspaceModels.length === 0) return;
|
||||||
|
if (!spaces || spaces.length === 0) return;
|
||||||
|
|
||||||
if (!subspaceModels || subspaceModels.length === 0) return;
|
for (const subspaceModel of subspaceModels) {
|
||||||
if (!spaces || spaces.length === 0) return;
|
if (subspaceModel.action === ModifyAction.ADD) {
|
||||||
|
await this.addSubspaceModel(subspaceModel, spaces);
|
||||||
for (const subspaceModel of subspaceModels) {
|
} else if (subspaceModel.action === ModifyAction.DELETE) {
|
||||||
if (subspaceModel.action === ModifyAction.ADD) {
|
await this.deleteSubspaceModel(subspaceModel, spaces);
|
||||||
await this.addSubspaceModel(subspaceModel, spaces);
|
} else if (subspaceModel.action === ModifyAction.UPDATE) {
|
||||||
} else if (subspaceModel.action === ModifyAction.DELETE) {
|
await this.updateSubspaceModel(subspaceModel);
|
||||||
await this.deleteSubspaceModel(subspaceModel, spaces);
|
}
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error processing subspaceModel updates`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +108,9 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!subspaces.length) return;
|
if (!subspaces.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
|
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
|
||||||
subspace.productAllocations.map((allocation) => allocation.uuid),
|
subspace.productAllocations.map((allocation) => allocation.uuid),
|
||||||
@ -122,7 +126,10 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
);
|
);
|
||||||
|
|
||||||
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
|
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
|
||||||
if (!relocatedAllocations.length) return;
|
|
||||||
|
if (!relocatedAllocations.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (const space of spaces) {
|
for (const space of spaces) {
|
||||||
for (const { allocation, tags = [] } of relocatedAllocations) {
|
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(
|
async updateSubspaceModels(
|
||||||
subspaceModels: UpdatedSubspaceModelPayload[],
|
subspaceModels: UpdatedSubspaceModelPayload[],
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
|
@ -36,6 +36,13 @@ export class PropogateDeleteSpaceModelHandler
|
|||||||
|
|
||||||
for (const space of spaces) {
|
for (const space of spaces) {
|
||||||
try {
|
try {
|
||||||
|
await queryRunner.manager.update(
|
||||||
|
this.spaceRepository.target,
|
||||||
|
{ uuid: space.uuid },
|
||||||
|
{
|
||||||
|
spaceModel: null,
|
||||||
|
},
|
||||||
|
);
|
||||||
await this.spaceService.unlinkSpaceFromModel(space, queryRunner);
|
await this.spaceService.unlinkSpaceFromModel(space, queryRunner);
|
||||||
} catch (innerError) {
|
} catch (innerError) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
|
@ -440,11 +440,12 @@ export class SpaceModelService {
|
|||||||
queryRunner?: QueryRunner, // Make queryRunner optional
|
queryRunner?: QueryRunner, // Make queryRunner optional
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
space.spaceModel = spaceModel;
|
|
||||||
if (queryRunner) {
|
if (queryRunner) {
|
||||||
await queryRunner.manager.save(SpaceEntity, space);
|
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
||||||
|
spaceModel,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
await this.spaceRepository.save(space);
|
await this.spaceRepository.update(space.uuid, { spaceModel });
|
||||||
}
|
}
|
||||||
const spaceProductAllocations = spaceModel.productAllocations.map(
|
const spaceProductAllocations = spaceModel.productAllocations.map(
|
||||||
(modelAllocation) =>
|
(modelAllocation) =>
|
||||||
|
@ -281,7 +281,7 @@ export class SubspaceModelProductAllocationService {
|
|||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||||
) {
|
): Promise<IUpdatedAllocations[]> {
|
||||||
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
|
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
|
||||||
const updatedAllocations: IUpdatedAllocations[] = [];
|
const updatedAllocations: IUpdatedAllocations[] = [];
|
||||||
|
|
||||||
|
@ -145,13 +145,14 @@ export class SubSpaceModelService {
|
|||||||
);
|
);
|
||||||
const combineModels = [...createdSubspaces, ...updatedSubspaces];
|
const combineModels = [...createdSubspaces, ...updatedSubspaces];
|
||||||
|
|
||||||
await this.productAllocationService.updateAllocations(
|
const updatedAllocations =
|
||||||
combineModels,
|
await this.productAllocationService.updateAllocations(
|
||||||
projectUuid,
|
combineModels,
|
||||||
queryRunner,
|
projectUuid,
|
||||||
spaceModel,
|
queryRunner,
|
||||||
spaceTagUpdateDtos,
|
spaceModel,
|
||||||
);
|
spaceTagUpdateDtos,
|
||||||
|
);
|
||||||
|
|
||||||
const deletedSubspaces = await this.deleteSubspaceModels(
|
const deletedSubspaces = await this.deleteSubspaceModels(
|
||||||
deleteDtos,
|
deleteDtos,
|
||||||
|
@ -45,14 +45,15 @@ export class ValidationService {
|
|||||||
await this.communityService.validateProject(projectUuid);
|
await this.communityService.validateProject(projectUuid);
|
||||||
const spaces = await this.spaceRepository.find({
|
const spaces = await this.spaceRepository.find({
|
||||||
where: { uuid: In(spacesUuids), disabled: false },
|
where: { uuid: In(spacesUuids), disabled: false },
|
||||||
relations: ['devices', 'subspaces', 'productAllocations'],
|
relations: ['devices', 'subspaces', 'productAllocations', 'spaceModel'],
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasInvalidSpaces = spaces.some(
|
const hasInvalidSpaces = spaces.some(
|
||||||
(space) =>
|
(space) =>
|
||||||
space.devices.length > 0 ||
|
space.devices.length > 0 ||
|
||||||
space.subspaces.length > 0 ||
|
space.subspaces.length > 0 ||
|
||||||
space.productAllocations.length > 0,
|
space.productAllocations.length > 0 ||
|
||||||
|
space.spaceModel,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (hasInvalidSpaces) {
|
if (hasInvalidSpaces) {
|
||||||
@ -176,17 +177,36 @@ export class ValidationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async validateSpaceModel(spaceModelUuid: string): Promise<SpaceModelEntity> {
|
async validateSpaceModel(spaceModelUuid: string): Promise<SpaceModelEntity> {
|
||||||
const spaceModel = await this.spaceModelRepository.findOne({
|
const queryBuilder = this.spaceModelRepository
|
||||||
where: { uuid: spaceModelUuid },
|
.createQueryBuilder('spaceModel')
|
||||||
relations: [
|
.leftJoinAndSelect(
|
||||||
|
'spaceModel.subspaceModels',
|
||||||
'subspaceModels',
|
'subspaceModels',
|
||||||
|
'subspaceModels.disabled = :disabled',
|
||||||
|
{ disabled: false },
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect(
|
||||||
'subspaceModels.productAllocations',
|
'subspaceModels.productAllocations',
|
||||||
'subspaceModels.productAllocations.tags',
|
'subspaceProductAllocations',
|
||||||
'subspaceModels.productAllocations.product',
|
)
|
||||||
|
.leftJoinAndSelect(
|
||||||
|
'subspaceProductAllocations.tags',
|
||||||
|
'subspaceAllocationTags',
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect(
|
||||||
|
'subspaceProductAllocations.product',
|
||||||
|
'subspaceAllocationProduct',
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect('spaceModel.productAllocations', 'productAllocations')
|
||||||
|
.leftJoinAndSelect(
|
||||||
'productAllocations.product',
|
'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) {
|
if (!spaceModel) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
@ -427,6 +427,8 @@ export class SpaceService {
|
|||||||
const { communityUuid, spaceUuid, projectUuid } = params;
|
const { communityUuid, spaceUuid, projectUuid } = params;
|
||||||
|
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
||||||
|
const hasTags = updateSpaceDto.tags?.length > 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
@ -450,11 +452,19 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (space.spaceModel && !updateSpaceDto.spaceModelUuid) {
|
if (space.spaceModel && !updateSpaceDto.spaceModelUuid) {
|
||||||
space.spaceModel = null;
|
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
||||||
|
spaceModel: null,
|
||||||
|
});
|
||||||
await this.unlinkSpaceFromModel(space, queryRunner);
|
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) {
|
if (updateSpaceDto.spaceModelUuid) {
|
||||||
const spaceModel = await this.validationService.validateSpaceModel(
|
const spaceModel = await this.validationService.validateSpaceModel(
|
||||||
@ -478,6 +488,7 @@ export class SpaceService {
|
|||||||
project,
|
project,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.spaceModelService.linkToSpace(
|
await this.spaceModelService.linkToSpace(
|
||||||
space,
|
space,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
@ -485,14 +496,9 @@ export class SpaceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
|
||||||
|
|
||||||
if (hasSubspace) {
|
if (hasSubspace) {
|
||||||
space.spaceModel = null;
|
|
||||||
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
|
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
|
||||||
}
|
}
|
||||||
await queryRunner.manager.save(space);
|
|
||||||
|
|
||||||
if (hasSubspace) {
|
if (hasSubspace) {
|
||||||
await this.subSpaceService.modifySubSpace(
|
await this.subSpaceService.modifySubSpace(
|
||||||
@ -513,6 +519,7 @@ export class SpaceService {
|
|||||||
updateSpaceDto.subspace,
|
updateSpaceDto.subspace,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
@ -560,16 +567,23 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateSpaceProperties(
|
private async updateSpaceProperties(
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
updateSpaceDto: UpdateSpaceDto,
|
updateSpaceDto: UpdateSpaceDto,
|
||||||
): void {
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<void> {
|
||||||
const { spaceName, x, y, icon } = updateSpaceDto;
|
const { spaceName, x, y, icon } = updateSpaceDto;
|
||||||
|
|
||||||
if (spaceName) space.spaceName = spaceName;
|
const updateFields: Partial<SpaceEntity> = {};
|
||||||
if (x) space.x = x;
|
|
||||||
if (y) space.y = y;
|
if (spaceName) updateFields.spaceName = spaceName;
|
||||||
if (icon) space.icon = icon;
|
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(
|
async getSpacesHierarchyForSpace(
|
||||||
|
@ -327,51 +327,58 @@ export class SubSpaceService {
|
|||||||
if (!subspaceDtos || subspaceDtos.length === 0) {
|
if (!subspaceDtos || subspaceDtos.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const addedSubspaces = [];
|
try {
|
||||||
const updatedSubspaces = [];
|
const addedSubspaces = [];
|
||||||
|
const updatedSubspaces = [];
|
||||||
|
|
||||||
for (const subspace of subspaceDtos) {
|
for (const subspace of subspaceDtos) {
|
||||||
switch (subspace.action) {
|
switch (subspace.action) {
|
||||||
case ModifyAction.ADD:
|
case ModifyAction.ADD:
|
||||||
const addedSubspace = await this.handleAddAction(
|
const addedSubspace = await this.handleAddAction(
|
||||||
subspace,
|
subspace,
|
||||||
space,
|
space,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
if (addedSubspace) addedSubspaces.push(addedSubspace);
|
if (addedSubspace) addedSubspaces.push(addedSubspace);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case ModifyAction.UPDATE:
|
case ModifyAction.UPDATE:
|
||||||
const updatedSubspace = await this.handleUpdateAction(
|
const updatedSubspace = await this.handleUpdateAction(
|
||||||
subspace,
|
subspace,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
if (updatedSubspace) {
|
if (updatedSubspace) {
|
||||||
updatedSubspaces.push(updatedSubspace);
|
updatedSubspaces.push(updatedSubspace);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ModifyAction.DELETE:
|
case ModifyAction.DELETE:
|
||||||
await this.handleDeleteAction(subspace, queryRunner);
|
await this.handleDeleteAction(subspace, queryRunner);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Invalid action "${subspace.action}".`,
|
`Invalid action "${subspace.action}".`,
|
||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
|
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
|
||||||
(subspace) => subspace !== undefined,
|
(subspace) => subspace !== undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (combinedSubspaces.length > 0) {
|
if (combinedSubspaces.length > 0) {
|
||||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
|
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
|
||||||
combinedSubspaces,
|
combinedSubspaces,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
space,
|
space,
|
||||||
spaceTagUpdateDtos,
|
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