mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
fixed issue in association and disassociation
This commit is contained in:
@ -1,11 +1,4 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
Unique,
|
||||
OneToOne,
|
||||
} from 'typeorm';
|
||||
import { Entity, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { TagDto } from '../dtos';
|
||||
@ -15,7 +8,6 @@ import { SubspaceEntity } from './subspace';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
|
||||
@Entity({ name: 'tag' })
|
||||
@Unique(['tag', 'product', 'space', 'subspace'])
|
||||
export class TagEntity extends AbstractEntity<TagDto> {
|
||||
@Column({ type: 'varchar', length: 255 })
|
||||
tag: string;
|
||||
|
@ -23,7 +23,7 @@ export class SpaceSceneService {
|
||||
try {
|
||||
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
|
@ -72,7 +72,7 @@ export class SpaceUserService {
|
||||
}
|
||||
|
||||
// Find the space by ID
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
|
@ -492,7 +492,7 @@ export class SpaceService {
|
||||
params: GetSpaceParam,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
|
@ -29,22 +29,14 @@ export class SubspaceDeviceService {
|
||||
): Promise<BaseResponseDto> {
|
||||
const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params;
|
||||
|
||||
console.time('Total Execution Time'); // ⏳ Start total execution time
|
||||
console.time('Validation Time');
|
||||
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
);
|
||||
|
||||
console.timeEnd('Validation Time'); // ⏳ Log validation time
|
||||
|
||||
console.time('Subspace Query Time');
|
||||
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) => {
|
||||
try {
|
||||
const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya(
|
||||
@ -62,17 +54,11 @@ export class SubspaceDeviceService {
|
||||
...tuyaDetails,
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`⚠️ Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
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({
|
||||
data: detailedDevices.filter(Boolean), // Remove nulls
|
||||
@ -119,14 +105,14 @@ export class SubspaceDeviceService {
|
||||
|
||||
return new SuccessResponseDto({
|
||||
data: newDevice,
|
||||
message: 'Successfully associated device to subspace',
|
||||
message: `Successfully associated device to subspace`,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'Failed to associate device to subspace',
|
||||
`Failed to associate device to subspace with error = ${error}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
@ -145,7 +131,7 @@ export class SubspaceDeviceService {
|
||||
spaceUuid,
|
||||
);
|
||||
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) {
|
||||
throw new HttpException(
|
||||
@ -157,7 +143,7 @@ export class SubspaceDeviceService {
|
||||
if (device.tag?.subspace !== null) {
|
||||
await this.tagRepository.update(
|
||||
{ uuid: device.tag.uuid },
|
||||
{ subspace: null, space: device.subspace },
|
||||
{ subspace: null, space: device.spaceDevice },
|
||||
);
|
||||
}
|
||||
|
||||
@ -186,7 +172,7 @@ export class SubspaceDeviceService {
|
||||
throw error;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'Failed to dissociate device from subspace',
|
||||
`Failed to dissociate device from subspace error = ${error}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
@ -218,7 +204,13 @@ export class SubspaceDeviceService {
|
||||
private async findDevice(deviceUuid: string) {
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuid },
|
||||
relations: ['subspace', , 'tag', 'tag.space', 'tag.subspace'],
|
||||
relations: [
|
||||
'subspace',
|
||||
'tag',
|
||||
'tag.space',
|
||||
'tag.subspace',
|
||||
'spaceDevice',
|
||||
],
|
||||
});
|
||||
if (!device) {
|
||||
this.throwNotFound('Device', deviceUuid);
|
||||
@ -300,4 +292,12 @@ export class SubspaceDeviceService {
|
||||
: 1;
|
||||
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'],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ export class SubSpaceService {
|
||||
pageable: Partial<TypeORMCustomModelFindAllQuery>,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { communityUuid, spaceUuid, projectUuid } = params;
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
@ -205,7 +205,7 @@ export class SubSpaceService {
|
||||
updateSubSpaceDto: AddSubspaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params;
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
@ -244,7 +244,7 @@ export class SubSpaceService {
|
||||
|
||||
async delete(params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params;
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
spaceUuid,
|
||||
@ -367,7 +367,7 @@ export class SubSpaceService {
|
||||
}
|
||||
|
||||
async getOne(params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
||||
params.communityUuid,
|
||||
params.projectUuid,
|
||||
params.spaceUuid,
|
||||
|
Reference in New Issue
Block a user