mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 00:37:54 +00:00
disable space
This commit is contained in:
@@ -19,6 +19,12 @@ import { ProjectService } from 'src/project/services';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { TagModelService } from './tag-model.service';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
||||
import {
|
||||
ModifiedTagsModelPayload,
|
||||
ModifySubspaceModelPayload,
|
||||
} from '../interfaces';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceModelService {
|
||||
@@ -28,6 +34,7 @@ export class SpaceModelService {
|
||||
private readonly projectService: ProjectService,
|
||||
private readonly subSpaceModelService: SubSpaceModelService,
|
||||
private readonly tagModelService: TagModelService,
|
||||
private commandBus: CommandBus,
|
||||
) {}
|
||||
|
||||
async createSpaceModel(
|
||||
@@ -136,6 +143,9 @@ export class SpaceModelService {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
let modifiedSubspaceModels: ModifySubspaceModelPayload = {};
|
||||
let modifiedTagsModelPayload: ModifiedTagsModelPayload = {};
|
||||
try {
|
||||
const { modelName } = dto;
|
||||
if (modelName) {
|
||||
@@ -145,22 +155,34 @@ export class SpaceModelService {
|
||||
}
|
||||
|
||||
if (dto.subspaceModels) {
|
||||
await this.subSpaceModelService.modifySubSpaceModels(
|
||||
dto.subspaceModels,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
modifiedSubspaceModels =
|
||||
await this.subSpaceModelService.modifySubSpaceModels(
|
||||
dto.subspaceModels,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
if (dto.tags) {
|
||||
await this.tagModelService.modifyTags(
|
||||
modifiedTagsModelPayload = await this.tagModelService.modifyTags(
|
||||
dto.tags,
|
||||
queryRunner,
|
||||
spaceModel,
|
||||
);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
await this.commandBus.execute(
|
||||
new PropogateUpdateSpaceModelCommand({
|
||||
spaceModel: spaceModel,
|
||||
modifiedSpaceModels: {
|
||||
modifiedSubspaceModels,
|
||||
modifiedTags: modifiedTagsModelPayload,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: 'SpaceModel updated successfully',
|
||||
data: spaceModel,
|
||||
|
||||
@@ -6,7 +6,11 @@ import {
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { IDeletedSubsaceModelInterface } from 'src/space-model/interfaces';
|
||||
import {
|
||||
IDeletedSubsaceModelInterface,
|
||||
ModifySubspaceModelPayload,
|
||||
UpdatedSubspaceModelPayload,
|
||||
} from 'src/space-model/interfaces';
|
||||
import {
|
||||
DeleteSubspaceModelDto,
|
||||
ModifySubspaceModelDto,
|
||||
@@ -99,17 +103,30 @@ export class SubSpaceModelService {
|
||||
subspaceDtos: ModifySubspaceModelDto[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
): Promise<ModifySubspaceModelPayload> {
|
||||
const modifiedSubspaceModels: ModifySubspaceModelPayload = {};
|
||||
for (const subspace of subspaceDtos) {
|
||||
switch (subspace.action) {
|
||||
case ModifyAction.ADD:
|
||||
await this.handleAddAction(subspace, spaceModel, queryRunner);
|
||||
const subspaceModel = await this.handleAddAction(
|
||||
subspace,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
modifiedSubspaceModels.addedSubspaceModels.push(subspaceModel);
|
||||
break;
|
||||
case ModifyAction.UPDATE:
|
||||
await this.handleUpdateAction(subspace, queryRunner);
|
||||
const updatedSubspaceModel = await this.handleUpdateAction(
|
||||
subspace,
|
||||
queryRunner,
|
||||
);
|
||||
modifiedSubspaceModels.updatedSubspaceModels.push(
|
||||
updatedSubspaceModel,
|
||||
);
|
||||
break;
|
||||
case ModifyAction.DELETE:
|
||||
await this.handleDeleteAction(subspace, queryRunner);
|
||||
modifiedSubspaceModels.deletedSubspaceModels.push(subspace.uuid);
|
||||
break;
|
||||
default:
|
||||
throw new HttpException(
|
||||
@@ -118,29 +135,42 @@ export class SubSpaceModelService {
|
||||
);
|
||||
}
|
||||
}
|
||||
return modifiedSubspaceModels;
|
||||
}
|
||||
|
||||
private async handleAddAction(
|
||||
subspace: ModifySubspaceModelDto,
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
): Promise<SubspaceModelEntity> {
|
||||
const createTagDtos: CreateTagModelDto[] =
|
||||
subspace.tags?.map((tag) => ({
|
||||
tag: tag.tag as string,
|
||||
productUuid: tag.productUuid as string,
|
||||
tag: tag.tag,
|
||||
productUuid: tag.productUuid,
|
||||
})) || [];
|
||||
await this.createSubSpaceModels(
|
||||
[{ subspaceName: subspace.subspaceName, tags: createTagDtos }],
|
||||
|
||||
const [createdSubspaceModel] = await this.createSubSpaceModels(
|
||||
[
|
||||
{
|
||||
subspaceName: subspace.subspaceName,
|
||||
tags: createTagDtos,
|
||||
},
|
||||
],
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
return createdSubspaceModel;
|
||||
}
|
||||
|
||||
private async handleUpdateAction(
|
||||
modifyDto: ModifySubspaceModelDto,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
): Promise<UpdatedSubspaceModelPayload> {
|
||||
const updatePayload: UpdatedSubspaceModelPayload = {
|
||||
subspaceModelUuid: modifyDto.uuid,
|
||||
};
|
||||
|
||||
const subspace = await this.findOne(modifyDto.uuid);
|
||||
|
||||
await this.updateSubspaceName(
|
||||
@@ -148,21 +178,24 @@ export class SubSpaceModelService {
|
||||
subspace,
|
||||
modifyDto.subspaceName,
|
||||
);
|
||||
updatePayload.subspaceName = modifyDto.subspaceName;
|
||||
|
||||
if (modifyDto.tags?.length) {
|
||||
await this.tagModelService.modifyTags(
|
||||
updatePayload.modifiedTags = await this.tagModelService.modifyTags(
|
||||
modifyDto.tags,
|
||||
queryRunner,
|
||||
null,
|
||||
subspace,
|
||||
);
|
||||
}
|
||||
|
||||
return updatePayload;
|
||||
}
|
||||
|
||||
private async handleDeleteAction(
|
||||
subspace: ModifySubspaceModelDto,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
) {
|
||||
const subspaceModel = await this.findOne(subspace.uuid);
|
||||
|
||||
await queryRunner.manager.update(
|
||||
|
||||
@@ -9,6 +9,7 @@ import { TagModelRepository } from '@app/common/modules/space-model';
|
||||
import { CreateTagModelDto, ModifyTagModelDto } from '../dtos';
|
||||
import { ProductService } from 'src/product/services';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { ModifiedTagsModelPayload } from '../interfaces';
|
||||
|
||||
@Injectable()
|
||||
export class TagModelService {
|
||||
@@ -132,9 +133,9 @@ export class TagModelService {
|
||||
queryRunner: QueryRunner,
|
||||
spaceModel?: SpaceModelEntity,
|
||||
subspaceModel?: SubspaceModelEntity,
|
||||
): Promise<void> {
|
||||
): Promise<ModifiedTagsModelPayload> {
|
||||
const modifiedTagModels: ModifiedTagsModelPayload = {};
|
||||
try {
|
||||
console.log(tags);
|
||||
for (const tag of tags) {
|
||||
if (tag.action === ModifyAction.ADD) {
|
||||
const createTagDto: CreateTagModelDto = {
|
||||
@@ -142,20 +143,28 @@ export class TagModelService {
|
||||
productUuid: tag.productUuid as string,
|
||||
};
|
||||
|
||||
await this.createTags(
|
||||
const newModel = await this.createTags(
|
||||
[createTagDto],
|
||||
queryRunner,
|
||||
spaceModel,
|
||||
subspaceModel,
|
||||
);
|
||||
modifiedTagModels.added.push(...newModel);
|
||||
} else if (tag.action === ModifyAction.UPDATE) {
|
||||
await this.updateTag(tag, queryRunner, spaceModel, subspaceModel);
|
||||
const updatedModel = await this.updateTag(
|
||||
tag,
|
||||
queryRunner,
|
||||
spaceModel,
|
||||
subspaceModel,
|
||||
);
|
||||
modifiedTagModels.updated.push(updatedModel);
|
||||
} else if (tag.action === ModifyAction.DELETE) {
|
||||
await queryRunner.manager.update(
|
||||
this.tagModelRepository.target,
|
||||
{ uuid: tag.uuid },
|
||||
{ disabled: true },
|
||||
);
|
||||
modifiedTagModels.deleted.push(tag.uuid);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
`Invalid action "${tag.action}" provided.`,
|
||||
@@ -163,6 +172,7 @@ export class TagModelService {
|
||||
);
|
||||
}
|
||||
}
|
||||
return modifiedTagModels;
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
@@ -235,7 +245,7 @@ export class TagModelService {
|
||||
});
|
||||
}
|
||||
|
||||
private async getTagByUuid(uuid: string): Promise<TagModel> {
|
||||
async getTagByUuid(uuid: string): Promise<TagModel> {
|
||||
const tag = await this.tagModelRepository.findOne({
|
||||
where: { uuid },
|
||||
relations: ['product'],
|
||||
@@ -248,4 +258,38 @@ export class TagModelService {
|
||||
}
|
||||
return tag;
|
||||
}
|
||||
|
||||
async getTagByName(
|
||||
tag: string,
|
||||
subspaceUuid?: string,
|
||||
spaceUuid?: string,
|
||||
): Promise<TagModel> {
|
||||
const queryConditions: any = { tag };
|
||||
|
||||
if (spaceUuid) {
|
||||
queryConditions.spaceModel = { uuid: spaceUuid };
|
||||
} else if (subspaceUuid) {
|
||||
queryConditions.subspaceModel = { uuid: subspaceUuid };
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'Either spaceUuid or subspaceUuid must be provided.',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
queryConditions.disabled = false;
|
||||
|
||||
const existingTag = await this.tagModelRepository.findOne({
|
||||
where: queryConditions,
|
||||
relations: ['product'],
|
||||
});
|
||||
|
||||
if (!existingTag) {
|
||||
throw new HttpException(
|
||||
`Tag model with tag "${tag}" not found.`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
return existingTag;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user