mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
propagate
This commit is contained in:
@ -1,6 +1,9 @@
|
|||||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||||
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
||||||
import { SpaceRepository } from '@app/common/modules/space';
|
import {
|
||||||
|
SpaceProductAllocationRepository,
|
||||||
|
SpaceRepository,
|
||||||
|
} from '@app/common/modules/space';
|
||||||
import {
|
import {
|
||||||
SubspaceProductAllocationRepository,
|
SubspaceProductAllocationRepository,
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
@ -32,6 +35,7 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
private readonly tagService: TagService,
|
private readonly tagService: TagService,
|
||||||
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
|
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
|
||||||
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
|
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
|
||||||
|
private readonly spaceProductRepository: SpaceProductAllocationRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
||||||
@ -43,6 +47,8 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
for (const subspaceModel of subspaceModels) {
|
for (const subspaceModel of subspaceModels) {
|
||||||
if (subspaceModel.action === ModifyAction.ADD) {
|
if (subspaceModel.action === ModifyAction.ADD) {
|
||||||
await this.addSubspaceModel(subspaceModel, spaces);
|
await this.addSubspaceModel(subspaceModel, spaces);
|
||||||
|
} else if (subspaceModel.action === ModifyAction.DELETE) {
|
||||||
|
await this.deleteSubspaceModel(subspaceModel, spaces);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,6 +90,42 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteSubspaceModel(
|
||||||
|
subspaceModel: ISingleSubspaceModel,
|
||||||
|
spaces: SpaceEntity[],
|
||||||
|
) {
|
||||||
|
const subspaces = await this.subspaceRepository.find({
|
||||||
|
where: {
|
||||||
|
subSpaceModel: {
|
||||||
|
uuid: subspaceModel.subspaceModel.uuid,
|
||||||
|
},
|
||||||
|
disabled: false,
|
||||||
|
},
|
||||||
|
relations: [
|
||||||
|
'productAllocations',
|
||||||
|
'productAllocations.product',
|
||||||
|
'productAllocations.tags',
|
||||||
|
'space',
|
||||||
|
],
|
||||||
|
});
|
||||||
|
if (!subspaces || subspaces.length === 0) return;
|
||||||
|
|
||||||
|
for (const subspace of subspaces) {
|
||||||
|
const allocationsToRemove = subspace.productAllocations;
|
||||||
|
if (allocationsToRemove.length > 0) {
|
||||||
|
for (const allocation of allocationsToRemove) {
|
||||||
|
await this.subspaceProductRepository.delete({
|
||||||
|
uuid: allocation.uuid,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await this.subspaceRepository.update(
|
||||||
|
{ uuid: subspace.uuid },
|
||||||
|
{ disabled: true },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async updateSubspaceModels(
|
async updateSubspaceModels(
|
||||||
subspaceModels: UpdatedSubspaceModelPayload[],
|
subspaceModels: UpdatedSubspaceModelPayload[],
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
|
|||||||
@ -1,9 +1,18 @@
|
|||||||
import { SubspaceModelEntity } from '@app/common/modules/space-model';
|
import {
|
||||||
|
SpaceModelProductAllocationEntity,
|
||||||
|
SubspaceModelEntity,
|
||||||
|
} from '@app/common/modules/space-model';
|
||||||
import { ModifyTagModelDto } from '../dtos';
|
import { ModifyTagModelDto } from '../dtos';
|
||||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||||
|
import { NewTagEntity } from '@app/common/modules/tag';
|
||||||
|
|
||||||
export interface ISingleSubspaceModel {
|
export interface ISingleSubspaceModel {
|
||||||
subspaceModel: SubspaceModelEntity;
|
subspaceModel: SubspaceModelEntity;
|
||||||
action: ModifyAction;
|
action: ModifyAction;
|
||||||
tags: ModifyTagModelDto[];
|
tags: ModifyTagModelDto[];
|
||||||
|
movedToNewSpaceAllocation?: SpaceModelProductAllocationEntity[];
|
||||||
|
movedToAlreadyExistingSpaceAllocation?: {
|
||||||
|
allocation: SpaceModelProductAllocationEntity;
|
||||||
|
tags: NewTagEntity[];
|
||||||
|
}[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user