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