fixed issue in association and disassociation

This commit is contained in:
hannathkadher
2025-02-03 11:54:49 +04:00
parent 427e764539
commit 3bb88a6e97
6 changed files with 28 additions and 36 deletions

View File

@ -1,11 +1,4 @@
import { import { Entity, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm';
Entity,
Column,
ManyToOne,
JoinColumn,
Unique,
OneToOne,
} from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { ProductEntity } from '../../product/entities'; import { ProductEntity } from '../../product/entities';
import { TagDto } from '../dtos'; import { TagDto } from '../dtos';
@ -15,7 +8,6 @@ import { SubspaceEntity } from './subspace';
import { DeviceEntity } from '../../device/entities'; import { DeviceEntity } from '../../device/entities';
@Entity({ name: 'tag' }) @Entity({ name: 'tag' })
@Unique(['tag', 'product', 'space', 'subspace'])
export class TagEntity extends AbstractEntity<TagDto> { export class TagEntity extends AbstractEntity<TagDto> {
@Column({ type: 'varchar', length: 255 }) @Column({ type: 'varchar', length: 255 })
tag: string; tag: string;

View File

@ -23,7 +23,7 @@ export class SpaceSceneService {
try { try {
const { spaceUuid, communityUuid, projectUuid } = params; const { spaceUuid, communityUuid, projectUuid } = params;
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,

View File

@ -72,7 +72,7 @@ export class SpaceUserService {
} }
// Find the space by ID // Find the space by ID
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,

View File

@ -492,7 +492,7 @@ export class SpaceService {
params: GetSpaceParam, params: GetSpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { spaceUuid, communityUuid, projectUuid } = params; const { spaceUuid, communityUuid, projectUuid } = params;
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,

View File

@ -29,22 +29,14 @@ export class SubspaceDeviceService {
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params; const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params;
console.time('Total Execution Time'); // ⏳ Start total execution time
console.time('Validation Time');
await this.validationService.checkCommunityAndProjectSpaceExistence( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,
); );
console.timeEnd('Validation Time'); // ⏳ Log validation time
console.time('Subspace Query Time');
const subspace = await this.findSubspaceWithDevices(subSpaceUuid); const subspace = await this.findSubspaceWithDevices(subSpaceUuid);
console.timeEnd('Subspace Query Time'); // ⏳ Log subspace fetching time
console.time('Fetching Tuya Details Time');
const safeFetch = async (device: any) => { const safeFetch = async (device: any) => {
try { try {
const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya( const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya(
@ -62,17 +54,11 @@ export class SubspaceDeviceService {
...tuyaDetails, ...tuyaDetails,
}; };
} catch (error) { } catch (error) {
console.warn(
`⚠️ Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`,
);
return null; return null;
} }
}; };
const detailedDevices = await Promise.all(subspace.devices.map(safeFetch)); const detailedDevices = await Promise.all(subspace.devices.map(safeFetch));
console.timeEnd('Fetching Tuya Details Time'); // ⏳ Log total Tuya fetching time
console.timeEnd('Total Execution Time'); // ⏳ End total execution time
return new SuccessResponseDto({ return new SuccessResponseDto({
data: detailedDevices.filter(Boolean), // Remove nulls data: detailedDevices.filter(Boolean), // Remove nulls
@ -119,14 +105,14 @@ export class SubspaceDeviceService {
return new SuccessResponseDto({ return new SuccessResponseDto({
data: newDevice, data: newDevice,
message: 'Successfully associated device to subspace', message: `Successfully associated device to subspace`,
}); });
} catch (error) { } catch (error) {
if (error instanceof HttpException) { if (error instanceof HttpException) {
throw error; throw error;
} else { } else {
throw new HttpException( throw new HttpException(
'Failed to associate device to subspace', `Failed to associate device to subspace with error = ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
@ -145,7 +131,7 @@ export class SubspaceDeviceService {
spaceUuid, spaceUuid,
); );
const subspace = await this.findSubspace(subSpaceUuid); const subspace = await this.findSubspace(subSpaceUuid);
const device = await this.findDevice(deviceUuid); const device = await this.findDeviceWithSubspaceAndTag(deviceUuid);
if (!device.subspace || device.subspace.uuid !== subspace.uuid) { if (!device.subspace || device.subspace.uuid !== subspace.uuid) {
throw new HttpException( throw new HttpException(
@ -157,7 +143,7 @@ export class SubspaceDeviceService {
if (device.tag?.subspace !== null) { if (device.tag?.subspace !== null) {
await this.tagRepository.update( await this.tagRepository.update(
{ uuid: device.tag.uuid }, { uuid: device.tag.uuid },
{ subspace: null, space: device.subspace }, { subspace: null, space: device.spaceDevice },
); );
} }
@ -186,7 +172,7 @@ export class SubspaceDeviceService {
throw error; throw error;
} else { } else {
throw new HttpException( throw new HttpException(
'Failed to dissociate device from subspace', `Failed to dissociate device from subspace error = ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
@ -218,7 +204,13 @@ 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', , 'tag', 'tag.space', 'tag.subspace'], relations: [
'subspace',
'tag',
'tag.space',
'tag.subspace',
'spaceDevice',
],
}); });
if (!device) { if (!device) {
this.throwNotFound('Device', deviceUuid); this.throwNotFound('Device', deviceUuid);
@ -300,4 +292,12 @@ export class SubspaceDeviceService {
: 1; : 1;
return nextTagNumber; return nextTagNumber;
} }
private async findDeviceWithSubspaceAndTag(deviceUuid: string) {
return await this.deviceRepository.findOne({
where: { uuid: deviceUuid },
relations: ['subspace', 'tag', 'spaceDevice'],
select: ['uuid', 'subspace', 'spaceDevice', 'productDevice', 'tag'],
});
}
} }

View File

@ -178,7 +178,7 @@ export class SubSpaceService {
pageable: Partial<TypeORMCustomModelFindAllQuery>, pageable: Partial<TypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params; const { communityUuid, spaceUuid, projectUuid } = params;
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,
@ -205,7 +205,7 @@ export class SubSpaceService {
updateSubSpaceDto: AddSubspaceDto, updateSubSpaceDto: AddSubspaceDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params; const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params;
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,
@ -244,7 +244,7 @@ export class SubSpaceService {
async delete(params: GetSubSpaceParam): Promise<BaseResponseDto> { async delete(params: GetSubSpaceParam): Promise<BaseResponseDto> {
const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params; const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params;
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,
@ -367,7 +367,7 @@ export class SubSpaceService {
} }
async getOne(params: GetSubSpaceParam): Promise<BaseResponseDto> { async getOne(params: GetSubSpaceParam): Promise<BaseResponseDto> {
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
params.communityUuid, params.communityUuid,
params.projectUuid, params.projectUuid,
params.spaceUuid, params.spaceUuid,