mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
meger branch with dev branch
This commit is contained in:
@ -6,6 +6,7 @@ import { AuthenticationController } from './auth/controllers/authentication.cont
|
|||||||
import { UserModule } from './users/user.module';
|
import { UserModule } from './users/user.module';
|
||||||
import { HomeModule } from './home/home.module';
|
import { HomeModule } from './home/home.module';
|
||||||
import { RoomModule } from './room/room.module';
|
import { RoomModule } from './room/room.module';
|
||||||
|
import { GroupModule } from './group/group.module';
|
||||||
import { DeviceModule } from './device/device.module';
|
import { DeviceModule } from './device/device.module';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -16,6 +17,7 @@ import { DeviceModule } from './device/device.module';
|
|||||||
UserModule,
|
UserModule,
|
||||||
HomeModule,
|
HomeModule,
|
||||||
RoomModule,
|
RoomModule,
|
||||||
|
GroupModule,
|
||||||
DeviceModule,
|
DeviceModule,
|
||||||
],
|
],
|
||||||
controllers: [AuthenticationController],
|
controllers: [AuthenticationController],
|
||||||
|
91
src/group/controllers/group.controller.ts
Normal file
91
src/group/controllers/group.controller.ts
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import { GroupService } from '../services/group.service';
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Post,
|
||||||
|
UseGuards,
|
||||||
|
Query,
|
||||||
|
Param,
|
||||||
|
Put,
|
||||||
|
Delete,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
|
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||||
|
import { GetGroupDto } from '../dtos/get.group.dto';
|
||||||
|
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||||
|
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||||
|
|
||||||
|
@ApiTags('Group Module')
|
||||||
|
@Controller({
|
||||||
|
version: '1',
|
||||||
|
path: 'group',
|
||||||
|
})
|
||||||
|
export class GroupController {
|
||||||
|
constructor(private readonly groupService: GroupService) {}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get()
|
||||||
|
async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) {
|
||||||
|
try {
|
||||||
|
return await this.groupService.getGroupsByHomeId(getGroupsDto);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get(':groupId')
|
||||||
|
async getGroupsByGroupId(@Param('groupId') groupId: number) {
|
||||||
|
try {
|
||||||
|
return await this.groupService.getGroupsByGroupId(groupId);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post()
|
||||||
|
async addGroup(@Body() addGroupDto: AddGroupDto) {
|
||||||
|
try {
|
||||||
|
return await this.groupService.addGroup(addGroupDto);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('control')
|
||||||
|
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
|
||||||
|
try {
|
||||||
|
return await this.groupService.controlGroup(controlGroupDto);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Put('rename')
|
||||||
|
async renameGroup(@Body() renameGroupDto: RenameGroupDto) {
|
||||||
|
try {
|
||||||
|
return await this.groupService.renameGroup(renameGroupDto);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Delete(':groupId')
|
||||||
|
async deleteGroup(@Param('groupId') groupId: number) {
|
||||||
|
try {
|
||||||
|
return await this.groupService.deleteGroup(groupId);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/group/controllers/index.ts
Normal file
1
src/group/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.controller';
|
36
src/group/dtos/add.group.dto.ts
Normal file
36
src/group/dtos/add.group.dto.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
||||||
|
|
||||||
|
export class AddGroupDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'groupName',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public groupName: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'homeId',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNumberString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public homeId: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'productId',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public productId: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'The list of up to 20 device IDs, separated with commas (,)',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public deviceIds: string;
|
||||||
|
}
|
20
src/group/dtos/control.group.dto.ts
Normal file
20
src/group/dtos/control.group.dto.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsObject, IsNumberString } from 'class-validator';
|
||||||
|
|
||||||
|
export class ControlGroupDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'groupId',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNumberString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public groupId: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'example {"switch_1":true,"add_ele":300}',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsObject()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public properties: object;
|
||||||
|
}
|
28
src/group/dtos/get.group.dto.ts
Normal file
28
src/group/dtos/get.group.dto.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsNumberString } from 'class-validator';
|
||||||
|
|
||||||
|
export class GetGroupDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'homeId',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNumberString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public homeId: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'pageSize',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNumberString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public pageSize: number;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'pageNo',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNumberString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public pageNo: number;
|
||||||
|
}
|
1
src/group/dtos/index.ts
Normal file
1
src/group/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './add.group.dto';
|
20
src/group/dtos/rename.group.dto copy.ts
Normal file
20
src/group/dtos/rename.group.dto copy.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
||||||
|
|
||||||
|
export class RenameGroupDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'groupId',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNumberString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public groupId: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'groupName',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public groupName: string;
|
||||||
|
}
|
11
src/group/group.module.ts
Normal file
11
src/group/group.module.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { GroupService } from './services/group.service';
|
||||||
|
import { GroupController } from './controllers/group.controller';
|
||||||
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
@Module({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
controllers: [GroupController],
|
||||||
|
providers: [GroupService],
|
||||||
|
exports: [GroupService],
|
||||||
|
})
|
||||||
|
export class GroupModule {}
|
26
src/group/interfaces/get.group.interface.ts
Normal file
26
src/group/interfaces/get.group.interface.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
export class GetGroupDetailsInterface {
|
||||||
|
result: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export class GetGroupsInterface {
|
||||||
|
result: {
|
||||||
|
count: number;
|
||||||
|
data_list: [];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class addGroupInterface {
|
||||||
|
success: boolean;
|
||||||
|
msg: string;
|
||||||
|
result: {
|
||||||
|
id: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class controlGroupInterface {
|
||||||
|
success: boolean;
|
||||||
|
result: boolean;
|
||||||
|
msg: string;
|
||||||
|
}
|
245
src/group/services/group.service.ts
Normal file
245
src/group/services/group.service.ts
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||||
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||||
|
import {
|
||||||
|
GetGroupDetailsInterface,
|
||||||
|
GetGroupsInterface,
|
||||||
|
addGroupInterface,
|
||||||
|
controlGroupInterface,
|
||||||
|
} from '../interfaces/get.group.interface';
|
||||||
|
import { GetGroupDto } from '../dtos/get.group.dto';
|
||||||
|
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||||
|
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class GroupService {
|
||||||
|
private tuya: TuyaContext;
|
||||||
|
constructor(private readonly configService: ConfigService) {
|
||||||
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
|
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
||||||
|
this.tuya = new TuyaContext({
|
||||||
|
baseUrl: 'https://openapi.tuyaeu.com',
|
||||||
|
accessKey,
|
||||||
|
secretKey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getGroupsByHomeId(getGroupDto: GetGroupDto) {
|
||||||
|
try {
|
||||||
|
const response = await this.getGroupsTuya(getGroupDto);
|
||||||
|
|
||||||
|
const groups = response.result.data_list.map((group: any) => ({
|
||||||
|
groupId: group.id,
|
||||||
|
groupName: group.name,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return {
|
||||||
|
count: response.result.count,
|
||||||
|
groups: groups,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching groups',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getGroupsTuya(getGroupDto: GetGroupDto): Promise<GetGroupsInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/group`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
query: {
|
||||||
|
space_id: getGroupDto.homeId,
|
||||||
|
page_size: getGroupDto.pageSize,
|
||||||
|
page_no: getGroupDto.pageNo,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return response as unknown as GetGroupsInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching groups ',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async addGroup(addGroupDto: AddGroupDto) {
|
||||||
|
const response = await this.addGroupTuya(addGroupDto);
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
groupId: response.result.id,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
response.msg || 'Unknown error',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async addGroupTuya(addGroupDto: AddGroupDto): Promise<addGroupInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/group`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'POST',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
space_id: addGroupDto.homeId,
|
||||||
|
name: addGroupDto.groupName,
|
||||||
|
product_id: addGroupDto.productId,
|
||||||
|
device_ids: addGroupDto.deviceIds,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as addGroupInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error adding group',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async controlGroup(controlGroupDto: ControlGroupDto) {
|
||||||
|
const response = await this.controlGroupTuya(controlGroupDto);
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
response.msg || 'Unknown error',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async controlGroupTuya(
|
||||||
|
controlGroupDto: ControlGroupDto,
|
||||||
|
): Promise<controlGroupInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/group/properties`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'POST',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
group_id: controlGroupDto.groupId,
|
||||||
|
properties: controlGroupDto.properties,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as controlGroupInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error control group',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async renameGroup(renameGroupDto: RenameGroupDto) {
|
||||||
|
const response = await this.renameGroupTuya(renameGroupDto);
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
return {
|
||||||
|
success: response.success,
|
||||||
|
result: response.result,
|
||||||
|
msg: response.msg,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
response.msg || 'Unknown error',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async renameGroupTuya(
|
||||||
|
renameGroupDto: RenameGroupDto,
|
||||||
|
): Promise<controlGroupInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/group/${renameGroupDto.groupId}/${renameGroupDto.groupName}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'PUT',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as controlGroupInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error rename group',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteGroup(groupId: number) {
|
||||||
|
const response = await this.deleteGroupTuya(groupId);
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
return {
|
||||||
|
success: response.success,
|
||||||
|
result: response.result,
|
||||||
|
msg: response.msg,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
response.msg || 'Unknown error',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async deleteGroupTuya(groupId: number): Promise<controlGroupInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/group/${groupId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'DELETE',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as controlGroupInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error delete group',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getGroupsByGroupId(groupId: number) {
|
||||||
|
try {
|
||||||
|
const response = await this.getGroupsByGroupIdTuya(groupId);
|
||||||
|
|
||||||
|
return {
|
||||||
|
groupId: response.result.id,
|
||||||
|
groupName: response.result.name,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching group',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getGroupsByGroupIdTuya(
|
||||||
|
groupId: number,
|
||||||
|
): Promise<GetGroupDetailsInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/group/${groupId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
return response as GetGroupDetailsInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching group ',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/group/services/index.ts
Normal file
1
src/group/services/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.service';
|
@ -1,5 +1,13 @@
|
|||||||
import { HomeService } from './../services/home.service';
|
import { HomeService } from './../services/home.service';
|
||||||
import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Post,
|
||||||
|
Param,
|
||||||
|
UseGuards,
|
||||||
|
Query,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddHomeDto } from '../dtos/add.home.dto';
|
import { AddHomeDto } from '../dtos/add.home.dto';
|
||||||
@ -14,14 +22,24 @@ export class HomeController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get(':userUuid')
|
@Get()
|
||||||
async userList(@Param('userUuid') userUuid: string) {
|
async getHomesByUserId(@Query('userUuid') userUuid: string) {
|
||||||
try {
|
try {
|
||||||
return await this.homeService.getHomesByUserId(userUuid);
|
return await this.homeService.getHomesByUserId(userUuid);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get(':homeId')
|
||||||
|
async getHomesByHomeId(@Param('homeId') homeId: string) {
|
||||||
|
try {
|
||||||
|
return await this.homeService.getHomeByHomeId(homeId);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
|
6
src/home/interfaces/get.home.interface.ts
Normal file
6
src/home/interfaces/get.home.interface.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export class GetHomeDetailsInterface {
|
||||||
|
result: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
}
|
@ -4,6 +4,7 @@ import { Injectable, HttpException, HttpStatus } 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 { AddHomeDto } from '../dtos';
|
import { AddHomeDto } from '../dtos';
|
||||||
|
import { GetHomeDetailsInterface } from '../interfaces/get.home.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HomeService {
|
export class HomeService {
|
||||||
@ -78,4 +79,35 @@ export class HomeService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getHomeDetails(homeId: string): Promise<GetHomeDetailsInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/space/${homeId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as GetHomeDetailsInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching home details',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getHomeByHomeId(homeId: string) {
|
||||||
|
try {
|
||||||
|
const response = await this.getHomeDetails(homeId);
|
||||||
|
|
||||||
|
return {
|
||||||
|
homeId: response.result.id,
|
||||||
|
homeName: response.result.name,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching home',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
import { RoomService } from '../services/room.service';
|
import { RoomService } from '../services/room.service';
|
||||||
import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Post,
|
||||||
|
UseGuards,
|
||||||
|
Query,
|
||||||
|
Param,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddRoomDto } from '../dtos/add.room.dto';
|
import { AddRoomDto } from '../dtos/add.room.dto';
|
||||||
@ -14,15 +22,24 @@ export class RoomController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get(':homeId')
|
@Get()
|
||||||
async userList(@Param('homeId') homeId: string) {
|
async getRoomsByHomeId(@Query('homeId') homeId: string) {
|
||||||
try {
|
try {
|
||||||
return await this.roomService.getRoomsByHomeId(homeId);
|
return await this.roomService.getRoomsByHomeId(homeId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get(':roomId')
|
||||||
|
async getRoomsByRoomId(@Param('roomId') roomId: string) {
|
||||||
|
try {
|
||||||
|
return await this.roomService.getRoomsByRoomId(roomId);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
|
@ -2,6 +2,7 @@ export class GetRoomDetailsInterface {
|
|||||||
result: {
|
result: {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
root_id: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
export class GetRoomsIdsInterface {
|
export class GetRoomsIdsInterface {
|
||||||
|
@ -96,4 +96,20 @@ export class RoomService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getRoomsByRoomId(roomId: string) {
|
||||||
|
try {
|
||||||
|
const response = await this.getRoomDetails(roomId);
|
||||||
|
|
||||||
|
return {
|
||||||
|
homeId: response.result.root_id,
|
||||||
|
roomId: response.result.id,
|
||||||
|
roomName: response.result.name,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching rooms',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user