mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-26 00:17:54 +00:00
update in tag service
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../../dtos';
|
||||
import {
|
||||
AddSubspaceDto,
|
||||
CreateTagDto,
|
||||
DeleteSubspaceDto,
|
||||
GetSpaceParam,
|
||||
GetSubSpaceParam,
|
||||
ModifySubspaceDto,
|
||||
} from '../../dtos';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import {
|
||||
TypeORMCustomModel,
|
||||
@@ -19,12 +26,15 @@ import {
|
||||
} from '@app/common/modules/space-model';
|
||||
import { ValidationService } from '../space-validation.service';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { TagService } from '../tag';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
|
||||
@Injectable()
|
||||
export class SubSpaceService {
|
||||
constructor(
|
||||
private readonly subspaceRepository: SubspaceRepository,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly tagService: TagService,
|
||||
) {}
|
||||
|
||||
async createSubspaces(
|
||||
@@ -71,6 +81,7 @@ export class SubSpaceService {
|
||||
addSubspaceDtos: AddSubspaceDto[],
|
||||
space: SpaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
otherTags?: CreateTagDto[],
|
||||
): Promise<SubspaceEntity[]> {
|
||||
try {
|
||||
const subspaceData = addSubspaceDtos.map((dto) => ({
|
||||
@@ -80,6 +91,20 @@ export class SubSpaceService {
|
||||
|
||||
const subspaces = await this.createSubspaces(subspaceData, queryRunner);
|
||||
|
||||
await Promise.all(
|
||||
addSubspaceDtos.map(async (dto, index) => {
|
||||
const subspace = subspaces[index];
|
||||
if (dto.tags?.length) {
|
||||
subspace.tags = await this.tagService.createTags(
|
||||
dto.tags,
|
||||
queryRunner,
|
||||
null,
|
||||
subspace,
|
||||
otherTags,
|
||||
);
|
||||
}
|
||||
}),
|
||||
);
|
||||
return subspaces;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
@@ -144,43 +169,6 @@ export class SubSpaceService {
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
const { communityUuid, subSpaceUuid, spaceUuid, projectUuid } = params;
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
);
|
||||
try {
|
||||
const subSpace = await this.subspaceRepository.findOne({
|
||||
where: {
|
||||
uuid: subSpaceUuid,
|
||||
},
|
||||
});
|
||||
|
||||
// If space is not found, throw a NotFoundException
|
||||
if (!subSpace) {
|
||||
throw new HttpException(
|
||||
`Sub Space with UUID ${subSpaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
return new SuccessResponseDto({
|
||||
message: `Subspace with ID ${subSpaceUuid} successfully fetched`,
|
||||
data: subSpace,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error; // If it's an HttpException, rethrow it
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'An error occurred while deleting the subspace',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateSubSpace(
|
||||
params: GetSubSpaceParam,
|
||||
updateSubSpaceDto: AddSubspaceDto,
|
||||
@@ -256,4 +244,153 @@ export class SubSpaceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteSubspaces(
|
||||
deleteDtos: DeleteSubspaceDto[],
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
const deleteResults: { uuid: string }[] = [];
|
||||
|
||||
for (const dto of deleteDtos) {
|
||||
const subspaceModel = await this.findOne(dto.subspaceUuid);
|
||||
|
||||
await queryRunner.manager.update(
|
||||
this.subspaceRepository.target,
|
||||
{ uuid: dto.subspaceUuid },
|
||||
{ disabled: true },
|
||||
);
|
||||
|
||||
if (subspaceModel.tags?.length) {
|
||||
const modifyTagDtos = subspaceModel.tags.map((tag) => ({
|
||||
uuid: tag.uuid,
|
||||
action: ModifyAction.DELETE,
|
||||
}));
|
||||
await this.tagService.modifyTags(
|
||||
modifyTagDtos,
|
||||
queryRunner,
|
||||
null,
|
||||
subspaceModel,
|
||||
);
|
||||
}
|
||||
|
||||
deleteResults.push({ uuid: dto.subspaceUuid });
|
||||
}
|
||||
|
||||
return deleteResults;
|
||||
}
|
||||
|
||||
async modifySubSpace(
|
||||
subspaceDtos: ModifySubspaceDto[],
|
||||
space: SpaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
for (const subspace of subspaceDtos) {
|
||||
switch (subspace.action) {
|
||||
case ModifyAction.ADD:
|
||||
await this.handleAddAction(subspace, space, queryRunner);
|
||||
break;
|
||||
case ModifyAction.UPDATE:
|
||||
await this.handleUpdateAction(subspace, queryRunner);
|
||||
break;
|
||||
case ModifyAction.DELETE:
|
||||
await this.handleDeleteAction(subspace, queryRunner);
|
||||
break;
|
||||
default:
|
||||
throw new HttpException(
|
||||
`Invalid action "${subspace.action}".`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleAddAction(
|
||||
subspace: ModifySubspaceDto,
|
||||
space: SpaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const createTagDtos: CreateTagDto[] =
|
||||
subspace.tags?.map((tag) => ({
|
||||
tag: tag.tag as string,
|
||||
productUuid: tag.productUuid as string,
|
||||
})) || [];
|
||||
await this.createSubspacesFromDto(
|
||||
[{ subspaceName: subspace.subspaceName, tags: createTagDtos }],
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
private async handleUpdateAction(
|
||||
modifyDto: ModifySubspaceDto,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const subspace = await this.findOne(modifyDto.uuid);
|
||||
|
||||
await this.updateSubspaceName(
|
||||
queryRunner,
|
||||
subspace,
|
||||
modifyDto.subspaceName,
|
||||
);
|
||||
|
||||
if (modifyDto.tags?.length) {
|
||||
await this.tagService.modifyTags(
|
||||
modifyDto.tags,
|
||||
queryRunner,
|
||||
null,
|
||||
subspace,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleDeleteAction(
|
||||
subspace: ModifySubspaceDto,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const subspaceModel = await this.findOne(subspace.uuid);
|
||||
|
||||
await queryRunner.manager.update(
|
||||
this.subspaceRepository.target,
|
||||
{ uuid: subspace.uuid },
|
||||
{ disabled: true },
|
||||
);
|
||||
|
||||
if (subspaceModel.tags?.length) {
|
||||
const modifyTagDtos = subspaceModel.tags.map((tag) => ({
|
||||
uuid: tag.uuid,
|
||||
action: ModifyAction.DELETE,
|
||||
}));
|
||||
await this.tagService.modifyTags(
|
||||
modifyTagDtos,
|
||||
queryRunner,
|
||||
null,
|
||||
subspaceModel,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async findOne(subspaceUuid: string): Promise<SubspaceEntity> {
|
||||
const subspace = await this.subspaceRepository.findOne({
|
||||
where: { uuid: subspaceUuid },
|
||||
relations: ['tags'],
|
||||
});
|
||||
if (!subspace) {
|
||||
throw new HttpException(
|
||||
`SubspaceModel with UUID ${subspaceUuid} not found.`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
return subspace;
|
||||
}
|
||||
|
||||
private async updateSubspaceName(
|
||||
queryRunner: QueryRunner,
|
||||
subSpace: SubspaceEntity,
|
||||
subspaceName?: string,
|
||||
): Promise<void> {
|
||||
if (subspaceName) {
|
||||
subSpace.subspaceName = subspaceName;
|
||||
await queryRunner.manager.save(subSpace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user