mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:04:53 +00:00
removed old tags
This commit is contained in:
@ -1,2 +1,2 @@
|
|||||||
export * from './propogate-subspace-update-command';
|
|
||||||
export * from './propagate-space-model-deletion.command';
|
export * from './propagate-space-model-deletion.command';
|
||||||
|
export * from './propagate-subspace-model-update-command';
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
import { ICommand } from '@nestjs/cqrs';
|
import { ICommand } from '@nestjs/cqrs';
|
||||||
import { SpaceModelEntity } from '@app/common/modules/space-model';
|
import { SpaceModelEntity } from '@app/common/modules/space-model';
|
||||||
import { ModifyspaceModelPayload } from '../interfaces';
|
import { ISingleSubspaceModel } from '../interfaces';
|
||||||
import { QueryRunner } from 'typeorm';
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
|
|
||||||
export class PropogateUpdateSpaceModelCommand implements ICommand {
|
export class PropogateUpdateSpaceModelCommand implements ICommand {
|
||||||
constructor(
|
constructor(
|
||||||
public readonly param: {
|
public readonly param: {
|
||||||
spaceModel: SpaceModelEntity;
|
spaceModel: SpaceModelEntity;
|
||||||
modifiedSpaceModels: ModifyspaceModelPayload;
|
subspaceModels: ISingleSubspaceModel[];
|
||||||
queryRunner: QueryRunner;
|
spaces: SpaceEntity[];
|
||||||
},
|
},
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
@ -2,18 +2,16 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
|||||||
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
||||||
import { SpaceRepository } from '@app/common/modules/space';
|
import { SpaceRepository } from '@app/common/modules/space';
|
||||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||||
import {
|
import { SpaceModelEntity, TagModel } from '@app/common/modules/space-model';
|
||||||
SpaceModelEntity,
|
|
||||||
SubspaceModelEntity,
|
|
||||||
TagModel,
|
|
||||||
} from '@app/common/modules/space-model';
|
|
||||||
import { DataSource, QueryRunner } from 'typeorm';
|
import { DataSource, QueryRunner } from 'typeorm';
|
||||||
import { SubSpaceService } from 'src/space/services';
|
import { SubSpaceService } from 'src/space/services';
|
||||||
import { TagService } from 'src/space/services/tag';
|
import { TagService } from 'src/space/services/tag';
|
||||||
import { UpdatedSubspaceModelPayload } from '../interfaces';
|
import {
|
||||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
ISingleSubspaceModel,
|
||||||
import { ModifySubspaceDto } from 'src/space/dtos';
|
UpdatedSubspaceModelPayload,
|
||||||
|
} from '../interfaces';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
|
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||||
|
|
||||||
@CommandHandler(PropogateUpdateSpaceModelCommand)
|
@CommandHandler(PropogateUpdateSpaceModelCommand)
|
||||||
export class PropogateUpdateSpaceModelHandler
|
export class PropogateUpdateSpaceModelHandler
|
||||||
@ -28,86 +26,27 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
|
||||||
const { spaceModel, modifiedSpaceModels, queryRunner } = command.param;
|
console.log("propagate");
|
||||||
|
const { spaceModel, subspaceModels, spaces } = command.param;
|
||||||
|
|
||||||
try {
|
if (!subspaceModels || subspaceModels.length === 0) return;
|
||||||
const spaces = await queryRunner.manager.find(SpaceEntity, {
|
|
||||||
where: { spaceModel },
|
|
||||||
});
|
|
||||||
|
|
||||||
const { modifiedSubspaceModels = {}, modifiedTags = {} } =
|
if (!spaces) {
|
||||||
modifiedSpaceModels;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const {
|
for (const subspaceModel of subspaceModels) {
|
||||||
addedSubspaceModels = [],
|
if (subspaceModel.action === ModifyAction.ADD) {
|
||||||
updatedSubspaceModels = [],
|
await this.addSubspaceModel(subspaceModel, spaces);
|
||||||
deletedSubspaceModels = [],
|
|
||||||
} = modifiedSubspaceModels;
|
|
||||||
|
|
||||||
const { added = [], updated = [], deleted = [] } = modifiedTags;
|
|
||||||
|
|
||||||
if (addedSubspaceModels.length > 0) {
|
|
||||||
await this.addSubspaceModels(
|
|
||||||
modifiedSpaceModels.modifiedSubspaceModels.addedSubspaceModels,
|
|
||||||
spaces,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
} else if (updatedSubspaceModels.length > 0) {
|
|
||||||
await this.updateSubspaceModels(
|
|
||||||
modifiedSpaceModels.modifiedSubspaceModels.updatedSubspaceModels,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (deletedSubspaceModels.length > 0) {
|
|
||||||
const dtos: ModifySubspaceDto[] =
|
|
||||||
modifiedSpaceModels.modifiedSubspaceModels.deletedSubspaceModels.map(
|
|
||||||
(model) => ({
|
|
||||||
action: ModifyAction.DELETE,
|
|
||||||
uuid: model,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
await this.subSpaceService.modifySubSpace(dtos, queryRunner);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (added.length > 0) {
|
|
||||||
await this.createTags(
|
|
||||||
modifiedSpaceModels.modifiedTags.added,
|
|
||||||
queryRunner,
|
|
||||||
null,
|
|
||||||
spaceModel,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (updated.length > 0) {
|
|
||||||
await this.updateTags(
|
|
||||||
modifiedSpaceModels.modifiedTags.updated,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deleted.length > 0) {
|
|
||||||
await this.deleteTags(
|
|
||||||
modifiedSpaceModels.modifiedTags.deleted,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addSubspaceModels(
|
async addSubspaceModel(
|
||||||
subspaceModels: SubspaceModelEntity[],
|
subspaceModel: ISingleSubspaceModel,
|
||||||
spaces: SpaceEntity[],
|
spaces: SpaceEntity[],
|
||||||
queryRunner: QueryRunner,
|
|
||||||
) {
|
) {
|
||||||
for (const space of spaces) {
|
console.log(`subspace is ${JSON.stringify(subspaceModel)}`);
|
||||||
await this.subSpaceService.createSubSpaceFromModel(
|
|
||||||
subspaceModels,
|
|
||||||
[space],
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateSubspaceModels(
|
async updateSubspaceModels(
|
||||||
|
|||||||
@ -365,14 +365,6 @@ export class SpaceModelProductAllocationService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.spaceProductAllocationService.propagateDeleteToInheritedAllocations(
|
|
||||||
queryRunner,
|
|
||||||
allocationsToUpdate,
|
|
||||||
tagUuidsToDelete,
|
|
||||||
project,
|
|
||||||
spaces,
|
|
||||||
);
|
|
||||||
|
|
||||||
await queryRunner.manager
|
await queryRunner.manager
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.delete()
|
.delete()
|
||||||
|
|||||||
@ -25,7 +25,10 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|||||||
import { CommandBus } from '@nestjs/cqrs';
|
import { CommandBus } from '@nestjs/cqrs';
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
import { ProcessTagDto } from 'src/tags/dtos';
|
||||||
import { SpaceModelProductAllocationService } from './space-model-product-allocation.service';
|
import { SpaceModelProductAllocationService } from './space-model-product-allocation.service';
|
||||||
import { PropogateDeleteSpaceModelCommand } from '../commands';
|
import {
|
||||||
|
PropogateDeleteSpaceModelCommand,
|
||||||
|
PropogateUpdateSpaceModelCommand,
|
||||||
|
} from '../commands';
|
||||||
import {
|
import {
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
@ -44,7 +47,6 @@ 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';
|
import { ISingleSubspaceModel } from '../interfaces';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -239,6 +241,14 @@ export class SpaceModelService {
|
|||||||
}
|
}
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
|
|
||||||
|
await this.commandBus.execute(
|
||||||
|
new PropogateUpdateSpaceModelCommand({
|
||||||
|
spaceModel: spaceModel,
|
||||||
|
subspaceModels: modifiedSubspaces,
|
||||||
|
spaces: spaces,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: 'SpaceModel updated successfully',
|
message: 'SpaceModel updated successfully',
|
||||||
});
|
});
|
||||||
|
|||||||
@ -155,7 +155,13 @@ export class SubSpaceModelService {
|
|||||||
queryRunner,
|
queryRunner,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
);
|
);
|
||||||
return [...createdSubspaces, ...updatedSubspaces, ...deletedSubspaces];
|
return [
|
||||||
|
createdSubspaces ?? [],
|
||||||
|
updatedSubspaces ?? [],
|
||||||
|
deletedSubspaces ?? [],
|
||||||
|
]
|
||||||
|
.filter((arr) => arr.length > 0)
|
||||||
|
.flat();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in modifySubspaceModels:', error);
|
console.error('Error in modifySubspaceModels:', error);
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
|||||||
@ -81,24 +81,6 @@ export class SubspaceDeviceService {
|
|||||||
const subspace = await this.findSubspace(subSpaceUuid);
|
const subspace = await this.findSubspace(subSpaceUuid);
|
||||||
const device = await this.findDevice(deviceUuid);
|
const device = await this.findDevice(deviceUuid);
|
||||||
|
|
||||||
if (device.tag?.subspace?.uuid !== subspace.uuid) {
|
|
||||||
await this.tagRepository.update(
|
|
||||||
{ uuid: device.tag.uuid },
|
|
||||||
{ subspace, space: null },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!device.tag) {
|
|
||||||
const tag = this.tagRepository.create({
|
|
||||||
tag: `Tag ${this.findNextTag()}`,
|
|
||||||
product: device.productDevice,
|
|
||||||
subspace: subspace,
|
|
||||||
device: device,
|
|
||||||
});
|
|
||||||
await this.tagRepository.save(tag);
|
|
||||||
device.tag = tag;
|
|
||||||
}
|
|
||||||
|
|
||||||
device.subspace = subspace;
|
device.subspace = subspace;
|
||||||
|
|
||||||
const newDevice = await this.deviceRepository.save(device);
|
const newDevice = await this.deviceRepository.save(device);
|
||||||
@ -140,26 +122,6 @@ export class SubspaceDeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device.tag?.subspace !== null) {
|
|
||||||
await this.tagRepository.update(
|
|
||||||
{ uuid: device.tag.uuid },
|
|
||||||
{ subspace: null, space: device.spaceDevice },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!device.tag) {
|
|
||||||
const tag = this.tagRepository.create({
|
|
||||||
tag: `Tag ${this.findNextTag()}`,
|
|
||||||
product: device.productDevice,
|
|
||||||
subspace: null,
|
|
||||||
space: device.spaceDevice,
|
|
||||||
device: device,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.tagRepository.save(tag);
|
|
||||||
device.tag = tag;
|
|
||||||
}
|
|
||||||
|
|
||||||
device.subspace = null;
|
device.subspace = null;
|
||||||
const updatedDevice = await this.deviceRepository.save(device);
|
const updatedDevice = await this.deviceRepository.save(device);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user