mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
Add endpoint to get devices by unit ID
This commit is contained in:
@ -83,7 +83,19 @@ export class DeviceController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('unit/:unitUuid')
|
||||||
|
async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.deviceService.getDevicesByUnitId(unitUuid);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||||
@Put('room')
|
@Put('room')
|
||||||
|
|||||||
@ -24,6 +24,7 @@ import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|||||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||||
import { In } from 'typeorm';
|
import { In } from 'typeorm';
|
||||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceService {
|
export class DeviceService {
|
||||||
@ -32,6 +33,7 @@ export class DeviceService {
|
|||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
private readonly productRepository: ProductRepository,
|
private readonly productRepository: ProductRepository,
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
@ -538,4 +540,42 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getDevicesByUnitId(unitUuid: string) {
|
||||||
|
try {
|
||||||
|
const spaces = await this.spaceRepository.find({
|
||||||
|
where: {
|
||||||
|
parent: {
|
||||||
|
uuid: unitUuid,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const devices = spaces.flatMap((space) => {
|
||||||
|
return space.devicesSpaceEntity.map((device) => device);
|
||||||
|
});
|
||||||
|
|
||||||
|
const devicesData = await Promise.all(
|
||||||
|
devices.map(async (device) => {
|
||||||
|
return {
|
||||||
|
haveRoom: true,
|
||||||
|
productUuid: device.productDevice.uuid,
|
||||||
|
productType: device.productDevice.prodType,
|
||||||
|
permissionType: PermissionType.CONTROLLABLE,
|
||||||
|
...(await this.getDeviceDetailsByDeviceIdTuya(
|
||||||
|
device.deviceTuyaUuid,
|
||||||
|
)),
|
||||||
|
uuid: device.uuid,
|
||||||
|
} as GetDeviceDetailsInterface;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return devicesData;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'This unit does not have any devices',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user