Added tuya space to community

This commit is contained in:
hannathkadher
2024-10-29 14:24:01 +04:00
parent 00ed2c5bbf
commit d6e6eb9b1f
8 changed files with 82 additions and 46 deletions
+3 -1
View File
@@ -7,10 +7,12 @@ import { ConfigModule } from '@nestjs/config';
import config from './config';
import { EmailService } from './util/email.service';
import { ErrorMessageService } from 'src/error-message/error-message.service';
import { TuyaService } from './integrations/tuya/tuya.service';
@Module({
providers: [CommonService, EmailService, ErrorMessageService],
providers: [CommonService, EmailService, ErrorMessageService, TuyaService],
exports: [
CommonService,
TuyaService,
HelperModule,
AuthModule,
EmailService,
@@ -13,10 +13,10 @@ export class ControllerRoute {
public static readonly ROUTE = 'community';
static ACTIONS = class {
public static readonly GET_COMMUNITY_BY_ID_SUMMARY =
'Get community by community id';
'Get community by community community uuid';
public static readonly GET_COMMUNITY_BY_ID_DESCRIPTION =
'Get community by community id - ( [a-zA-Z0-9]{10} )';
'Get community by community community uuid - ( [a-zA-Z0-9]{10} )';
public static readonly LIST_COMMUNITY_SUMMARY = 'Get list of community';
@@ -40,7 +40,7 @@ export class ControllerRoute {
};
static COMMUNITY_SPACE = class {
public static readonly ROUTE = 'community/:id/spaces';
public static readonly ROUTE = 'community/:communityUuid/space';
static ACTIONS = class {
public static readonly GET_COMMUNITY_SPACES_HIERARCHY_SUMMARY =
'Fetch hierarchical structure of spaces within a community.';
@@ -0,0 +1,38 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
@Injectable()
export class TuyaService {
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 tuyaEuUrl = this.configService.get<string>('tuya-config.TUYA_EU_URL');
this.tuya = new TuyaContext({
baseUrl: tuyaEuUrl,
accessKey,
secretKey,
});
}
async createSpace({ name }: { name: string }) {
const path = '/v2.0/cloud/space/creation';
const body = { name };
const response = await this.tuya.request({
method: 'POST',
path,
body,
});
if (response.success) {
return response.result as string;
} else {
throw new HttpException(
'Error creating space in Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
@@ -1,7 +1,6 @@
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
import { Column, Entity, OneToMany, Unique } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { CommunityDto } from '../dtos';
import { RegionEntity } from '../../region/entities';
import { SpaceEntity } from '../../space/entities';
@Entity({ name: 'community' })
@@ -23,12 +22,6 @@ export class CommunityEntity extends AbstractEntity<CommunityDto> {
@Column({ length: 255, nullable: true })
description: string;
@ManyToOne(() => RegionEntity, (region) => region.communities, {
nullable: false,
onDelete: 'CASCADE',
})
region: RegionEntity;
@OneToMany(() => SpaceEntity, (space) => space.community)
spaces: SpaceEntity[];
@@ -2,7 +2,6 @@ import { Column, Entity, OneToMany } from 'typeorm';
import { RegionDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { UserEntity } from '../../user/entities';
import { CommunityEntity } from '../../community/entities';
@Entity({ name: 'region' })
export class RegionEntity extends AbstractEntity<RegionDto> {
@@ -14,9 +13,6 @@ export class RegionEntity extends AbstractEntity<RegionDto> {
@OneToMany(() => UserEntity, (user) => user.region)
users: UserEntity[];
@OneToMany(() => CommunityEntity, (community) => community.region)
communities: CommunityEntity[];
constructor(partial: Partial<RegionEntity>) {
super();
Object.assign(this, partial);