mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:44:54 +00:00
Remove Home Module
This commit is contained in:
@ -5,7 +5,6 @@ import { SnakeNamingStrategy } from './strategies';
|
|||||||
import { UserEntity } from '../modules/user/entities/user.entity';
|
import { UserEntity } from '../modules/user/entities/user.entity';
|
||||||
import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
||||||
import { UserOtpEntity } from '../modules/user-otp/entities';
|
import { UserOtpEntity } from '../modules/user-otp/entities';
|
||||||
import { HomeEntity } from '../modules/home/entities';
|
|
||||||
import { ProductEntity } from '../modules/product/entities';
|
import { ProductEntity } from '../modules/product/entities';
|
||||||
import {
|
import {
|
||||||
DeviceEntity,
|
DeviceEntity,
|
||||||
@ -35,7 +34,6 @@ import { GroupDeviceEntity } from '../modules/group-device/entities';
|
|||||||
UserEntity,
|
UserEntity,
|
||||||
UserSessionEntity,
|
UserSessionEntity,
|
||||||
UserOtpEntity,
|
UserOtpEntity,
|
||||||
HomeEntity,
|
|
||||||
ProductEntity,
|
ProductEntity,
|
||||||
DeviceUserPermissionEntity,
|
DeviceUserPermissionEntity,
|
||||||
DeviceEntity,
|
DeviceEntity,
|
||||||
|
|||||||
@ -1,19 +0,0 @@
|
|||||||
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 +0,0 @@
|
|||||||
export * from './home.dto';
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
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 +0,0 @@
|
|||||||
export * from './home.entity';
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
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 {}
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
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 +0,0 @@
|
|||||||
export * from './home.repository';
|
|
||||||
@ -4,7 +4,6 @@ import config from './config';
|
|||||||
import { AuthenticationModule } from './auth/auth.module';
|
import { AuthenticationModule } from './auth/auth.module';
|
||||||
import { AuthenticationController } from './auth/controllers/authentication.controller';
|
import { AuthenticationController } from './auth/controllers/authentication.controller';
|
||||||
import { UserModule } from './users/user.module';
|
import { UserModule } from './users/user.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 { GroupModule } from './group/group.module';
|
||||||
import { DeviceModule } from './device/device.module';
|
import { DeviceModule } from './device/device.module';
|
||||||
@ -25,7 +24,6 @@ import { UnitModule } from './unit/unit.module';
|
|||||||
FloorModule,
|
FloorModule,
|
||||||
UnitModule,
|
UnitModule,
|
||||||
RoomModule,
|
RoomModule,
|
||||||
HomeModule,
|
|
||||||
RoomModule,
|
RoomModule,
|
||||||
GroupModule,
|
GroupModule,
|
||||||
DeviceModule,
|
DeviceModule,
|
||||||
|
|||||||
@ -1,54 +0,0 @@
|
|||||||
import { HomeService } from './../services/home.service';
|
|
||||||
import {
|
|
||||||
Body,
|
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
Post,
|
|
||||||
Param,
|
|
||||||
UseGuards,
|
|
||||||
Query,
|
|
||||||
} 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()
|
|
||||||
async getHomesByUserId(@Query('userUuid') userUuid: string) {
|
|
||||||
try {
|
|
||||||
return await this.homeService.getHomesByUserId(userUuid);
|
|
||||||
} catch (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()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Post()
|
|
||||||
async addHome(@Body() addHomeDto: AddHomeDto) {
|
|
||||||
try {
|
|
||||||
return await this.homeService.addHome(addHomeDto);
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1 +0,0 @@
|
|||||||
export * from './home.controller';
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
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 +0,0 @@
|
|||||||
export * from './add.home.dto';
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
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 {}
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
export class GetHomeDetailsInterface {
|
|
||||||
result: {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,113 +0,0 @@
|
|||||||
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';
|
|
||||||
import { GetHomeDetailsInterface } from '../interfaces/get.home.interface';
|
|
||||||
|
|
||||||
@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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 +0,0 @@
|
|||||||
export * from './home.service';
|
|
||||||
Reference in New Issue
Block a user