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