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

View File

@ -1,7 +1,6 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { AddCommunityDto } from '../dtos';
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
import { RegionRepository } from '@app/common/modules/region/repositories';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import {
TypeORMCustomModel,
@ -11,23 +10,25 @@ import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { CommunityDto } from '@app/common/modules/community/dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { TuyaService } from '@app/common/integrations/tuya/tuya.service';
@Injectable()
export class CommunityService {
constructor(
private readonly communityRepository: CommunityRepository,
private readonly regionRepository: RegionRepository,
private readonly tuyaService: TuyaService,
) {}
async createCommunity(dto: AddCommunityDto): Promise<BaseResponseDto> {
const { regionId, name, description } = dto;
// Find the region by the provided regionId
const region = await this.regionRepository.findOneBy({ uuid: regionId });
const { name, description } = dto;
if (!region) {
const existingCommunity = await this.communityRepository.findOneBy({
name,
});
if (existingCommunity) {
throw new HttpException(
`Region with ID ${dto.regionId} not found.`,
HttpStatus.NOT_FOUND,
`A community with the name '${name}' already exists.`,
HttpStatus.BAD_REQUEST,
);
}
@ -35,11 +36,12 @@ export class CommunityService {
const community = this.communityRepository.create({
name: name,
description: description,
region: region, // Associate with the found region
});
// Save the community to the database
try {
const externalId = await this.createTuyaSpace(name);
community.externalId = externalId;
await this.communityRepository.save(community);
return new SuccessResponseDto({
statusCode: HttpStatus.CREATED,
@ -52,13 +54,15 @@ export class CommunityService {
}
}
async getCommunityById(id: string): Promise<BaseResponseDto> {
const community = await this.communityRepository.findOneBy({ uuid: id });
async getCommunityById(communityUuid: string): Promise<BaseResponseDto> {
const community = await this.communityRepository.findOneBy({
uuid: communityUuid,
});
// If the community is not found, throw a 404 NotFoundException
if (!community) {
throw new HttpException(
`Community with ID ${id} not found.`,
`Community with ID ${communityUuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
@ -87,17 +91,17 @@ export class CommunityService {
}
async updateCommunity(
id: string,
communityUuid: string,
updateCommunityDto: UpdateCommunityNameDto,
): Promise<BaseResponseDto> {
const community = await this.communityRepository.findOne({
where: { uuid: id },
where: { uuid: communityUuid },
});
// If the community doesn't exist, throw a 404 error
if (!community) {
throw new HttpException(
`Community with ID ${id} not found`,
`Community with ID ${communityUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
@ -129,15 +133,15 @@ export class CommunityService {
}
}
async deleteCommunity(id: string): Promise<BaseResponseDto> {
async deleteCommunity(communityUuid: string): Promise<BaseResponseDto> {
const community = await this.communityRepository.findOne({
where: { uuid: id },
where: { uuid: communityUuid },
});
// If the community is not found, throw an error
if (!community) {
throw new HttpException(
`Community with ID ${id} not found`,
`Community with ID ${communityUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
@ -145,11 +149,10 @@ export class CommunityService {
// Find the community by its uuid
// Remove the community from the database
await this.communityRepository.remove(community);
// Return success response
return new SuccessResponseDto({
message: `Community with ID ${id} successfully deleted`,
message: `Community with ID ${communityUuid} successfully deleted`,
data: null,
});
} catch (err) {
@ -164,4 +167,16 @@ export class CommunityService {
}
}
}
private async createTuyaSpace(name: string): Promise<string> {
try {
const response = await this.tuyaService.createSpace({ name });
return response;
} catch (error) {
throw new HttpException(
'Failed to create a Tuya space',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}