mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 12:24:54 +00:00
updated root of endoint of space device from project
This commit is contained in:
@ -209,7 +209,7 @@ export class ControllerRoute {
|
|||||||
|
|
||||||
static SPACE_DEVICES = class {
|
static SPACE_DEVICES = class {
|
||||||
public static readonly ROUTE =
|
public static readonly ROUTE =
|
||||||
'/communities/:communityUuid/spaces/:spaceUuid/devices';
|
'/projects/:projectUuid/communities/:communityUuid/spaces/:spaceUuid/devices';
|
||||||
static ACTIONS = class {
|
static ACTIONS = class {
|
||||||
public static readonly LIST_SPACE_DEVICE_SUMMARY =
|
public static readonly LIST_SPACE_DEVICE_SUMMARY =
|
||||||
'List devices in a space';
|
'List devices in a space';
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
|
import { SpaceService } from './space.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceDeviceService {
|
export class SpaceDeviceService {
|
||||||
@ -16,14 +17,16 @@ export class SpaceDeviceService {
|
|||||||
private readonly tuyaService: TuyaService,
|
private readonly tuyaService: TuyaService,
|
||||||
private readonly productRepository: ProductRepository,
|
private readonly productRepository: ProductRepository,
|
||||||
private readonly communityRepository: CommunityRepository,
|
private readonly communityRepository: CommunityRepository,
|
||||||
|
private readonly spaceService: SpaceService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||||
const { spaceUuid, communityUuid } = params;
|
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||||
try {
|
try {
|
||||||
const space = await this.validateCommunityAndSpace(
|
const space = await this.spaceService.validateCommunityAndSpace(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
spaceUuid,
|
spaceUuid,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const safeFetch = async (device: any) => {
|
const safeFetch = async (device: any) => {
|
||||||
@ -52,7 +55,7 @@ export class SpaceDeviceService {
|
|||||||
const detailedDevices = await Promise.all(space.devices.map(safeFetch));
|
const detailedDevices = await Promise.all(space.devices.map(safeFetch));
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
data: detailedDevices.filter(Boolean), // Remove null or undefined values
|
data: detailedDevices.filter(Boolean),
|
||||||
message: 'Successfully retrieved list of devices',
|
message: 'Successfully retrieved list of devices',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} 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(
|
private async getDeviceDetailsByDeviceIdTuya(
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
): Promise<GetDeviceDetailsInterface> {
|
): Promise<GetDeviceDetailsInterface> {
|
||||||
|
|||||||
Reference in New Issue
Block a user