updated root of endoint of space device from project

This commit is contained in:
hannathkadher
2024-12-09 17:47:26 +04:00
parent dade201752
commit dc9c67b6c0
2 changed files with 7 additions and 29 deletions

View File

@ -209,7 +209,7 @@ export class ControllerRoute {
static SPACE_DEVICES = class {
public static readonly ROUTE =
'/communities/:communityUuid/spaces/:spaceUuid/devices';
'/projects/:projectUuid/communities/:communityUuid/spaces/:spaceUuid/devices';
static ACTIONS = class {
public static readonly LIST_SPACE_DEVICE_SUMMARY =
'List devices in a space';

View File

@ -8,6 +8,7 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { SpaceService } from './space.service';
@Injectable()
export class SpaceDeviceService {
@ -16,14 +17,16 @@ export class SpaceDeviceService {
private readonly tuyaService: TuyaService,
private readonly productRepository: ProductRepository,
private readonly communityRepository: CommunityRepository,
private readonly spaceService: SpaceService,
) {}
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
const { spaceUuid, communityUuid } = params;
const { spaceUuid, communityUuid, projectUuid } = params;
try {
const space = await this.validateCommunityAndSpace(
const space = await this.spaceService.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const safeFetch = async (device: any) => {
@ -52,7 +55,7 @@ export class SpaceDeviceService {
const detailedDevices = await Promise.all(space.devices.map(safeFetch));
return new SuccessResponseDto({
data: detailedDevices.filter(Boolean), // Remove null or undefined values
data: detailedDevices.filter(Boolean),
message: 'Successfully retrieved list of devices',
});
} catch (error) {
@ -64,31 +67,6 @@ export class SpaceDeviceService {
}
}
async validateCommunityAndSpace(communityUuid: string, spaceUuid: string) {
const community = await this.communityRepository.findOne({
where: { uuid: communityUuid },
});
if (!community) {
this.throwNotFound('Community', communityUuid);
}
const space = await this.spaceRepository.findOne({
where: { uuid: spaceUuid, community: { uuid: communityUuid } },
relations: ['devices', 'devices.productDevice'],
});
if (!space) {
this.throwNotFound('Space', spaceUuid);
}
return space;
}
private throwNotFound(entity: string, uuid: string) {
throw new HttpException(
`${entity} with ID ${uuid} not found`,
HttpStatus.NOT_FOUND,
);
}
private async getDeviceDetailsByDeviceIdTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface> {