Merge pull request #205 from SyncrowIOT/bugfix/fix-update-of-disabled-subspace

subspace update fixed disabled
This commit is contained in:
hannathkadher
2025-01-08 11:27:53 +04:00
committed by GitHub
3 changed files with 57 additions and 48 deletions

View File

@ -117,6 +117,12 @@ export class SpaceModelService {
pageable.where = {
project: { uuid: param.projectUuid },
disabled: false,
subspaceModels: {
disabled: false,
},
tags: {
disabled: false,
},
};
pageable.include =
'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product';
@ -201,7 +207,6 @@ export class SpaceModelService {
return new SuccessResponseDto({
message: 'SpaceModel updated successfully',
data: spaceModel,
});
} catch (error) {
await queryRunner.rollbackTransaction();

View File

@ -5,7 +5,7 @@ import {
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
import { In, QueryRunner } from 'typeorm';
import { Not, QueryRunner } from 'typeorm';
import {
IDeletedSubsaceModelInterface,
ModifySubspaceModelPayload,
@ -123,6 +123,7 @@ export class SubSpaceModelService {
};
try {
for (const subspace of subspaceDtos) {
console.log(subspace.action);
switch (subspace.action) {
case ModifyAction.ADD:
const subspaceModel = await this.handleAddAction(
@ -258,7 +259,7 @@ export class SubSpaceModelService {
private async findOne(subspaceUuid: string): Promise<SubspaceModelEntity> {
const subspace = await this.subspaceModelRepository.findOne({
where: { uuid: subspaceUuid },
where: { uuid: subspaceUuid, disabled: false },
relations: ['tags', 'spaceModel'],
});
if (!subspace) {
@ -299,59 +300,52 @@ export class SubSpaceModelService {
}
}
private async checkDuplicateNames(
subspaceName: string,
spaceModelUuid: string,
excludeUuid?: string,
): Promise<void> {
const duplicateSubspace = await this.subspaceModelRepository.findOne({
where: {
subspaceName,
spaceModel: {
uuid: spaceModelUuid,
},
...(excludeUuid && { uuid: Not(excludeUuid) }),
},
});
if (duplicateSubspace) {
throw new HttpException(
`A subspace with the name '${subspaceName}' already exists within the same space model. ${spaceModelUuid}`,
HttpStatus.CONFLICT,
);
}
}
private async validateName(
names: string[],
spaceModel: SpaceModelEntity,
): Promise<void> {
try {
const seenNames = new Set<string>();
const duplicateNames = new Set<string>();
const seenNames = new Set<string>();
const duplicateNames = new Set<string>();
// Check for duplicate names within the input array
for (const name of names) {
if (!seenNames.add(name)) {
duplicateNames.add(name);
}
for (const name of names) {
if (!seenNames.add(name)) {
duplicateNames.add(name);
}
}
if (duplicateNames.size > 0) {
throw new HttpException(
`Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`,
HttpStatus.CONFLICT,
);
}
// Check for existing names in the database
const existingNames = await this.subspaceModelRepository.find({
select: ['subspaceName'],
where: {
subspaceName: In([...seenNames]),
spaceModel: {
uuid: spaceModel.uuid,
},
},
});
if (existingNames.length > 0) {
const existingNamesList = existingNames
.map((e) => e.subspaceName)
.join(', ');
throw new HttpException(
`Subspace model names already exist in the space: ${existingNamesList}`,
HttpStatus.BAD_REQUEST,
);
}
} catch (error) {
if (error instanceof HttpException) {
throw error; // Rethrow known HttpExceptions
}
// Handle unexpected errors
if (duplicateNames.size > 0) {
throw new HttpException(
`An error occurred while validating subspace model names: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
`Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`,
HttpStatus.CONFLICT,
);
}
for (const name of names) {
await this.checkDuplicateNames(name, spaceModel.uuid);
}
}
private async updateSubspaceName(
@ -360,6 +354,12 @@ export class SubSpaceModelService {
subspaceName?: string,
): Promise<void> {
if (subspaceName) {
await this.checkDuplicateNames(
subspaceName,
subSpaceModel.spaceModel.uuid,
subSpaceModel.uuid,
);
subSpaceModel.subspaceName = subspaceName;
await queryRunner.manager.save(subSpaceModel);
}

View File

@ -96,7 +96,11 @@ export class TagModelService {
async deleteTags(tagUuids: string[], queryRunner: QueryRunner) {
try {
const deletePromises = tagUuids.map((id) =>
queryRunner.manager.softDelete(this.tagModelRepository.target, id),
queryRunner.manager.update(
this.tagModelRepository.target,
{ uuid: id },
{ disabled: true },
),
);
await Promise.all(deletePromises);
@ -276,7 +280,7 @@ export class TagModelService {
async getTagByUuid(uuid: string): Promise<TagModel> {
const tag = await this.tagModelRepository.findOne({
where: { uuid },
where: { uuid, disabled: false },
relations: ['product'],
});
if (!tag) {