changed endpoint for subspace device

This commit is contained in:
hannathkadher
2024-12-09 17:56:18 +04:00
parent 30c2294224
commit e61132561b
3 changed files with 23 additions and 29 deletions

View File

@ -12,6 +12,7 @@ import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { GetDeviceDetailsInterface } from '../../../device/interfaces/get.device.interface';
import { SpaceService } from '../space.service';
@Injectable()
export class SubspaceDeviceService {
@ -22,14 +23,19 @@ export class SubspaceDeviceService {
private readonly deviceRepository: DeviceRepository,
private readonly tuyaService: TuyaService,
private readonly productRepository: ProductRepository,
private readonly spaceService: SpaceService,
) {}
async listDevicesInSubspace(
params: GetSubSpaceParam,
): Promise<BaseResponseDto> {
const { subSpaceUuid, spaceUuid, communityUuid } = params;
const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
await this.spaceService.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const subspace = await this.findSubspaceWithDevices(subSpaceUuid);
@ -67,9 +73,14 @@ export class SubspaceDeviceService {
async associateDeviceToSubspace(
params: DeviceSubSpaceParam,
): Promise<BaseResponseDto> {
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid } = params;
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
params;
try {
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
await this.spaceService.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const subspace = await this.findSubspace(subSpaceUuid);
const device = await this.findDevice(deviceUuid);
@ -97,9 +108,14 @@ export class SubspaceDeviceService {
async disassociateDeviceFromSubspace(
params: DeviceSubSpaceParam,
): Promise<BaseResponseDto> {
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid } = params;
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
params;
try {
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
await this.spaceService.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const subspace = await this.findSubspace(subSpaceUuid);
const device = await this.findDevice(deviceUuid);
@ -129,26 +145,6 @@ export class SubspaceDeviceService {
}
}
}
// Helper method to validate community and space
private 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 } },
});
if (!space) {
this.throwNotFound('Space', spaceUuid);
}
return space;
}
// Helper method to find subspace with devices relation
private async findSubspaceWithDevices(subSpaceUuid: string) {

View File

@ -18,8 +18,6 @@ import { SpaceService } from '../space.service';
@Injectable()
export class SubSpaceService {
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly communityRepository: CommunityRepository,
private readonly subspaceRepository: SubspaceRepository,
private readonly spaceService: SpaceService,
) {}