disable space

This commit is contained in:
hannathkadher
2024-12-30 10:06:33 +04:00
parent 0d539883f1
commit 6a89a17ce9
17 changed files with 604 additions and 177 deletions
@@ -0,0 +1,5 @@
import { SubspaceEntity } from '@app/common/modules/space';
export interface ModifySubspacePayload {
addedSubspaces?: SubspaceEntity[];
}
View File
+32 -1
View File
@@ -317,8 +317,8 @@ export class SpaceService {
if (hasSubspace) {
await this.subSpaceService.modifySubSpace(
updateSpaceDto.subspace,
space,
queryRunner,
space,
);
}
@@ -353,6 +353,37 @@ export class SpaceService {
}
}
async unlinkSpaceFromModel(
space: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
try {
await queryRunner.manager.update(
this.spaceRepository.target,
{ uuid: space.uuid },
{
spaceModel: null,
},
);
// Unlink subspaces and tags if they exist
if (space.subspaces || space.tags) {
if (space.tags) {
await this.tagService.unlinkModels(space.tags, queryRunner);
}
if (space.subspaces) {
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
}
}
} catch (error) {
throw new HttpException(
`Failed to unlink space model: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private updateSpaceProperties(
space: SpaceEntity,
updateSpaceDto: UpdateSpaceDto,
+34 -16
View File
@@ -26,6 +26,7 @@ import { SubspaceRepository } from '@app/common/modules/space/repositories/subsp
import { TagService } from '../tag';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { SubspaceDeviceService } from './subspace-device.service';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
@Injectable()
export class SubSpaceService {
@@ -317,9 +318,9 @@ export class SubSpaceService {
async modifySubSpace(
subspaceDtos: ModifySubspaceDto[],
space: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
space?: SpaceEntity,
) {
for (const subspace of subspaceDtos) {
switch (subspace.action) {
case ModifyAction.ADD:
@@ -382,17 +383,18 @@ export class SubSpaceService {
subspace: ModifySubspaceDto,
space: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
): Promise<SubspaceEntity> {
const createTagDtos: CreateTagDto[] =
subspace.tags?.map((tag) => ({
tag: tag.tag as string,
productUuid: tag.productUuid as string,
})) || [];
await this.createSubspacesFromDto(
const subSpace = await this.createSubspacesFromDto(
[{ subspaceName: subspace.subspaceName, tags: createTagDtos }],
space,
queryRunner,
);
return subSpace[0];
}
private async handleUpdateAction(
@@ -400,16 +402,25 @@ export class SubSpaceService {
queryRunner: QueryRunner,
): Promise<void> {
const subspace = await this.findOne(modifyDto.uuid);
await this.updateSubspaceName(
await this.update(
queryRunner,
subspace,
modifyDto.subspaceName,
modifyDto.tags,
);
}
if (modifyDto.tags?.length) {
async update(
queryRunner: QueryRunner,
subspace: SubspaceEntity,
subspaceName?: string,
modifyTagDto?: ModifyTagDto[],
) {
await this.updateSubspaceName(queryRunner, subspace, subspaceName);
if (modifyTagDto?.length) {
await this.tagService.modifyTags(
modifyDto.tags,
modifyTagDto,
queryRunner,
null,
subspace,
@@ -417,11 +428,11 @@ export class SubSpaceService {
}
}
private async handleDeleteAction(
subspace: ModifySubspaceDto,
async handleDeleteAction(
modifyDto: ModifySubspaceDto,
queryRunner: QueryRunner,
): Promise<void> {
const subspaceModel = await this.findOne(subspace.uuid);
const subspace = await this.findOne(modifyDto.uuid);
await queryRunner.manager.update(
this.subspaceRepository.target,
@@ -429,8 +440,8 @@ export class SubSpaceService {
{ disabled: true },
);
if (subspaceModel.tags?.length) {
const modifyTagDtos = subspaceModel.tags.map((tag) => ({
if (subspace.tags?.length) {
const modifyTagDtos = subspace.tags.map((tag) => ({
uuid: tag.uuid,
action: ModifyAction.DELETE,
}));
@@ -438,7 +449,14 @@ export class SubSpaceService {
modifyTagDtos,
queryRunner,
null,
subspaceModel,
subspace,
);
}
if (subspace.devices.length > 0) {
await this.deviceService.deleteSubspaceDevices(
subspace.devices,
queryRunner,
);
}
}
@@ -446,7 +464,7 @@ export class SubSpaceService {
private async findOne(subspaceUuid: string): Promise<SubspaceEntity> {
const subspace = await this.subspaceRepository.findOne({
where: { uuid: subspaceUuid },
relations: ['tags', 'space'],
relations: ['tags', 'space', 'devices', 'tags.product', 'tags.device'],
});
if (!subspace) {
throw new HttpException(
@@ -457,7 +475,7 @@ export class SubSpaceService {
return subspace;
}
private async updateSubspaceName(
async updateSubspaceName(
queryRunner: QueryRunner,
subSpace: SubspaceEntity,
subspaceName?: string,
+61
View File
@@ -63,6 +63,7 @@ export class TagService {
await queryRunner.manager.save(tags);
}
async updateTag(
tag: ModifyTagDto,
queryRunner: QueryRunner,
@@ -90,6 +91,38 @@ export class TagService {
}
}
async updateTagsFromModel(
model: TagModel,
queryRunner: QueryRunner,
): Promise<void> {
try {
const tags = await this.tagRepository.find({
where: {
model: {
uuid: model.uuid,
},
},
});
if (!tags.length) return;
await queryRunner.manager.update(
this.tagRepository.target,
{ model: { uuid: model.uuid } },
{ tag: model.tag },
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`Failed to update tags for model with UUID: ${model.uuid}. Reason: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async deleteTags(tagUuids: string[], queryRunner: QueryRunner) {
if (!tagUuids?.length) return;
@@ -109,6 +142,34 @@ export class TagService {
}
}
async deleteTagFromModel(modelUuid: string, queryRunner: QueryRunner) {
try {
const tags = await this.tagRepository.find({
where: {
model: {
uuid: modelUuid,
},
},
});
if (!tags.length) return;
await queryRunner.manager.update(
this.tagRepository.target,
{ model: { uuid: modelUuid } },
{ disabled: true, device: null },
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`Failed to update tags for model with UUID: ${modelUuid}. Reason: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async modifyTags(
tags: ModifyTagDto[],
queryRunner: QueryRunner,