device start using space

This commit is contained in:
hannathkadher
2024-10-21 10:00:01 +04:00
parent f8c011f9dc
commit 51922a8ae2
4 changed files with 41 additions and 54 deletions

View File

@ -12,11 +12,8 @@ import {
Put, Put,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto'; import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto';
import { import { GetDeviceLogsDto } from '../dtos/get.device.dto';
GetDeviceByRoomUuidDto,
GetDeviceLogsDto,
} from '../dtos/get.device.dto';
import { import {
ControlDeviceDto, ControlDeviceDto,
BatchControlDevicesDto, BatchControlDevicesDto,
@ -30,7 +27,6 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { CheckDeviceGuard } from 'src/guards/device.guard'; import { CheckDeviceGuard } from 'src/guards/device.guard';
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard'; import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
import { SpaceType } from '@app/common/constants/space-type.enum';
@ApiTags('Device Module') @ApiTags('Device Module')
@Controller({ @Controller({
@ -58,33 +54,21 @@ export class DeviceController {
async getDevicesByUser(@Param('userUuid') userUuid: string) { async getDevicesByUser(@Param('userUuid') userUuid: string) {
return await this.deviceService.getDevicesByUser(userUuid); return await this.deviceService.getDevicesByUser(userUuid);
} }
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard)
@Get(SpaceType.ROOM)
async getDevicesByRoomId(
@Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
@Req() req: any,
) {
const userUuid = req.user.uuid;
return await this.deviceService.getDevicesByRoomId(
getDeviceByRoomUuidDto,
userUuid,
);
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('unit/:unitUuid') @Get('space/:spaceUuid')
async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) { async getDevicesByUnitId(@Param('spaceUuid') spaceUuid: string) {
return await this.deviceService.getDevicesByUnitId(unitUuid); return await this.deviceService.getDevicesBySpaceUuid(spaceUuid);
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard) @UseGuards(JwtAuthGuard, CheckRoomGuard)
@Put('room') @Put('space')
async updateDeviceInRoom( async updateDeviceInRoom(
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto, @Body() updateDeviceInSpaceDto: UpdateDeviceInSpaceDto,
) { ) {
const device = await this.deviceService.updateDeviceInRoom( const device = await this.deviceService.updateDeviceInSpace(
updateDeviceInRoomDto, updateDeviceInSpaceDto,
); );
return { return {

View File

@ -18,7 +18,7 @@ export class AddDeviceDto {
@IsNotEmpty() @IsNotEmpty()
public userUuid: string; public userUuid: string;
} }
export class UpdateDeviceInRoomDto { export class UpdateDeviceInSpaceDto {
@ApiProperty({ @ApiProperty({
description: 'deviceUuid', description: 'deviceUuid',
required: true, required: true,
@ -28,10 +28,10 @@ export class UpdateDeviceInRoomDto {
public deviceUuid: string; public deviceUuid: string;
@ApiProperty({ @ApiProperty({
description: 'roomUuid', description: 'spaceUuid',
required: true, required: true,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public roomUuid: string; public spaceUuid: string;
} }

View File

@ -1,14 +1,14 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator'; import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class GetDeviceByRoomUuidDto { export class GetDeviceBySpaceUuidDto {
@ApiProperty({ @ApiProperty({
description: 'roomUuid', description: 'spaceUuid',
required: true, required: true,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public roomUuid: string; public spaceUuid: string;
} }
export class GetDeviceLogsDto { export class GetDeviceLogsDto {
@ApiProperty({ @ApiProperty({

View File

@ -8,7 +8,7 @@ import {
} from '@nestjs/common'; } from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto'; import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto';
import { import {
DeviceInstructionResponse, DeviceInstructionResponse,
GetDeviceDetailsFunctionsInterface, GetDeviceDetailsFunctionsInterface,
@ -19,7 +19,7 @@ import {
updateDeviceFirmwareInterface, updateDeviceFirmwareInterface,
} from '../interfaces/get.device.interface'; } from '../interfaces/get.device.interface';
import { import {
GetDeviceByRoomUuidDto, GetDeviceBySpaceUuidDto,
GetDeviceLogsDto, GetDeviceLogsDto,
} from '../dtos/get.device.dto'; } from '../dtos/get.device.dto';
import { import {
@ -69,6 +69,7 @@ export class DeviceService {
...(withProductDevice && { relations: ['productDevice'] }), ...(withProductDevice && { relations: ['productDevice'] }),
}); });
} }
async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) { async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) {
return await this.deviceRepository.findOne({ return await this.deviceRepository.findOne({
where: { where: {
@ -77,6 +78,7 @@ export class DeviceService {
relations: ['productDevice'], relations: ['productDevice'],
}); });
} }
async addDeviceUser(addDeviceDto: AddDeviceDto) { async addDeviceUser(addDeviceDto: AddDeviceDto) {
try { try {
const device = await this.getDeviceDetailsByDeviceIdTuya( const device = await this.getDeviceDetailsByDeviceIdTuya(
@ -116,12 +118,13 @@ export class DeviceService {
); );
} else { } else {
throw new HttpException( throw new HttpException(
error.message || 'Failed to add device in room', error.message || 'Failed to add device in space',
error.status || HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
} }
async getDevicesByUser( async getDevicesByUser(
userUuid: string, userUuid: string,
): Promise<GetDeviceDetailsInterface[]> { ): Promise<GetDeviceDetailsInterface[]> {
@ -168,14 +171,15 @@ export class DeviceService {
); );
} }
} }
async getDevicesByRoomId(
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, async getDevicesBySpaceId(
getDeviceBySpaceUuidDto: GetDeviceBySpaceUuidDto,
userUuid: string, userUuid: string,
): Promise<GetDeviceDetailsInterface[]> { ): Promise<GetDeviceDetailsInterface[]> {
try { try {
const devices = await this.deviceRepository.find({ const devices = await this.deviceRepository.find({
where: { where: {
spaceDevice: { uuid: getDeviceByRoomUuidDto.roomUuid }, spaceDevice: { uuid: getDeviceBySpaceUuidDto.spaceUuid },
isActive: true, isActive: true,
permission: { permission: {
userUuid, userUuid,
@ -210,22 +214,22 @@ export class DeviceService {
} catch (error) { } catch (error) {
// Handle the error here // Handle the error here
throw new HttpException( throw new HttpException(
'Error fetching devices by room', 'Error fetching devices by space',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
async updateDeviceInRoom(updateDeviceInRoomDto: UpdateDeviceInRoomDto) { async updateDeviceInSpace(updateDeviceInSpaceDto: UpdateDeviceInSpaceDto) {
try { try {
await this.deviceRepository.update( await this.deviceRepository.update(
{ uuid: updateDeviceInRoomDto.deviceUuid }, { uuid: updateDeviceInSpaceDto.deviceUuid },
{ {
spaceDevice: { uuid: updateDeviceInRoomDto.roomUuid }, spaceDevice: { uuid: updateDeviceInSpaceDto.spaceUuid },
}, },
); );
const device = await this.deviceRepository.findOne({ const device = await this.deviceRepository.findOne({
where: { where: {
uuid: updateDeviceInRoomDto.deviceUuid, uuid: updateDeviceInSpaceDto.deviceUuid,
}, },
relations: ['spaceDevice', 'spaceDevice.parent'], relations: ['spaceDevice', 'spaceDevice.parent'],
}); });
@ -238,15 +242,16 @@ export class DeviceService {
return { return {
uuid: device.uuid, uuid: device.uuid,
roomUuid: device.spaceDevice.uuid, spaceUuid: device.spaceDevice.uuid,
}; };
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
'Failed to add device in room', 'Failed to add device in space',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
async transferDeviceInSpacesTuya( async transferDeviceInSpacesTuya(
deviceId: string, deviceId: string,
spaceId: string, spaceId: string,
@ -294,6 +299,7 @@ export class DeviceService {
); );
} }
} }
async factoryResetDeviceTuya( async factoryResetDeviceTuya(
deviceUuid: string, deviceUuid: string,
): Promise<controlDeviceInterface> { ): Promise<controlDeviceInterface> {
@ -336,6 +342,7 @@ export class DeviceService {
); );
} }
} }
async batchControlDevices(batchControlDevicesDto: BatchControlDevicesDto) { async batchControlDevices(batchControlDevicesDto: BatchControlDevicesDto) {
const { devicesUuid } = batchControlDevicesDto; const { devicesUuid } = batchControlDevicesDto;
@ -777,12 +784,12 @@ export class DeviceService {
); );
} }
} }
async getDevicesByUnitId(unitUuid: string) { async getDevicesBySpaceUuid(SpaceUuid: string) {
try { try {
const spaces = await this.spaceRepository.find({ const spaces = await this.spaceRepository.find({
where: { where: {
parent: { parent: {
uuid: unitUuid, uuid: SpaceUuid,
}, },
devicesSpaceEntity: { devicesSpaceEntity: {
isActive: true, isActive: true,
@ -813,7 +820,7 @@ export class DeviceService {
return devicesData; return devicesData;
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
'This unit does not have any devices', 'This space does not have any devices',
HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND,
); );
} }
@ -874,16 +881,12 @@ export class DeviceService {
} }
} }
const spaceDevice = device?.spaceDevice; const spaceDevice = device?.spaceDevice;
const parentDevice = spaceDevice?.parent;
return { return {
room: { space: {
uuid: spaceDevice?.uuid, uuid: spaceDevice?.uuid,
name: spaceDevice?.spaceName, name: spaceDevice?.spaceName,
}, },
unit: {
uuid: parentDevice?.uuid,
name: parentDevice?.spaceName,
},
productUuid: device.productDevice.uuid, productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType, productType: device.productDevice.prodType,
permissionType: device.permission[0].permissionType.type, permissionType: device.permission[0].permissionType.type,