mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
Merge pull request #205 from SyncrowIOT/bugfix/fix-update-of-disabled-subspace
subspace update fixed disabled
This commit is contained in:
@ -117,6 +117,12 @@ export class SpaceModelService {
|
|||||||
pageable.where = {
|
pageable.where = {
|
||||||
project: { uuid: param.projectUuid },
|
project: { uuid: param.projectUuid },
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
subspaceModels: {
|
||||||
|
disabled: false,
|
||||||
|
},
|
||||||
|
tags: {
|
||||||
|
disabled: false,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
pageable.include =
|
pageable.include =
|
||||||
'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product';
|
'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product';
|
||||||
@ -201,7 +207,6 @@ export class SpaceModelService {
|
|||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: 'SpaceModel updated successfully',
|
message: 'SpaceModel updated successfully',
|
||||||
data: spaceModel,
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await queryRunner.rollbackTransaction();
|
await queryRunner.rollbackTransaction();
|
||||||
|
@ -5,7 +5,7 @@ import {
|
|||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
|
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
|
||||||
import { In, QueryRunner } from 'typeorm';
|
import { Not, QueryRunner } from 'typeorm';
|
||||||
import {
|
import {
|
||||||
IDeletedSubsaceModelInterface,
|
IDeletedSubsaceModelInterface,
|
||||||
ModifySubspaceModelPayload,
|
ModifySubspaceModelPayload,
|
||||||
@ -123,6 +123,7 @@ export class SubSpaceModelService {
|
|||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
for (const subspace of subspaceDtos) {
|
for (const subspace of subspaceDtos) {
|
||||||
|
console.log(subspace.action);
|
||||||
switch (subspace.action) {
|
switch (subspace.action) {
|
||||||
case ModifyAction.ADD:
|
case ModifyAction.ADD:
|
||||||
const subspaceModel = await this.handleAddAction(
|
const subspaceModel = await this.handleAddAction(
|
||||||
@ -258,7 +259,7 @@ export class SubSpaceModelService {
|
|||||||
|
|
||||||
private async findOne(subspaceUuid: string): Promise<SubspaceModelEntity> {
|
private async findOne(subspaceUuid: string): Promise<SubspaceModelEntity> {
|
||||||
const subspace = await this.subspaceModelRepository.findOne({
|
const subspace = await this.subspaceModelRepository.findOne({
|
||||||
where: { uuid: subspaceUuid },
|
where: { uuid: subspaceUuid, disabled: false },
|
||||||
relations: ['tags', 'spaceModel'],
|
relations: ['tags', 'spaceModel'],
|
||||||
});
|
});
|
||||||
if (!subspace) {
|
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(
|
private async validateName(
|
||||||
names: string[],
|
names: string[],
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
const seenNames = new Set<string>();
|
||||||
const seenNames = new Set<string>();
|
const duplicateNames = new Set<string>();
|
||||||
const duplicateNames = new Set<string>();
|
|
||||||
|
|
||||||
// Check for duplicate names within the input array
|
for (const name of names) {
|
||||||
for (const name of names) {
|
if (!seenNames.add(name)) {
|
||||||
if (!seenNames.add(name)) {
|
duplicateNames.add(name);
|
||||||
duplicateNames.add(name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (duplicateNames.size > 0) {
|
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
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`An error occurred while validating subspace model names: ${error.message}`,
|
`Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.CONFLICT,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const name of names) {
|
||||||
|
await this.checkDuplicateNames(name, spaceModel.uuid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async updateSubspaceName(
|
private async updateSubspaceName(
|
||||||
@ -360,6 +354,12 @@ export class SubSpaceModelService {
|
|||||||
subspaceName?: string,
|
subspaceName?: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (subspaceName) {
|
if (subspaceName) {
|
||||||
|
await this.checkDuplicateNames(
|
||||||
|
subspaceName,
|
||||||
|
subSpaceModel.spaceModel.uuid,
|
||||||
|
subSpaceModel.uuid,
|
||||||
|
);
|
||||||
|
|
||||||
subSpaceModel.subspaceName = subspaceName;
|
subSpaceModel.subspaceName = subspaceName;
|
||||||
await queryRunner.manager.save(subSpaceModel);
|
await queryRunner.manager.save(subSpaceModel);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,11 @@ export class TagModelService {
|
|||||||
async deleteTags(tagUuids: string[], queryRunner: QueryRunner) {
|
async deleteTags(tagUuids: string[], queryRunner: QueryRunner) {
|
||||||
try {
|
try {
|
||||||
const deletePromises = tagUuids.map((id) =>
|
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);
|
await Promise.all(deletePromises);
|
||||||
@ -276,7 +280,7 @@ export class TagModelService {
|
|||||||
|
|
||||||
async getTagByUuid(uuid: string): Promise<TagModel> {
|
async getTagByUuid(uuid: string): Promise<TagModel> {
|
||||||
const tag = await this.tagModelRepository.findOne({
|
const tag = await this.tagModelRepository.findOne({
|
||||||
where: { uuid },
|
where: { uuid, disabled: false },
|
||||||
relations: ['product'],
|
relations: ['product'],
|
||||||
});
|
});
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
|
Reference in New Issue
Block a user