mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
fix propagation
This commit is contained in:
@ -0,0 +1,32 @@
|
|||||||
|
import { SubspaceModelProductAllocationEntity } from '@app/common/modules/space-model';
|
||||||
|
import { NewTagEntity } from '@app/common/modules/tag';
|
||||||
|
|
||||||
|
export type IUpdatedAllocations = {
|
||||||
|
allocation?: SubspaceModelProductAllocationEntity;
|
||||||
|
tagsRemoved?: NewTagEntity[];
|
||||||
|
tagsAdded?: NewTagEntity[];
|
||||||
|
newAllocation?: SubspaceModelProductAllocationEntity;
|
||||||
|
deletedAllocation?: SubspaceModelProductAllocationEntity;
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface ISubspaceProductAllocationUpdateResult {
|
||||||
|
createdAllocations: ICreatedAllocation[];
|
||||||
|
updatedAllocations: IUpdatedAllocation[];
|
||||||
|
deletedAllocations: IDeletedAllocation[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICreatedAllocation {
|
||||||
|
allocation: SubspaceModelProductAllocationEntity;
|
||||||
|
tags: NewTagEntity[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IUpdatedAllocation {
|
||||||
|
allocation: SubspaceModelProductAllocationEntity;
|
||||||
|
tagsAdded: NewTagEntity[];
|
||||||
|
tagsRemoved: NewTagEntity[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IDeletedAllocation {
|
||||||
|
allocation: SubspaceModelProductAllocationEntity;
|
||||||
|
tagsRemoved: NewTagEntity[];
|
||||||
|
}
|
||||||
@ -44,6 +44,8 @@ import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities
|
|||||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||||
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
|
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||||
|
import { ISingleSubspace } from 'src/space/interfaces/single-subspace.interface';
|
||||||
|
import { ISingleSubspaceModel } from '../interfaces';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceModelService {
|
export class SpaceModelService {
|
||||||
@ -195,7 +197,7 @@ export class SpaceModelService {
|
|||||||
param.projectUuid,
|
param.projectUuid,
|
||||||
);
|
);
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
|
let modifiedSubspaces: ISingleSubspaceModel[] = [];
|
||||||
try {
|
try {
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
const spaces = await this.fetchModelSpaces(spaceModel);
|
const spaces = await this.fetchModelSpaces(spaceModel);
|
||||||
@ -215,13 +217,14 @@ export class SpaceModelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (dto.subspaceModels) {
|
if (dto.subspaceModels) {
|
||||||
await this.subSpaceModelService.modifySubspaceModels(
|
modifiedSubspaces =
|
||||||
dto.subspaceModels,
|
await this.subSpaceModelService.modifySubspaceModels(
|
||||||
spaceModel,
|
dto.subspaceModels,
|
||||||
queryRunner,
|
spaceModel,
|
||||||
param.projectUuid,
|
queryRunner,
|
||||||
dto.tags,
|
param.projectUuid,
|
||||||
);
|
dto.tags,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dto.tags) {
|
if (dto.tags) {
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import { NewTagEntity } from '@app/common/modules/tag';
|
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { ModifyTagModelDto } from 'src/space-model/dtos';
|
import { ModifyTagModelDto } from 'src/space-model/dtos';
|
||||||
import { ISingleSubspaceModel } from 'src/space-model/interfaces';
|
import { ISingleSubspaceModel } from 'src/space-model/interfaces';
|
||||||
|
import { IUpdatedAllocations } from 'src/space-model/interfaces/subspace-product-allocation-update-result.interface';
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
import { ProcessTagDto } from 'src/tags/dtos';
|
||||||
import { TagService as NewTagService } from 'src/tags/services';
|
import { TagService as NewTagService } from 'src/tags/services';
|
||||||
import { In, QueryRunner } from 'typeorm';
|
import { In, QueryRunner } from 'typeorm';
|
||||||
@ -29,8 +30,10 @@ export class SubspaceModelProductAllocationService {
|
|||||||
tags: NewTagEntity[],
|
tags: NewTagEntity[],
|
||||||
queryRunner?: QueryRunner,
|
queryRunner?: QueryRunner,
|
||||||
spaceAllocationsToExclude?: SpaceModelProductAllocationEntity[],
|
spaceAllocationsToExclude?: SpaceModelProductAllocationEntity[],
|
||||||
): Promise<SubspaceModelProductAllocationEntity[]> {
|
): Promise<IUpdatedAllocations[]> {
|
||||||
try {
|
try {
|
||||||
|
const updatedAllocations: IUpdatedAllocations[] = [];
|
||||||
|
|
||||||
const allocations: SubspaceModelProductAllocationEntity[] = [];
|
const allocations: SubspaceModelProductAllocationEntity[] = [];
|
||||||
|
|
||||||
for (const tag of tags) {
|
for (const tag of tags) {
|
||||||
@ -140,7 +143,6 @@ export class SubspaceModelProductAllocationService {
|
|||||||
product: tag.product,
|
product: tag.product,
|
||||||
tags: [tag],
|
tags: [tag],
|
||||||
});
|
});
|
||||||
|
|
||||||
allocations.push(allocation);
|
allocations.push(allocation);
|
||||||
} else {
|
} else {
|
||||||
//If allocation exists, add the tag to it
|
//If allocation exists, add the tag to it
|
||||||
@ -155,6 +157,10 @@ export class SubspaceModelProductAllocationService {
|
|||||||
existingAllocationsForProduct[0],
|
existingAllocationsForProduct[0],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
updatedAllocations.push({
|
||||||
|
allocation: existingAllocationsForProduct[0],
|
||||||
|
tagsAdded: [tag],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,9 +174,13 @@ export class SubspaceModelProductAllocationService {
|
|||||||
} else {
|
} else {
|
||||||
await this.subspaceModelProductAllocationRepository.save(allocations);
|
await this.subspaceModelProductAllocationRepository.save(allocations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allocations.forEach((allocation) => {
|
||||||
|
updatedAllocations.push({ newAllocation: allocation });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return allocations;
|
return updatedAllocations;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`An unexpected error occurred while creating subspace product allocations ${error}`,
|
`An unexpected error occurred while creating subspace product allocations ${error}`,
|
||||||
@ -267,6 +277,7 @@ export class SubspaceModelProductAllocationService {
|
|||||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||||
) {
|
) {
|
||||||
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
|
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
|
||||||
|
const updatedAllocations: IUpdatedAllocations[] = [];
|
||||||
|
|
||||||
for (const subspaceModel of subspaceModels) {
|
for (const subspaceModel of subspaceModels) {
|
||||||
const tagDtos = subspaceModel.tags;
|
const tagDtos = subspaceModel.tags;
|
||||||
@ -332,6 +343,11 @@ export class SubspaceModelProductAllocationService {
|
|||||||
(tag) => tag.uuid !== deletedTag.tagUuid,
|
(tag) => tag.uuid !== deletedTag.tagUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updatedAllocations.push({
|
||||||
|
allocation,
|
||||||
|
tagsRemoved: [tagEntity],
|
||||||
|
});
|
||||||
|
|
||||||
await queryRunner.manager.save(allocation);
|
await queryRunner.manager.save(allocation);
|
||||||
|
|
||||||
const productAllocationExistInSubspace =
|
const productAllocationExistInSubspace =
|
||||||
@ -350,6 +366,12 @@ export class SubspaceModelProductAllocationService {
|
|||||||
|
|
||||||
if (productAllocationExistInSubspace) {
|
if (productAllocationExistInSubspace) {
|
||||||
productAllocationExistInSubspace.tags.push(tagEntity);
|
productAllocationExistInSubspace.tags.push(tagEntity);
|
||||||
|
|
||||||
|
updatedAllocations.push({
|
||||||
|
allocation: productAllocationExistInSubspace,
|
||||||
|
tagsAdded: [tagEntity],
|
||||||
|
});
|
||||||
|
|
||||||
await queryRunner.manager.save(
|
await queryRunner.manager.save(
|
||||||
productAllocationExistInSubspace,
|
productAllocationExistInSubspace,
|
||||||
);
|
);
|
||||||
@ -363,6 +385,10 @@ export class SubspaceModelProductAllocationService {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updatedAllocations.push({
|
||||||
|
allocation: newProductAllocation,
|
||||||
|
});
|
||||||
|
|
||||||
await queryRunner.manager.save(newProductAllocation);
|
await queryRunner.manager.save(newProductAllocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,6 +432,11 @@ export class SubspaceModelProductAllocationService {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updatedAllocations.push({
|
||||||
|
allocation: allocation,
|
||||||
|
tagsRemoved: repeatedTags,
|
||||||
|
});
|
||||||
|
|
||||||
await queryRunner.manager.save(allocation);
|
await queryRunner.manager.save(allocation);
|
||||||
|
|
||||||
const productAllocationExistInSubspace =
|
const productAllocationExistInSubspace =
|
||||||
@ -421,6 +452,11 @@ export class SubspaceModelProductAllocationService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (productAllocationExistInSubspace) {
|
if (productAllocationExistInSubspace) {
|
||||||
|
updatedAllocations.push({
|
||||||
|
allocation: productAllocationExistInSubspace,
|
||||||
|
tagsAdded: repeatedTags,
|
||||||
|
});
|
||||||
|
|
||||||
productAllocationExistInSubspace.tags.push(...repeatedTags);
|
productAllocationExistInSubspace.tags.push(...repeatedTags);
|
||||||
await queryRunner.manager.save(
|
await queryRunner.manager.save(
|
||||||
productAllocationExistInSubspace,
|
productAllocationExistInSubspace,
|
||||||
@ -435,19 +471,12 @@ export class SubspaceModelProductAllocationService {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updatedAllocations.push({
|
||||||
|
newAllocation: newProductAllocation,
|
||||||
|
});
|
||||||
|
|
||||||
await queryRunner.manager.save(newProductAllocation);
|
await queryRunner.manager.save(newProductAllocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newAllocation = queryRunner.manager.create(
|
|
||||||
SubspaceModelProductAllocationEntity,
|
|
||||||
{
|
|
||||||
subspaceModel: subspaceModel.subspaceModel,
|
|
||||||
product: allocation.product,
|
|
||||||
tags: repeatedTags,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await queryRunner.manager.save(newAllocation);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -479,13 +508,14 @@ export class SubspaceModelProductAllocationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create new product allocations
|
// Create new product allocations
|
||||||
await this.createProductAllocations(
|
const newAllocations = await this.createProductAllocations(
|
||||||
subspaceModel.subspaceModel,
|
subspaceModel.subspaceModel,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
processedTags,
|
processedTags,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
spaceAllocationToExclude,
|
spaceAllocationToExclude,
|
||||||
);
|
);
|
||||||
|
return [...updatedAllocations, ...newAllocations];
|
||||||
}
|
}
|
||||||
if (tagsToDeleteDto.length > 0) {
|
if (tagsToDeleteDto.length > 0) {
|
||||||
await this.processDeleteActions(tagsToDeleteDto, queryRunner);
|
await this.processDeleteActions(tagsToDeleteDto, queryRunner);
|
||||||
|
|||||||
@ -115,7 +115,7 @@ export class SubSpaceModelService {
|
|||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||||
spaces?: SpaceEntity[],
|
spaces?: SpaceEntity[],
|
||||||
) {
|
): Promise<ISingleSubspaceModel[]> {
|
||||||
try {
|
try {
|
||||||
if (!dtos || dtos.length === 0) {
|
if (!dtos || dtos.length === 0) {
|
||||||
return;
|
return;
|
||||||
@ -129,18 +129,18 @@ export class SubSpaceModelService {
|
|||||||
(dto) => dto.action === ModifyAction.DELETE,
|
(dto) => dto.action === ModifyAction.DELETE,
|
||||||
);
|
);
|
||||||
|
|
||||||
const updatedModels = await this.updateSubspaceModel(
|
const updatedSubspaces = await this.updateSubspaceModel(
|
||||||
combinedDtos,
|
combinedDtos,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
const addedModels = await this.handleAddAction(
|
const createdSubspaces = await this.handleAddAction(
|
||||||
addDtos,
|
addDtos,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
spaces,
|
spaces,
|
||||||
);
|
);
|
||||||
const combineModels = [...addedModels, ...updatedModels];
|
const combineModels = [...createdSubspaces, ...updatedSubspaces];
|
||||||
|
|
||||||
await this.productAllocationService.updateAllocations(
|
await this.productAllocationService.updateAllocations(
|
||||||
combineModels,
|
combineModels,
|
||||||
@ -150,7 +150,12 @@ export class SubSpaceModelService {
|
|||||||
spaceTagUpdateDtos,
|
spaceTagUpdateDtos,
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.deleteSubspaceModels(deleteDtos, queryRunner, spaceModel);
|
const deletedSubspaces = await this.deleteSubspaceModels(
|
||||||
|
deleteDtos,
|
||||||
|
queryRunner,
|
||||||
|
spaceModel,
|
||||||
|
);
|
||||||
|
return [...createdSubspaces, ...updatedSubspaces, ...deletedSubspaces];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in modifySubspaceModels:', error);
|
console.error('Error in modifySubspaceModels:', error);
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -168,7 +173,7 @@ export class SubSpaceModelService {
|
|||||||
deleteDtos: ModifySubspaceModelDto[],
|
deleteDtos: ModifySubspaceModelDto[],
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
) {
|
): Promise<ISingleSubspaceModel[]> {
|
||||||
try {
|
try {
|
||||||
if (!deleteDtos || deleteDtos.length === 0) {
|
if (!deleteDtos || deleteDtos.length === 0) {
|
||||||
return;
|
return;
|
||||||
@ -275,7 +280,11 @@ export class SubSpaceModelService {
|
|||||||
|
|
||||||
deleteResults.push(...subspaceUuids.map((uuid) => ({ uuid })));
|
deleteResults.push(...subspaceUuids.map((uuid) => ({ uuid })));
|
||||||
|
|
||||||
return deleteResults;
|
return subspaces.map((subspace) => ({
|
||||||
|
subspaceModel: subspace,
|
||||||
|
action: ModifyAction.DELETE,
|
||||||
|
tags: [],
|
||||||
|
}));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof QueryFailedError) {
|
if (error instanceof QueryFailedError) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
|||||||
@ -38,7 +38,6 @@ import { ProcessTagDto } from 'src/tags/dtos';
|
|||||||
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
||||||
import { SubspaceProductAllocationService } from './subspace/subspace-product-allocation.service';
|
import { SubspaceProductAllocationService } from './subspace/subspace-product-allocation.service';
|
||||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { DeviceService } from 'src/device/services';
|
import { DeviceService } from 'src/device/services';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|||||||
Reference in New Issue
Block a user