mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Merge pull request #9 from SyncrowIOT/create-home-endpoint
Create home endpoint
This commit is contained in:
@ -5,6 +5,7 @@ import { SnakeNamingStrategy } from './strategies';
|
||||
import { UserEntity } from '../modules/user/entities/user.entity';
|
||||
import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
||||
import { UserOtpEntity } from '../modules/user-otp/entities';
|
||||
import { HomeEntity } from '../modules/home/entities';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -19,7 +20,7 @@ import { UserOtpEntity } from '../modules/user-otp/entities';
|
||||
username: configService.get('DB_USER'),
|
||||
password: configService.get('DB_PASSWORD'),
|
||||
database: configService.get('DB_NAME'),
|
||||
entities: [UserEntity, UserSessionEntity, UserOtpEntity],
|
||||
entities: [UserEntity, UserSessionEntity, UserOtpEntity, HomeEntity],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
logging: true,
|
||||
|
19
libs/common/src/modules/home/dtos/home.dto.ts
Normal file
19
libs/common/src/modules/home/dtos/home.dto.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class HomeDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeId: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeName: string;
|
||||
}
|
1
libs/common/src/modules/home/dtos/index.ts
Normal file
1
libs/common/src/modules/home/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.dto';
|
34
libs/common/src/modules/home/entities/home.entity.ts
Normal file
34
libs/common/src/modules/home/entities/home.entity.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { HomeDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
|
||||
@Entity({ name: 'home' })
|
||||
export class HomeEntity extends AbstractEntity<HomeDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
userUuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
unique: true,
|
||||
})
|
||||
public homeId: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public homeName: string;
|
||||
|
||||
constructor(partial: Partial<HomeEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
1
libs/common/src/modules/home/entities/index.ts
Normal file
1
libs/common/src/modules/home/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.entity';
|
11
libs/common/src/modules/home/home.repository.module.ts
Normal file
11
libs/common/src/modules/home/home.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { HomeEntity } from './entities/home.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([HomeEntity])],
|
||||
})
|
||||
export class HomeRepositoryModule {}
|
10
libs/common/src/modules/home/repositories/home.repository.ts
Normal file
10
libs/common/src/modules/home/repositories/home.repository.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HomeEntity } from '../entities/home.entity';
|
||||
|
||||
@Injectable()
|
||||
export class HomeRepository extends Repository<HomeEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(HomeEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
1
libs/common/src/modules/home/repositories/index.ts
Normal file
1
libs/common/src/modules/home/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.repository';
|
@ -4,6 +4,8 @@ import config from './config';
|
||||
import { AuthenticationModule } from './auth/auth.module';
|
||||
import { AuthenticationController } from './auth/controllers/authentication.controller';
|
||||
import { UserModule } from './users/user.module';
|
||||
import { HomeModule } from './home/home.module';
|
||||
import { RoomModule } from './room/room.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
@ -11,6 +13,8 @@ import { UserModule } from './users/user.module';
|
||||
}),
|
||||
AuthenticationModule,
|
||||
UserModule,
|
||||
HomeModule,
|
||||
RoomModule,
|
||||
],
|
||||
controllers: [AuthenticationController],
|
||||
})
|
||||
|
36
src/home/controllers/home.controller.ts
Normal file
36
src/home/controllers/home.controller.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { HomeService } from './../services/home.service';
|
||||
import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddHomeDto } from '../dtos/add.home.dto';
|
||||
|
||||
@ApiTags('Home Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'home',
|
||||
})
|
||||
export class HomeController {
|
||||
constructor(private readonly homeService: HomeService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':userUuid')
|
||||
async userList(@Param('userUuid') userUuid: string) {
|
||||
try {
|
||||
return await this.homeService.getHomesByUserId(userUuid);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async addHome(@Body() addHomeDto: AddHomeDto) {
|
||||
try {
|
||||
return await this.homeService.addHome(addHomeDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
}
|
1
src/home/controllers/index.ts
Normal file
1
src/home/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.controller';
|
20
src/home/dtos/add.home.dto.ts
Normal file
20
src/home/dtos/add.home.dto.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddHomeDto {
|
||||
@ApiProperty({
|
||||
description: 'userUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'homeName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeName: string;
|
||||
}
|
1
src/home/dtos/index.ts
Normal file
1
src/home/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './add.home.dto';
|
14
src/home/home.module.ts
Normal file
14
src/home/home.module.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HomeService } from './services/home.service';
|
||||
import { HomeController } from './controllers/home.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { HomeRepositoryModule } from '@app/common/modules/home/home.repository.module';
|
||||
import { HomeRepository } from '@app/common/modules/home/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, HomeRepositoryModule],
|
||||
controllers: [HomeController],
|
||||
providers: [HomeService, HomeRepository],
|
||||
exports: [HomeService],
|
||||
})
|
||||
export class HomeModule {}
|
81
src/home/services/home.service.ts
Normal file
81
src/home/services/home.service.ts
Normal file
@ -0,0 +1,81 @@
|
||||
import { HomeRepository } from './../../../libs/common/src/modules/home/repositories/home.repository';
|
||||
import { HomeEntity } from './../../../libs/common/src/modules/home/entities/home.entity';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddHomeDto } from '../dtos';
|
||||
|
||||
@Injectable()
|
||||
export class HomeService {
|
||||
private tuya: TuyaContext;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly homeRepository: HomeRepository,
|
||||
) {
|
||||
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 getHomesByUserId(userUuid: string) {
|
||||
const homesData = await this.findHomes(userUuid);
|
||||
|
||||
const homesMapper = homesData.map((home) => ({
|
||||
homeId: home.homeId,
|
||||
homeName: home.homeName,
|
||||
}));
|
||||
|
||||
return homesMapper;
|
||||
}
|
||||
|
||||
async findHomes(userUuid: string) {
|
||||
try {
|
||||
return await this.homeRepository.find({
|
||||
where: {
|
||||
userUuid: userUuid,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error get homes',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async addHome(addHomeDto: AddHomeDto) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/creation`;
|
||||
const data = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: { name: addHomeDto.homeName },
|
||||
});
|
||||
if (data.success) {
|
||||
const homeEntity = {
|
||||
userUuid: addHomeDto.userUuid,
|
||||
homeId: data.result,
|
||||
homeName: addHomeDto.homeName,
|
||||
} as HomeEntity;
|
||||
const savedHome = await this.homeRepository.save(homeEntity);
|
||||
return {
|
||||
homeId: savedHome.homeId,
|
||||
homeName: savedHome.homeName,
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: data.success,
|
||||
homeId: data.result,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding home',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
1
src/home/services/index.ts
Normal file
1
src/home/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.service';
|
1
src/room/controllers/index.ts
Normal file
1
src/room/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './room.controller';
|
36
src/room/controllers/room.controller.ts
Normal file
36
src/room/controllers/room.controller.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { RoomService } from '../services/room.service';
|
||||
import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddRoomDto } from '../dtos/add.room.dto';
|
||||
|
||||
@ApiTags('Room Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'room',
|
||||
})
|
||||
export class RoomController {
|
||||
constructor(private readonly roomService: RoomService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':homeId')
|
||||
async userList(@Param('homeId') homeId: string) {
|
||||
try {
|
||||
return await this.roomService.getRoomsByHomeId(homeId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
||||
try {
|
||||
return await this.roomService.addRoom(addRoomDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
}
|
20
src/room/dtos/add.room.dto.ts
Normal file
20
src/room/dtos/add.room.dto.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
||||
|
||||
export class AddRoomDto {
|
||||
@ApiProperty({
|
||||
description: 'roomName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roomName: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'homeId',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
public homeId: string;
|
||||
}
|
1
src/room/dtos/index.ts
Normal file
1
src/room/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './add.room.dto';
|
11
src/room/interfaces/get.room.interface.ts
Normal file
11
src/room/interfaces/get.room.interface.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export class GetRoomDetailsInterface {
|
||||
result: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
export class GetRoomsIdsInterface {
|
||||
result: {
|
||||
data: [];
|
||||
};
|
||||
}
|
11
src/room/room.module.ts
Normal file
11
src/room/room.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { RoomService } from './services/room.service';
|
||||
import { RoomController } from './controllers/room.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [RoomController],
|
||||
providers: [RoomService],
|
||||
exports: [RoomService],
|
||||
})
|
||||
export class RoomModule {}
|
1
src/room/services/index.ts
Normal file
1
src/room/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './room.service';
|
99
src/room/services/room.service.ts
Normal file
99
src/room/services/room.service.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddRoomDto } from '../dtos';
|
||||
import {
|
||||
GetRoomDetailsInterface,
|
||||
GetRoomsIdsInterface,
|
||||
} from '../interfaces/get.room.interface';
|
||||
|
||||
@Injectable()
|
||||
export class RoomService {
|
||||
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 getRoomsByHomeId(homeId: string) {
|
||||
try {
|
||||
const roomsIds = await this.getRoomsIds(homeId);
|
||||
|
||||
const roomsDetails = await Promise.all(
|
||||
roomsIds.result.data.map(async (roomId) => {
|
||||
const roomData = await this.getRoomDetails(roomId);
|
||||
return {
|
||||
roomId: roomData?.result?.id,
|
||||
roomName: roomData ? roomData.result.name : null,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return roomsDetails;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching rooms',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getRoomsIds(homeId: string): Promise<GetRoomsIdsInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/child`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
query: { space_id: homeId },
|
||||
});
|
||||
return response as GetRoomsIdsInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching rooms ids',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getRoomDetails(roomId: string): Promise<GetRoomDetailsInterface> {
|
||||
// Added return type
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/${roomId}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
|
||||
return response as GetRoomDetailsInterface; // Cast response to RoomData
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching rooms details',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async addRoom(addRoomDto: AddRoomDto) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/creation`;
|
||||
const data = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: { name: addRoomDto.roomName, parent_id: addRoomDto.homeId },
|
||||
});
|
||||
|
||||
return {
|
||||
success: data.success,
|
||||
roomId: data.result,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding room',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user