mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:14:54 +00:00
Merge pull request #280 from SyncrowIOT/SP-1078-be-implement-api-to-assign-a-space-model-or-reassign-it-to-a-space
add space linking when create or update space
This commit is contained in:
@ -40,6 +40,10 @@ import {
|
|||||||
ORPHAN_SPACE_NAME,
|
ORPHAN_SPACE_NAME,
|
||||||
} from '@app/common/constants/orphan-constant';
|
} from '@app/common/constants/orphan-constant';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
|
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
|
||||||
|
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||||
|
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||||
|
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceModelService {
|
export class SpaceModelService {
|
||||||
@ -387,10 +391,15 @@ export class SpaceModelService {
|
|||||||
async linkToSpace(
|
async linkToSpace(
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner?: QueryRunner, // Make queryRunner optional
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
space.spaceModel = spaceModel;
|
space.spaceModel = spaceModel;
|
||||||
|
if (queryRunner) {
|
||||||
|
await queryRunner.manager.save(SpaceEntity, space);
|
||||||
|
} else {
|
||||||
await this.spaceRepository.save(space);
|
await this.spaceRepository.save(space);
|
||||||
|
}
|
||||||
|
|
||||||
const spaceProductAllocations = spaceModel.productAllocations.map(
|
const spaceProductAllocations = spaceModel.productAllocations.map(
|
||||||
(modelAllocation) =>
|
(modelAllocation) =>
|
||||||
@ -401,8 +410,17 @@ export class SpaceModelService {
|
|||||||
tags: modelAllocation.tags,
|
tags: modelAllocation.tags,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
await this.spaceProductAllocationRepository.save(spaceProductAllocations);
|
|
||||||
|
|
||||||
|
if (queryRunner) {
|
||||||
|
await queryRunner.manager.save(
|
||||||
|
SpaceProductAllocationEntity,
|
||||||
|
spaceProductAllocations,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await this.spaceProductAllocationRepository.save(
|
||||||
|
spaceProductAllocations,
|
||||||
|
);
|
||||||
|
}
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
spaceModel.subspaceModels.map(async (subspaceModel) => {
|
spaceModel.subspaceModels.map(async (subspaceModel) => {
|
||||||
const subspace = this.subspaceRepository.create({
|
const subspace = this.subspaceRepository.create({
|
||||||
@ -411,7 +429,11 @@ export class SpaceModelService {
|
|||||||
space: space,
|
space: space,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (queryRunner) {
|
||||||
|
await queryRunner.manager.save(SubspaceEntity, subspace);
|
||||||
|
} else {
|
||||||
await this.subspaceRepository.save(subspace);
|
await this.subspaceRepository.save(subspace);
|
||||||
|
}
|
||||||
|
|
||||||
const subspaceAllocations = subspaceModel.productAllocations.map(
|
const subspaceAllocations = subspaceModel.productAllocations.map(
|
||||||
(modelAllocation) =>
|
(modelAllocation) =>
|
||||||
@ -424,10 +446,17 @@ export class SpaceModelService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (subspaceAllocations.length) {
|
if (subspaceAllocations.length) {
|
||||||
|
if (queryRunner) {
|
||||||
|
await queryRunner.manager.save(
|
||||||
|
SubspaceProductAllocationEntity,
|
||||||
|
subspaceAllocations,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
await this.subspaceProductAllocationRepository.save(
|
await this.subspaceProductAllocationRepository.save(
|
||||||
subspaceAllocations,
|
subspaceAllocations,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -441,10 +470,31 @@ export class SpaceModelService {
|
|||||||
async overwriteSpace(
|
async overwriteSpace(
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
project: ProjectEntity,
|
project: ProjectEntity,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
const spaceProductAllocationRepository = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(SpaceProductAllocationEntity)
|
||||||
|
: this.spaceProductAllocationRepository;
|
||||||
|
|
||||||
|
const subspaceRepository = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(SubspaceEntity)
|
||||||
|
: this.subspaceRepository;
|
||||||
|
|
||||||
|
const subspaceProductAllocationRepository = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(SubspaceProductAllocationEntity)
|
||||||
|
: this.subspaceProductAllocationRepository;
|
||||||
|
|
||||||
|
const spaceRepository = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(SpaceEntity)
|
||||||
|
: this.spaceRepository;
|
||||||
|
|
||||||
|
const deviceRepository = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(DeviceEntity)
|
||||||
|
: this.deviceRepository;
|
||||||
|
|
||||||
if (space.productAllocations.length) {
|
if (space.productAllocations.length) {
|
||||||
await this.spaceProductAllocationRepository.delete({
|
await spaceProductAllocationRepository.delete({
|
||||||
uuid: In(
|
uuid: In(
|
||||||
space.productAllocations.map((allocation) => allocation.uuid),
|
space.productAllocations.map((allocation) => allocation.uuid),
|
||||||
),
|
),
|
||||||
@ -453,13 +503,13 @@ export class SpaceModelService {
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
space.subspaces.map(async (subspace) => {
|
space.subspaces.map(async (subspace) => {
|
||||||
await this.subspaceRepository.update(
|
await subspaceRepository.update(
|
||||||
{ uuid: subspace.uuid },
|
{ uuid: subspace.uuid },
|
||||||
{ disabled: true },
|
{ disabled: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
if (subspace.productAllocations.length) {
|
if (subspace.productAllocations.length) {
|
||||||
await this.subspaceProductAllocationRepository.delete({
|
await subspaceProductAllocationRepository.delete({
|
||||||
uuid: In(
|
uuid: In(
|
||||||
subspace.productAllocations.map(
|
subspace.productAllocations.map(
|
||||||
(allocation) => allocation.uuid,
|
(allocation) => allocation.uuid,
|
||||||
@ -471,7 +521,7 @@ export class SpaceModelService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (space.devices.length > 0) {
|
if (space.devices.length > 0) {
|
||||||
const orphanSpace = await this.spaceRepository.findOne({
|
const orphanSpace = await spaceRepository.findOne({
|
||||||
where: {
|
where: {
|
||||||
community: {
|
community: {
|
||||||
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
|
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
|
||||||
@ -487,7 +537,7 @@ export class SpaceModelService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.deviceRepository.update(
|
await deviceRepository.update(
|
||||||
{ uuid: In(space.devices.map((device) => device.uuid)) },
|
{ uuid: In(space.devices.map((device) => device.uuid)) },
|
||||||
{ spaceDevice: orphanSpace },
|
{ spaceDevice: orphanSpace },
|
||||||
);
|
);
|
||||||
|
|||||||
@ -58,4 +58,11 @@ export class UpdateSpaceDto {
|
|||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => ModifyTagDto)
|
@Type(() => ModifyTagDto)
|
||||||
tags?: ModifyTagDto[];
|
tags?: ModifyTagDto[];
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'UUID of the Space',
|
||||||
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
spaceModelUuid?: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -104,10 +104,23 @@ export class SpaceService {
|
|||||||
this.subSpaceService.extractTagsFromSubspace(subspaces);
|
this.subSpaceService.extractTagsFromSubspace(subspaces);
|
||||||
const allTags = [...tags, ...subspaceTags];
|
const allTags = [...tags, ...subspaceTags];
|
||||||
this.validateUniqueTags(allTags);
|
this.validateUniqueTags(allTags);
|
||||||
|
if (spaceModelUuid) {
|
||||||
|
const hasDependencies = subspaces?.length > 0 || tags?.length > 0;
|
||||||
|
if (!hasDependencies && !newSpace.spaceModel) {
|
||||||
|
await this.spaceModelService.linkToSpace(
|
||||||
|
newSpace,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
} else if (hasDependencies) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Space cannot be linked to a model because it has existing dependencies (subspaces or tags).`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
spaceModelUuid &&
|
|
||||||
this.createFromModel(spaceModelUuid, queryRunner, newSpace),
|
|
||||||
direction && parent
|
direction && parent
|
||||||
? this.spaceLinkService.saveSpaceLink(
|
? this.spaceLinkService.saveSpaceLink(
|
||||||
parent.uuid,
|
parent.uuid,
|
||||||
@ -125,9 +138,8 @@ export class SpaceService {
|
|||||||
projectUuid,
|
projectUuid,
|
||||||
)
|
)
|
||||||
: Promise.resolve(),
|
: Promise.resolve(),
|
||||||
|
|
||||||
tags?.length
|
tags?.length
|
||||||
? this.createTags(tags, projectUuid, queryRunner, space)
|
? this.createTags(tags, projectUuid, queryRunner, newSpace)
|
||||||
: Promise.resolve(),
|
: Promise.resolve(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -435,6 +447,9 @@ export class SpaceService {
|
|||||||
try {
|
try {
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
|
const project = await this.spaceModelService.validateProject(
|
||||||
|
params.projectUuid,
|
||||||
|
);
|
||||||
|
|
||||||
const space =
|
const space =
|
||||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
@ -452,6 +467,36 @@ export class SpaceService {
|
|||||||
|
|
||||||
this.updateSpaceProperties(space, updateSpaceDto);
|
this.updateSpaceProperties(space, updateSpaceDto);
|
||||||
|
|
||||||
|
if (updateSpaceDto.spaceModelUuid) {
|
||||||
|
const spaceModel = await this.validationService.validateSpaceModel(
|
||||||
|
updateSpaceDto.spaceModelUuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
const hasDependencies =
|
||||||
|
space.devices?.length > 0 ||
|
||||||
|
space.subspaces?.length > 0 ||
|
||||||
|
space.productAllocations?.length > 0;
|
||||||
|
|
||||||
|
if (!hasDependencies && !space.spaceModel) {
|
||||||
|
await this.spaceModelService.linkToSpace(
|
||||||
|
space,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
} else if (hasDependencies) {
|
||||||
|
await this.spaceModelService.overwriteSpace(
|
||||||
|
space,
|
||||||
|
project,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
await this.spaceModelService.linkToSpace(
|
||||||
|
space,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
||||||
const hasTags = updateSpaceDto.tags?.length > 0;
|
const hasTags = updateSpaceDto.tags?.length > 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user