added moving tags

This commit is contained in:
hannathkadher
2025-01-30 21:43:30 +04:00
parent 93b7f7a154
commit 11faa52283
3 changed files with 58 additions and 4 deletions

View File

@ -323,6 +323,8 @@ export class SpaceService {
updateSpaceDto: UpdateSpaceDto, updateSpaceDto: UpdateSpaceDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params; const { communityUuid, spaceUuid, projectUuid } = params;
console.log(communityUuid, spaceUuid, projectUuid);
console.log(updateSpaceDto);
const queryRunner = this.dataSource.createQueryRunner(); const queryRunner = this.dataSource.createQueryRunner();
try { try {

View File

@ -11,6 +11,7 @@ import { ValidationService } from '../space-validation.service';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository'; import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { In, QueryRunner } from 'typeorm'; import { In, QueryRunner } from 'typeorm';
import { DeviceEntity } from '@app/common/modules/device/entities'; import { DeviceEntity } from '@app/common/modules/device/entities';
import { TagRepository } from '@app/common/modules/space';
@Injectable() @Injectable()
export class SubspaceDeviceService { export class SubspaceDeviceService {
@ -20,6 +21,7 @@ export class SubspaceDeviceService {
private readonly tuyaService: TuyaService, private readonly tuyaService: TuyaService,
private readonly productRepository: ProductRepository, private readonly productRepository: ProductRepository,
private readonly validationService: ValidationService, private readonly validationService: ValidationService,
private readonly tagRepository: TagRepository,
) {} ) {}
async listDevicesInSubspace( async listDevicesInSubspace(
@ -80,6 +82,35 @@ 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);
console.log(device);
if (device.tag) {
console.log(device.tag);
const tag = device.tag;
if (tag.subspace !== null && tag.subspace.uuid === subspace.uuid) {
//do nothing
}
if (tag.subspace !== null && tag.subspace.uuid !== subspace.uuid) {
await this.tagRepository.update(
{
uuid: tag.uuid,
},
{
subspace: subspace,
},
);
}
if (tag.subspace === null) {
await this.tagRepository.update(
{
uuid: tag.uuid,
},
{
subspace: subspace,
},
);
}
}
device.subspace = subspace; device.subspace = subspace;
@ -123,6 +154,24 @@ export class SubspaceDeviceService {
); );
} }
if (device.tag) {
console.log(device.tag);
const tag = device.tag;
if (tag.subspace === null) {
//do nothing
}
if (tag.subspace !== null) {
await this.tagRepository.update(
{
uuid: tag.uuid,
},
{
subspace: null,
space: device.subspace,
},
);
}
}
device.subspace = null; device.subspace = null;
const updatedDevice = await this.deviceRepository.save(device); const updatedDevice = await this.deviceRepository.save(device);
@ -167,7 +216,7 @@ export class SubspaceDeviceService {
private async findDevice(deviceUuid: string) { private async findDevice(deviceUuid: string) {
const device = await this.deviceRepository.findOne({ const device = await this.deviceRepository.findOne({
where: { uuid: deviceUuid }, where: { uuid: deviceUuid },
relations: ['subspace'], relations: ['subspace', 'tag', 'tag.space', 'tag.subspace'],
}); });
if (!device) { if (!device) {
this.throwNotFound('Device', deviceUuid); this.throwNotFound('Device', deviceUuid);

View File

@ -8,7 +8,6 @@ import {
import { TagModel } from '@app/common/modules/space-model'; import { TagModel } from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ProductService } from 'src/product/services'; import { ProductService } from 'src/product/services';
import { ModifyTagModelDto } from 'src/space-model/dtos';
import { CreateTagDto, ModifySubspaceDto } from 'src/space/dtos'; import { CreateTagDto, ModifySubspaceDto } from 'src/space/dtos';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto'; import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { QueryRunner } from 'typeorm'; import { QueryRunner } from 'typeorm';
@ -32,10 +31,13 @@ export class TagService {
const combinedTags = this.combineTags(tags, additionalTags); const combinedTags = this.combineTags(tags, additionalTags);
this.ensureNoDuplicateTags(combinedTags); this.ensureNoDuplicateTags(combinedTags);
console.log(tags);
const tagEntitiesToCreate = tags.filter((tagDto) => tagDto.uuid === null); const tagEntitiesToCreate = tags.filter((tagDto) => !tagDto.uuid);
const tagEntitiesToUpdate = tags.filter((tagDto) => tagDto.uuid !== null); const tagEntitiesToUpdate = tags.filter((tagDto) => tagDto.uuid !== null);
console.log(tagEntitiesToCreate);
try { try {
const createdTags = await this.bulkSaveTags( const createdTags = await this.bulkSaveTags(
tagEntitiesToCreate, tagEntitiesToCreate,
@ -79,7 +81,8 @@ export class TagService {
), ),
), ),
); );
console.log('here');
console.log(JSON.stringify(tags));
try { try {
return await queryRunner.manager.save(tagEntities); return await queryRunner.manager.save(tagEntities);
} catch (error) { } catch (error) {