mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
fixed issues in create space model
This commit is contained in:
@ -27,7 +27,10 @@ export class SubspaceModelProductAllocationEntity extends AbstractEntity<Subspac
|
||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||
public product: ProductEntity;
|
||||
|
||||
@ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations)
|
||||
@ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations, {
|
||||
cascade: true,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinTable({ name: 'subspace_model_product_tags' })
|
||||
public tags: NewTagEntity[];
|
||||
|
||||
|
@ -28,6 +28,7 @@ export class SpaceModelProductAllocationService {
|
||||
queryRunner?: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
): Promise<SpaceModelProductAllocationEntity[]> {
|
||||
|
||||
try {
|
||||
if (!tags.length) return [];
|
||||
|
||||
@ -37,6 +38,8 @@ export class SpaceModelProductAllocationService {
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
|
||||
|
||||
const productAllocations: SpaceModelProductAllocationEntity[] = [];
|
||||
const existingAllocations = new Map<
|
||||
string,
|
||||
@ -344,26 +347,17 @@ export class SpaceModelProductAllocationService {
|
||||
|
||||
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {
|
||||
try {
|
||||
await queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(SpaceModelProductAllocationEntity, 'allocation')
|
||||
.relation(SpaceModelProductAllocationEntity, 'tags')
|
||||
.of(
|
||||
await queryRunner.manager.find(SpaceModelProductAllocationEntity, {
|
||||
where: { spaceModel: { uuid: spaceModelUuid } },
|
||||
}),
|
||||
)
|
||||
.execute();
|
||||
|
||||
await queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(SpaceModelProductAllocationEntity)
|
||||
.where('spaceModelUuid = :spaceModelUuid', { spaceModelUuid })
|
||||
.where('space_model_uuid = :spaceModelUuid', { spaceModelUuid })
|
||||
.execute();
|
||||
} catch (error) {
|
||||
throw this.handleError(error, `Failed to clear all allocations`);
|
||||
throw this.handleError(
|
||||
error,
|
||||
`Failed to clear all allocations in the space model product allocation`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -593,6 +593,12 @@ export class SpaceModelService {
|
||||
}
|
||||
tagUuidSet.add(tag.uuid);
|
||||
} else {
|
||||
if (!tag.name || !tag.productUuid) {
|
||||
throw new HttpException(
|
||||
`Tag name and product should not be null.`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
const tagKey = `${tag.name}-${tag.productUuid}`;
|
||||
if (tagNameProductSet.has(tagKey)) {
|
||||
throw new HttpException(
|
||||
@ -668,7 +674,7 @@ export class SpaceModelService {
|
||||
uuid: tag.uuid,
|
||||
createdAt: tag.createdAt,
|
||||
updatedAt: tag.updatedAt,
|
||||
tag: tag.tag,
|
||||
name: tag.name,
|
||||
disabled: tag.disabled,
|
||||
product: tag.product
|
||||
? {
|
||||
|
@ -493,16 +493,16 @@ export class SubspaceModelProductAllocationService {
|
||||
await queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from('subspace_model_product_tags') // Replace with entity name if you have one
|
||||
.where(
|
||||
'"subspace_model_product_allocation_uuid" IN (:...subspaceIds)',
|
||||
{
|
||||
subspaceIds,
|
||||
},
|
||||
)
|
||||
.from(SubspaceModelProductAllocationEntity)
|
||||
.where('"subspace_model_uuid" IN (:...subspaceIds)', {
|
||||
subspaceIds,
|
||||
})
|
||||
.execute();
|
||||
} catch (error) {
|
||||
throw this.handleError(error, `Failed to clear all allocations`);
|
||||
throw this.handleError(
|
||||
error,
|
||||
`Failed to clear all allocations subspace model product allocation`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,15 +135,11 @@ export class TagService {
|
||||
|
||||
return tag;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error moving tag with UUID ${tagDto.uuid}: ${error.message}`,
|
||||
);
|
||||
throw error; // Re-throw the error to propagate it to the parent Promise.all
|
||||
throw error;
|
||||
}
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`Error in moveTags: ${error.message}`);
|
||||
throw new HttpException(
|
||||
`Failed to move tags due to an unexpected error: ${error.message}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
|
@ -72,33 +72,43 @@ export class TagService {
|
||||
queryRunner?: QueryRunner,
|
||||
): Promise<NewTagEntity[]> {
|
||||
try {
|
||||
if (!tags || tags.length === 0) return [];
|
||||
if (!tags || tags.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const newTags: CreateTagDto[] = [];
|
||||
const existingTagUuids: string[] = [];
|
||||
|
||||
// Separate new tags and existing tag UUIDs
|
||||
for (const tag of tags) {
|
||||
tag.uuid ? existingTagUuids.push(tag.uuid) : newTags.push(tag);
|
||||
if (tag.uuid) {
|
||||
existingTagUuids.push(tag.uuid);
|
||||
} else {
|
||||
newTags.push(tag);
|
||||
}
|
||||
}
|
||||
|
||||
const existingTags = existingTagUuids.length
|
||||
? await (queryRunner
|
||||
? queryRunner.manager.find(NewTagEntity, {
|
||||
where: {
|
||||
uuid: In(existingTagUuids),
|
||||
project: { uuid: projectUuid },
|
||||
},
|
||||
relations: ['product'],
|
||||
})
|
||||
: this.tagRepository.find({
|
||||
where: {
|
||||
uuid: In(existingTagUuids),
|
||||
project: { uuid: projectUuid },
|
||||
},
|
||||
relations: ['product'],
|
||||
}))
|
||||
: [];
|
||||
let existingTags: NewTagEntity[] = [];
|
||||
|
||||
if (existingTagUuids.length > 0) {
|
||||
existingTags = await (queryRunner
|
||||
? queryRunner.manager.find(NewTagEntity, {
|
||||
where: {
|
||||
uuid: In(existingTagUuids),
|
||||
project: { uuid: projectUuid },
|
||||
},
|
||||
relations: ['product'],
|
||||
})
|
||||
: this.tagRepository.find({
|
||||
where: {
|
||||
uuid: In(existingTagUuids),
|
||||
project: { uuid: projectUuid },
|
||||
},
|
||||
relations: ['product'],
|
||||
}));
|
||||
}
|
||||
|
||||
// Check for missing UUIDs
|
||||
if (existingTags.length !== existingTagUuids.length) {
|
||||
const foundUuids = new Set(existingTags.map((tag) => tag.uuid));
|
||||
const missingUuids = existingTagUuids.filter(
|
||||
@ -111,15 +121,18 @@ export class TagService {
|
||||
);
|
||||
}
|
||||
|
||||
const createdTags =
|
||||
newTags.length > 0
|
||||
? await this.bulkCreateTags(
|
||||
{ projectUuid, tags: newTags },
|
||||
queryRunner,
|
||||
)
|
||||
: [];
|
||||
let createdTags: NewTagEntity[] = [];
|
||||
|
||||
return [...existingTags, ...createdTags];
|
||||
if (newTags.length > 0) {
|
||||
createdTags = await this.bulkCreateTags(
|
||||
{ projectUuid, tags: newTags },
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
const allTags = [...existingTags, ...createdTags];
|
||||
|
||||
return allTags;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error instanceof HttpException
|
||||
@ -138,28 +151,49 @@ export class TagService {
|
||||
): Promise<NewTagEntity[]> {
|
||||
try {
|
||||
const { projectUuid, tags } = dto;
|
||||
const newTags: NewTagEntity[] = [];
|
||||
|
||||
const project = await this.getProjectByUuid(projectUuid);
|
||||
|
||||
// Extract unique product UUIDs
|
||||
const productUuids = Array.from(
|
||||
new Set(tags.map((tag) => tag.productUuid)),
|
||||
);
|
||||
|
||||
const productMap = await this.getProductMap(productUuids);
|
||||
|
||||
const existingTagNames = new Set(
|
||||
await this.getExistingTagNames(projectUuid),
|
||||
);
|
||||
// Fetch existing tag names for this project
|
||||
const existingTags = await this.tagRepository.find({
|
||||
where: { project: { uuid: projectUuid } },
|
||||
relations: ['product'],
|
||||
});
|
||||
|
||||
const newTags: NewTagEntity[] = tags
|
||||
.filter((tag) => !existingTagNames.has(tag.name))
|
||||
.map((tag) =>
|
||||
this.tagRepository.create({
|
||||
name: tag.name,
|
||||
product: productMap.get(tag.productUuid),
|
||||
project,
|
||||
}),
|
||||
);
|
||||
// Convert existing tags into a Map for quick lookup
|
||||
const existingTagMap = new Map<string, string | null>();
|
||||
existingTags.forEach((tag) => {
|
||||
existingTagMap.set(tag.name, tag.product?.uuid || null);
|
||||
});
|
||||
|
||||
for (const tag of tags) {
|
||||
const existingProductUuid = existingTagMap.get(tag.name);
|
||||
|
||||
if (existingProductUuid) {
|
||||
if (existingProductUuid !== tag.productUuid) {
|
||||
throw new HttpException(
|
||||
`Tag "${tag.name}" already exists but is associated with a different product.`,
|
||||
HttpStatus.CONFLICT,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
newTags.push(
|
||||
this.tagRepository.create({
|
||||
name: tag.name,
|
||||
product: productMap.get(tag.productUuid),
|
||||
project,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (newTags.length > 0) {
|
||||
if (queryRunner) {
|
||||
@ -167,12 +201,11 @@ export class TagService {
|
||||
} else {
|
||||
await this.tagRepository.save(newTags);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
return newTags;
|
||||
} catch (error) {
|
||||
console.log('error1', error);
|
||||
|
||||
throw new HttpException(
|
||||
error instanceof HttpException
|
||||
? error.message
|
||||
|
Reference in New Issue
Block a user