mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
reduced duplication
This commit is contained in:
@ -1,33 +1,29 @@
|
|||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
|
import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
import { GetSpaceParam } from '../dtos';
|
import { GetSpaceParam } from '../dtos';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
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 { SpaceService } from './space.service';
|
import { ValidationService } from './space-validation.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceDeviceService {
|
export class SpaceDeviceService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
|
||||||
private readonly tuyaService: TuyaService,
|
private readonly tuyaService: TuyaService,
|
||||||
private readonly productRepository: ProductRepository,
|
private readonly validationService: ValidationService,
|
||||||
private readonly communityRepository: CommunityRepository,
|
|
||||||
private readonly spaceService: SpaceService,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||||
const { spaceUuid, communityUuid, projectUuid } = params;
|
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||||
try {
|
try {
|
||||||
const space = await this.spaceService.validateCommunityAndSpace(
|
const space =
|
||||||
communityUuid,
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
spaceUuid,
|
communityUuid,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
);
|
spaceUuid,
|
||||||
|
);
|
||||||
|
|
||||||
const safeFetch = async (device: any) => {
|
const safeFetch = async (device: any) => {
|
||||||
try {
|
try {
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { GetSpaceParam } from '../dtos';
|
import { GetSpaceParam } from '../dtos';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { SpaceService } from './space.service';
|
|
||||||
import { SceneService } from '../../scene/services';
|
import { SceneService } from '../../scene/services';
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
import { GetSceneDto } from '../../scene/dtos';
|
import { GetSceneDto } from '../../scene/dtos';
|
||||||
|
import { ValidationService } from './space-validation.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceSceneService {
|
export class SpaceSceneService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceSevice: SpaceService,
|
|
||||||
private readonly sceneSevice: SceneService,
|
private readonly sceneSevice: SceneService,
|
||||||
|
private readonly validationService: ValidationService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getScenes(
|
async getScenes(
|
||||||
@ -20,10 +20,10 @@ export class SpaceSceneService {
|
|||||||
try {
|
try {
|
||||||
const { spaceUuid, communityUuid, projectUuid } = params;
|
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||||
|
|
||||||
await this.spaceSevice.validateCommunityAndSpace(
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
spaceUuid,
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
spaceUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const scenes = await this.sceneSevice.findScenesBySpace(
|
const scenes = await this.sceneSevice.findScenesBySpace(
|
||||||
|
@ -1,21 +1,19 @@
|
|||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
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 { SpaceRepository } from '@app/common/modules/space/repositories';
|
|
||||||
import {
|
import {
|
||||||
UserRepository,
|
UserRepository,
|
||||||
UserSpaceRepository,
|
UserSpaceRepository,
|
||||||
} from '@app/common/modules/user/repositories';
|
} from '@app/common/modules/user/repositories';
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { SpaceService } from './space.service';
|
|
||||||
import { UserSpaceParam } from '../dtos';
|
import { UserSpaceParam } from '../dtos';
|
||||||
|
import { ValidationService } from './space-validation.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceUserService {
|
export class SpaceUserService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly validationService: ValidationService,
|
||||||
private readonly userRepository: UserRepository,
|
private readonly userRepository: UserRepository,
|
||||||
private readonly userSpaceRepository: UserSpaceRepository,
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
private readonly spaceService: SpaceService,
|
|
||||||
) {}
|
) {}
|
||||||
async associateUserToSpace(params: UserSpaceParam): Promise<BaseResponseDto> {
|
async associateUserToSpace(params: UserSpaceParam): Promise<BaseResponseDto> {
|
||||||
const { communityUuid, spaceUuid, userUuid, projectUuid } = params;
|
const { communityUuid, spaceUuid, userUuid, projectUuid } = params;
|
||||||
@ -31,11 +29,12 @@ export class SpaceUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the space by ID
|
// Find the space by ID
|
||||||
const space = await this.spaceService.validateCommunityAndSpace(
|
const space =
|
||||||
communityUuid,
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
spaceUuid,
|
communityUuid,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
);
|
spaceUuid,
|
||||||
|
);
|
||||||
|
|
||||||
// Check if the association already exists
|
// Check if the association already exists
|
||||||
const existingAssociation = await this.userSpaceRepository.findOne({
|
const existingAssociation = await this.userSpaceRepository.findOne({
|
||||||
@ -73,10 +72,10 @@ export class SpaceUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the space by ID
|
// Find the space by ID
|
||||||
await this.spaceService.validateCommunityAndSpace(
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
spaceUuid,
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
spaceUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Find the existing association
|
// Find the existing association
|
||||||
|
@ -1,29 +1,23 @@
|
|||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
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 { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import {
|
import { SubspaceRepository } from '@app/common/modules/space/repositories';
|
||||||
SpaceRepository,
|
|
||||||
SubspaceRepository,
|
|
||||||
} from '@app/common/modules/space/repositories';
|
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { DeviceSubSpaceParam, GetSubSpaceParam } from '../../dtos';
|
import { DeviceSubSpaceParam, GetSubSpaceParam } from '../../dtos';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
import { GetDeviceDetailsInterface } from '../../../device/interfaces/get.device.interface';
|
import { GetDeviceDetailsInterface } from '../../../device/interfaces/get.device.interface';
|
||||||
import { SpaceService } from '../space.service';
|
import { ValidationService } from '../space-validation.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SubspaceDeviceService {
|
export class SubspaceDeviceService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
|
||||||
private readonly communityRepository: CommunityRepository,
|
|
||||||
private readonly subspaceRepository: SubspaceRepository,
|
private readonly subspaceRepository: SubspaceRepository,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
private readonly tuyaService: TuyaService,
|
private readonly tuyaService: TuyaService,
|
||||||
private readonly productRepository: ProductRepository,
|
private readonly productRepository: ProductRepository,
|
||||||
private readonly spaceService: SpaceService,
|
private readonly validationService: ValidationService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async listDevicesInSubspace(
|
async listDevicesInSubspace(
|
||||||
@ -31,10 +25,10 @@ export class SubspaceDeviceService {
|
|||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params;
|
const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params;
|
||||||
|
|
||||||
await this.spaceService.validateCommunityAndSpace(
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
spaceUuid,
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
spaceUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const subspace = await this.findSubspaceWithDevices(subSpaceUuid);
|
const subspace = await this.findSubspaceWithDevices(subSpaceUuid);
|
||||||
@ -76,10 +70,10 @@ export class SubspaceDeviceService {
|
|||||||
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
|
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
|
||||||
params;
|
params;
|
||||||
try {
|
try {
|
||||||
await this.spaceService.validateCommunityAndSpace(
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
spaceUuid,
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
spaceUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const subspace = await this.findSubspace(subSpaceUuid);
|
const subspace = await this.findSubspace(subSpaceUuid);
|
||||||
@ -111,10 +105,10 @@ export class SubspaceDeviceService {
|
|||||||
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
|
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
|
||||||
params;
|
params;
|
||||||
try {
|
try {
|
||||||
await this.spaceService.validateCommunityAndSpace(
|
await this.validationService.validateSpaceWithinCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
spaceUuid,
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
spaceUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const subspace = await this.findSubspace(subSpaceUuid);
|
const subspace = await this.findSubspace(subSpaceUuid);
|
||||||
|
Reference in New Issue
Block a user