mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 09:54:55 +00:00
updated space update
This commit is contained in:
@ -7,7 +7,9 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
AddSpaceDto,
|
||||
AddSubspaceDto,
|
||||
CommunitySpaceParam,
|
||||
CreateTagDto,
|
||||
GetSpaceParam,
|
||||
UpdateSpaceDto,
|
||||
} from '../dtos';
|
||||
@ -17,10 +19,11 @@ import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { generateRandomString } from '@app/common/helper/randomString';
|
||||
import { SpaceLinkService } from './space-link';
|
||||
import { SubSpaceService } from './subspace';
|
||||
import { DataSource, Not } from 'typeorm';
|
||||
import { DataSource, Not, QueryRunner } from 'typeorm';
|
||||
import { ValidationService } from './space-validation.service';
|
||||
import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { TagService } from './tag';
|
||||
import { SpaceModelService } from 'src/space-model/services';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceService {
|
||||
@ -31,6 +34,7 @@ export class SpaceService {
|
||||
private readonly subSpaceService: SubSpaceService,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly tagService: TagService,
|
||||
private readonly spaceModelService: SpaceModelService,
|
||||
) {}
|
||||
|
||||
async createSpace(
|
||||
@ -78,31 +82,23 @@ export class SpaceService {
|
||||
|
||||
const newSpace = await queryRunner.manager.save(space);
|
||||
|
||||
if (direction && parent) {
|
||||
await this.spaceLinkService.saveSpaceLink(
|
||||
parent.uuid,
|
||||
newSpace.uuid,
|
||||
direction,
|
||||
);
|
||||
}
|
||||
|
||||
if (subspaces?.length) {
|
||||
await this.subSpaceService.createSubspacesFromDto(
|
||||
subspaces,
|
||||
newSpace,
|
||||
queryRunner,
|
||||
tags,
|
||||
);
|
||||
}
|
||||
|
||||
if (tags?.length) {
|
||||
newSpace.tags = await this.tagService.createTags(
|
||||
tags,
|
||||
queryRunner,
|
||||
newSpace,
|
||||
null,
|
||||
);
|
||||
}
|
||||
await Promise.all([
|
||||
spaceModelUuid &&
|
||||
this.createFromModel(spaceModelUuid, queryRunner, newSpace),
|
||||
direction && parent
|
||||
? this.spaceLinkService.saveSpaceLink(
|
||||
parent.uuid,
|
||||
newSpace.uuid,
|
||||
direction,
|
||||
)
|
||||
: Promise.resolve(),
|
||||
subspaces?.length
|
||||
? this.createSubspaces(subspaces, newSpace, queryRunner, tags)
|
||||
: Promise.resolve(),
|
||||
tags?.length
|
||||
? this.createTags(tags, queryRunner, newSpace)
|
||||
: Promise.resolve(),
|
||||
]);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
@ -123,6 +119,41 @@ export class SpaceService {
|
||||
}
|
||||
}
|
||||
|
||||
async createFromModel(
|
||||
spaceModelUuid: string,
|
||||
queryRunner: QueryRunner,
|
||||
space: SpaceEntity,
|
||||
) {
|
||||
try {
|
||||
const spaceModel =
|
||||
await this.spaceModelService.validateSpaceModel(spaceModelUuid);
|
||||
|
||||
space.spaceModel = spaceModel;
|
||||
await queryRunner.manager.save(SpaceEntity, space);
|
||||
|
||||
await this.subSpaceService.createSubSpaceFromModel(
|
||||
spaceModel.subspaceModels,
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
await this.tagService.createTagsFromModel(
|
||||
queryRunner,
|
||||
spaceModel.tags,
|
||||
space,
|
||||
null,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
'An error occurred while creating the space from space model',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getSpacesHierarchyForCommunity(
|
||||
params: CommunitySpaceParam,
|
||||
): Promise<BaseResponseDto> {
|
||||
@ -274,29 +305,32 @@ export class SpaceService {
|
||||
|
||||
if (space.spaceName === ORPHAN_SPACE_NAME) {
|
||||
throw new HttpException(
|
||||
`space ${ORPHAN_SPACE_NAME} cannot be updated`,
|
||||
`Space "${ORPHAN_SPACE_NAME}" cannot be updated`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
if (updateSpaceDto.spaceName) space.spaceName = updateSpaceDto.spaceName;
|
||||
|
||||
if (updateSpaceDto.x) space.x = updateSpaceDto.x;
|
||||
|
||||
if (updateSpaceDto.y) space.y = updateSpaceDto.y;
|
||||
if (updateSpaceDto.icon) space.icon = updateSpaceDto.icon;
|
||||
if (updateSpaceDto.icon) space.icon = updateSpaceDto.icon;
|
||||
this.updateSpaceProperties(space, updateSpaceDto);
|
||||
|
||||
await queryRunner.manager.save(space);
|
||||
|
||||
if (updateSpaceDto.subspaceModels) {
|
||||
const hasSubspace = updateSpaceDto.subspace?.length > 0;
|
||||
const hasTags = updateSpaceDto.tags?.length > 0;
|
||||
|
||||
if (hasSubspace || hasTags) {
|
||||
await this.tagService.unlinkModels(space.tags, queryRunner);
|
||||
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
|
||||
}
|
||||
|
||||
if (hasSubspace) {
|
||||
await this.subSpaceService.modifySubSpace(
|
||||
updateSpaceDto.subspaceModels,
|
||||
updateSpaceDto.subspace,
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
if (updateSpaceDto.tags) {
|
||||
if (hasTags) {
|
||||
await this.tagService.modifyTags(
|
||||
updateSpaceDto.tags,
|
||||
queryRunner,
|
||||
@ -317,6 +351,7 @@ export class SpaceService {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
'An error occurred while updating the space',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
@ -326,6 +361,18 @@ export class SpaceService {
|
||||
}
|
||||
}
|
||||
|
||||
private updateSpaceProperties(
|
||||
space: SpaceEntity,
|
||||
updateSpaceDto: UpdateSpaceDto,
|
||||
): 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;
|
||||
}
|
||||
|
||||
async getSpacesHierarchyForSpace(
|
||||
params: GetSpaceParam,
|
||||
): Promise<BaseResponseDto> {
|
||||
@ -339,7 +386,7 @@ export class SpaceService {
|
||||
try {
|
||||
// Get all spaces that are children of the provided space, including the parent-child relations
|
||||
const spaces = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: spaceUuid } },
|
||||
where: { parent: { uuid: spaceUuid }, disabled: false },
|
||||
relations: ['parent', 'children'], // Include parent and children relations
|
||||
});
|
||||
|
||||
@ -426,4 +473,26 @@ export class SpaceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async createSubspaces(
|
||||
subspaces: AddSubspaceDto[],
|
||||
space: SpaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
tags: CreateTagDto[],
|
||||
): Promise<void> {
|
||||
space.subspaces = await this.subSpaceService.createSubspacesFromDto(
|
||||
subspaces,
|
||||
space,
|
||||
queryRunner,
|
||||
tags,
|
||||
);
|
||||
}
|
||||
|
||||
private async createTags(
|
||||
tags: CreateTagDto[],
|
||||
queryRunner: QueryRunner,
|
||||
space: SpaceEntity,
|
||||
): Promise<void> {
|
||||
space.tags = await this.tagService.createTags(tags, queryRunner, space);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user