mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 07:54:53 +00:00
added validation for subspaces
This commit is contained in:
@ -530,7 +530,7 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'An error occurred while updating the space',
|
`An error occurred while updating the space: error ${error}`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@ -79,134 +79,62 @@ export class SubspaceProductAllocationService {
|
|||||||
spaceTagUpdateDtos?: ModifyTagDto[],
|
spaceTagUpdateDtos?: ModifyTagDto[],
|
||||||
) {
|
) {
|
||||||
const spaceAllocationToExclude: SpaceProductAllocationEntity[] = [];
|
const spaceAllocationToExclude: SpaceProductAllocationEntity[] = [];
|
||||||
|
|
||||||
for (const subspace of subspaces) {
|
for (const subspace of subspaces) {
|
||||||
|
if (!subspace.tags || subspace.tags.length === 0) continue;
|
||||||
const tagDtos = subspace.tags;
|
const tagDtos = subspace.tags;
|
||||||
if (tagDtos.length > 0) {
|
const tagsToAddDto: ProcessTagDto[] = tagDtos
|
||||||
const tagsToAddDto: ProcessTagDto[] = tagDtos
|
.filter((dto) => dto.action === ModifyAction.ADD)
|
||||||
.filter((dto) => dto.action === ModifyAction.ADD)
|
.map((dto) => ({
|
||||||
.map((dto) => ({
|
name: dto.name,
|
||||||
name: dto.name,
|
productUuid: dto.productUuid,
|
||||||
productUuid: dto.productUuid,
|
uuid: dto.newTagUuid,
|
||||||
uuid: dto.newTagUuid,
|
}));
|
||||||
}));
|
|
||||||
|
|
||||||
const tagsToDeleteDto = tagDtos.filter(
|
const tagsToDeleteDto = tagDtos.filter(
|
||||||
(dto) => dto.action === ModifyAction.DELETE,
|
(dto) => dto.action === ModifyAction.DELETE,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (tagsToAddDto.length > 0) {
|
||||||
|
let processedTags = await this.tagService.processTags(
|
||||||
|
tagsToAddDto,
|
||||||
|
projectUuid,
|
||||||
|
queryRunner,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (tagsToAddDto.length > 0) {
|
for (const subspaceDto of subspaces) {
|
||||||
let processedTags = await this.tagService.processTags(
|
if (
|
||||||
tagsToAddDto,
|
subspaceDto !== subspace &&
|
||||||
projectUuid,
|
subspaceDto.action === ModifyAction.UPDATE &&
|
||||||
queryRunner,
|
subspaceDto.tags
|
||||||
);
|
) {
|
||||||
|
const deletedTags = subspaceDto.tags.filter(
|
||||||
|
(tagDto) =>
|
||||||
|
tagDto.action === ModifyAction.DELETE &&
|
||||||
|
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
|
||||||
|
);
|
||||||
|
|
||||||
for (const subspaceDto of subspaces) {
|
for (const deletedTag of deletedTags) {
|
||||||
if (
|
|
||||||
subspaceDto !== subspace &&
|
|
||||||
subspaceDto.action === ModifyAction.UPDATE &&
|
|
||||||
subspaceDto.tags
|
|
||||||
) {
|
|
||||||
const deletedTags = subspaceDto.tags.filter(
|
|
||||||
(tagDto) =>
|
|
||||||
tagDto.action === ModifyAction.DELETE &&
|
|
||||||
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const deletedTag of deletedTags) {
|
|
||||||
const allocation = await queryRunner.manager.findOne(
|
|
||||||
SubspaceProductAllocationEntity,
|
|
||||||
{
|
|
||||||
where: {
|
|
||||||
subspace: { uuid: subspaceDto.subspace.uuid },
|
|
||||||
},
|
|
||||||
relations: ['tags', 'product', 'subspace'],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const isCommonTag = allocation.tags.some(
|
|
||||||
(tag) => tag.uuid === deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (allocation && isCommonTag) {
|
|
||||||
const tagEntity = allocation.tags.find(
|
|
||||||
(tag) => tag.uuid === deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
allocation.tags = allocation.tags.filter(
|
|
||||||
(tag) => tag.uuid !== deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
await queryRunner.manager.save(allocation);
|
|
||||||
|
|
||||||
const productAllocationExistInSubspace =
|
|
||||||
await queryRunner.manager.findOne(
|
|
||||||
SubspaceProductAllocationEntity,
|
|
||||||
{
|
|
||||||
where: {
|
|
||||||
subspace: {
|
|
||||||
uuid: subspaceDto.subspace.uuid,
|
|
||||||
},
|
|
||||||
product: { uuid: allocation.product.uuid },
|
|
||||||
},
|
|
||||||
relations: ['tags'],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (productAllocationExistInSubspace) {
|
|
||||||
productAllocationExistInSubspace.tags.push(tagEntity);
|
|
||||||
await queryRunner.manager.save(
|
|
||||||
productAllocationExistInSubspace,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
const newProductAllocation = queryRunner.manager.create(
|
|
||||||
SubspaceProductAllocationEntity,
|
|
||||||
{
|
|
||||||
subspace: subspace.subspace,
|
|
||||||
product: allocation.product,
|
|
||||||
tags: [tagEntity],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await queryRunner.manager.save(newProductAllocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
processedTags = processedTags.filter(
|
|
||||||
(tag) => tag.uuid !== deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
subspaceDto.tags = subspaceDto.tags.filter(
|
|
||||||
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
subspaceDto !== subspace &&
|
|
||||||
subspaceDto.action === ModifyAction.DELETE
|
|
||||||
) {
|
|
||||||
const allocation = await queryRunner.manager.findOne(
|
const allocation = await queryRunner.manager.findOne(
|
||||||
SubspaceProductAllocationEntity,
|
SubspaceProductAllocationEntity,
|
||||||
{
|
{
|
||||||
where: {
|
where: {
|
||||||
subspace: { uuid: subspaceDto.subspace.uuid },
|
subspace: { uuid: subspaceDto.subspace.uuid },
|
||||||
},
|
},
|
||||||
relations: ['tags'],
|
relations: ['tags', 'product', 'subspace'],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const repeatedTags = allocation?.tags.filter((tag) =>
|
const isCommonTag = allocation.tags.some(
|
||||||
processedTags.some(
|
(tag) => tag.uuid === deletedTag.tagUuid,
|
||||||
(processedTag) => processedTag.uuid === tag.uuid,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
if (repeatedTags.length > 0) {
|
|
||||||
|
if (allocation && isCommonTag) {
|
||||||
|
const tagEntity = allocation.tags.find(
|
||||||
|
(tag) => tag.uuid === deletedTag.tagUuid,
|
||||||
|
);
|
||||||
|
|
||||||
allocation.tags = allocation.tags.filter(
|
allocation.tags = allocation.tags.filter(
|
||||||
(tag) =>
|
(tag) => tag.uuid !== deletedTag.tagUuid,
|
||||||
!repeatedTags.some(
|
|
||||||
(repeatedTag) => repeatedTag.uuid === tag.uuid,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await queryRunner.manager.save(allocation);
|
await queryRunner.manager.save(allocation);
|
||||||
@ -216,7 +144,9 @@ export class SubspaceProductAllocationService {
|
|||||||
SubspaceProductAllocationEntity,
|
SubspaceProductAllocationEntity,
|
||||||
{
|
{
|
||||||
where: {
|
where: {
|
||||||
subspace: { uuid: subspaceDto.subspace.uuid },
|
subspace: {
|
||||||
|
uuid: subspaceDto.subspace.uuid,
|
||||||
|
},
|
||||||
product: { uuid: allocation.product.uuid },
|
product: { uuid: allocation.product.uuid },
|
||||||
},
|
},
|
||||||
relations: ['tags'],
|
relations: ['tags'],
|
||||||
@ -224,7 +154,7 @@ export class SubspaceProductAllocationService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (productAllocationExistInSubspace) {
|
if (productAllocationExistInSubspace) {
|
||||||
productAllocationExistInSubspace.tags.push(...repeatedTags);
|
productAllocationExistInSubspace.tags.push(tagEntity);
|
||||||
await queryRunner.manager.save(
|
await queryRunner.manager.save(
|
||||||
productAllocationExistInSubspace,
|
productAllocationExistInSubspace,
|
||||||
);
|
);
|
||||||
@ -234,14 +164,71 @@ export class SubspaceProductAllocationService {
|
|||||||
{
|
{
|
||||||
subspace: subspace.subspace,
|
subspace: subspace.subspace,
|
||||||
product: allocation.product,
|
product: allocation.product,
|
||||||
tags: repeatedTags,
|
tags: [tagEntity],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await queryRunner.manager.save(newProductAllocation);
|
await queryRunner.manager.save(newProductAllocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newAllocation = queryRunner.manager.create(
|
processedTags = processedTags.filter(
|
||||||
|
(tag) => tag.uuid !== deletedTag.tagUuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
subspaceDto.tags = subspaceDto.tags.filter(
|
||||||
|
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
subspaceDto !== subspace &&
|
||||||
|
subspaceDto.action === ModifyAction.DELETE
|
||||||
|
) {
|
||||||
|
const allocation = await queryRunner.manager.findOne(
|
||||||
|
SubspaceProductAllocationEntity,
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
subspace: { uuid: subspaceDto.subspace.uuid },
|
||||||
|
},
|
||||||
|
relations: ['tags'],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const repeatedTags = allocation?.tags.filter((tag) =>
|
||||||
|
processedTags.some(
|
||||||
|
(processedTag) => processedTag.uuid === tag.uuid,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (repeatedTags.length > 0) {
|
||||||
|
allocation.tags = allocation.tags.filter(
|
||||||
|
(tag) =>
|
||||||
|
!repeatedTags.some(
|
||||||
|
(repeatedTag) => repeatedTag.uuid === tag.uuid,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.manager.save(allocation);
|
||||||
|
|
||||||
|
const productAllocationExistInSubspace =
|
||||||
|
await queryRunner.manager.findOne(
|
||||||
|
SubspaceProductAllocationEntity,
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
subspace: { uuid: subspaceDto.subspace.uuid },
|
||||||
|
product: { uuid: allocation.product.uuid },
|
||||||
|
},
|
||||||
|
relations: ['tags'],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (productAllocationExistInSubspace) {
|
||||||
|
productAllocationExistInSubspace.tags.push(...repeatedTags);
|
||||||
|
await queryRunner.manager.save(
|
||||||
|
productAllocationExistInSubspace,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const newProductAllocation = queryRunner.manager.create(
|
||||||
SubspaceProductAllocationEntity,
|
SubspaceProductAllocationEntity,
|
||||||
{
|
{
|
||||||
subspace: subspace.subspace,
|
subspace: subspace.subspace,
|
||||||
@ -250,47 +237,58 @@ export class SubspaceProductAllocationService {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await queryRunner.manager.save(newAllocation);
|
await queryRunner.manager.save(newProductAllocation);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
const newAllocation = queryRunner.manager.create(
|
||||||
if (spaceTagUpdateDtos) {
|
SubspaceProductAllocationEntity,
|
||||||
const deletedSpaceTags = spaceTagUpdateDtos.filter(
|
|
||||||
(tagDto) =>
|
|
||||||
tagDto.action === ModifyAction.DELETE &&
|
|
||||||
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
|
|
||||||
);
|
|
||||||
for (const deletedTag of deletedSpaceTags) {
|
|
||||||
const allocation = await queryRunner.manager.findOne(
|
|
||||||
SpaceProductAllocationEntity,
|
|
||||||
{
|
{
|
||||||
where: {
|
subspace: subspace.subspace,
|
||||||
space: { uuid: space.uuid },
|
product: allocation.product,
|
||||||
tags: { uuid: deletedTag.tagUuid },
|
tags: repeatedTags,
|
||||||
},
|
|
||||||
relations: ['tags', 'subspace'],
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
await queryRunner.manager.save(newAllocation);
|
||||||
allocation &&
|
|
||||||
allocation.tags.some((tag) => tag.uuid === deletedTag.tagUuid)
|
|
||||||
) {
|
|
||||||
spaceAllocationToExclude.push(allocation);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
await this.createSubspaceProductAllocations(
|
if (spaceTagUpdateDtos) {
|
||||||
subspace.subspace,
|
const deletedSpaceTags = spaceTagUpdateDtos.filter(
|
||||||
processedTags,
|
(tagDto) =>
|
||||||
queryRunner,
|
tagDto.action === ModifyAction.DELETE &&
|
||||||
spaceAllocationToExclude,
|
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
|
||||||
);
|
);
|
||||||
|
for (const deletedTag of deletedSpaceTags) {
|
||||||
|
const allocation = await queryRunner.manager.findOne(
|
||||||
|
SpaceProductAllocationEntity,
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
space: { uuid: space.uuid },
|
||||||
|
tags: { uuid: deletedTag.tagUuid },
|
||||||
|
},
|
||||||
|
relations: ['tags', 'subspace'],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
allocation &&
|
||||||
|
allocation.tags.some((tag) => tag.uuid === deletedTag.tagUuid)
|
||||||
|
) {
|
||||||
|
spaceAllocationToExclude.push(allocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (tagsToDeleteDto.length > 0) {
|
|
||||||
await this.processDeleteActions(tagsToDeleteDto, queryRunner);
|
await this.createSubspaceProductAllocations(
|
||||||
}
|
subspace.subspace,
|
||||||
|
processedTags,
|
||||||
|
queryRunner,
|
||||||
|
spaceAllocationToExclude,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (tagsToDeleteDto.length > 0) {
|
||||||
|
await this.processDeleteActions(tagsToDeleteDto, queryRunner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -324,6 +324,9 @@ export class SubSpaceService {
|
|||||||
projectUuid?: string,
|
projectUuid?: string,
|
||||||
spaceTagUpdateDtos?: ModifyTagDto[],
|
spaceTagUpdateDtos?: ModifyTagDto[],
|
||||||
) {
|
) {
|
||||||
|
if (!subspaceDtos || subspaceDtos.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const addedSubspaces = [];
|
const addedSubspaces = [];
|
||||||
const updatedSubspaces = [];
|
const updatedSubspaces = [];
|
||||||
|
|
||||||
@ -335,14 +338,17 @@ export class SubSpaceService {
|
|||||||
space,
|
space,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
addedSubspaces.push(addedSubspace);
|
if (addedSubspace) addedSubspaces.push(addedSubspace);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case ModifyAction.UPDATE:
|
case ModifyAction.UPDATE:
|
||||||
const updatedSubspace = await this.handleUpdateAction(
|
const updatedSubspace = await this.handleUpdateAction(
|
||||||
subspace,
|
subspace,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
updatedSubspaces.push(updatedSubspace);
|
if (updatedSubspace) {
|
||||||
|
updatedSubspaces.push(updatedSubspace);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ModifyAction.DELETE:
|
case ModifyAction.DELETE:
|
||||||
await this.handleDeleteAction(subspace, queryRunner);
|
await this.handleDeleteAction(subspace, queryRunner);
|
||||||
@ -355,7 +361,9 @@ export class SubSpaceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces];
|
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
|
||||||
|
(subspace) => subspace !== undefined,
|
||||||
|
);
|
||||||
|
|
||||||
if (combinedSubspaces.length > 0) {
|
if (combinedSubspaces.length > 0) {
|
||||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
|
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
|
||||||
@ -436,32 +444,22 @@ export class SubSpaceService {
|
|||||||
private async handleUpdateAction(
|
private async handleUpdateAction(
|
||||||
modifyDto: ModifySubspaceDto,
|
modifyDto: ModifySubspaceDto,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
): Promise<void> {
|
): Promise<SubspaceEntity> {
|
||||||
const subspace = await this.findOne(modifyDto.uuid);
|
const subspace = await this.findOne(modifyDto.uuid);
|
||||||
await this.update(
|
const updatedSubspace = await this.update(
|
||||||
queryRunner,
|
queryRunner,
|
||||||
subspace,
|
subspace,
|
||||||
modifyDto.subspaceName,
|
modifyDto.subspaceName,
|
||||||
modifyDto.tags,
|
|
||||||
);
|
);
|
||||||
|
return updatedSubspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
subspace: SubspaceEntity,
|
subspace: SubspaceEntity,
|
||||||
subspaceName?: string,
|
subspaceName?: string,
|
||||||
modifyTagDto?: ModifyTagDto[],
|
|
||||||
) {
|
) {
|
||||||
await this.updateSubspaceName(queryRunner, subspace, subspaceName);
|
return await this.updateSubspaceName(queryRunner, subspace, subspaceName);
|
||||||
|
|
||||||
if (modifyTagDto?.length) {
|
|
||||||
await this.tagService.modifyTags(
|
|
||||||
modifyTagDto,
|
|
||||||
queryRunner,
|
|
||||||
null,
|
|
||||||
subspace,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleDeleteAction(
|
async handleDeleteAction(
|
||||||
@ -517,11 +515,12 @@ export class SubSpaceService {
|
|||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
subSpace: SubspaceEntity,
|
subSpace: SubspaceEntity,
|
||||||
subspaceName?: string,
|
subspaceName?: string,
|
||||||
): Promise<void> {
|
): Promise<SubspaceEntity> {
|
||||||
if (subspaceName) {
|
if (subspaceName) {
|
||||||
subSpace.subspaceName = subspaceName;
|
subSpace.subspaceName = subspaceName;
|
||||||
await queryRunner.manager.save(subSpace);
|
return await queryRunner.manager.save(subSpace);
|
||||||
}
|
}
|
||||||
|
return subSpace;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async checkForDuplicateNames(names: string[]): Promise<void> {
|
private async checkForDuplicateNames(names: string[]): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user