Compare commits

..

1 Commits

Author SHA1 Message Date
85d9a59848 fix: spaces structure in communities 2025-06-22 14:03:13 +03:00
4 changed files with 34 additions and 48 deletions

View File

@ -190,26 +190,25 @@ export class CommunityService {
.distinct(true); .distinct(true);
if (includeSpaces) { if (includeSpaces) {
qb.leftJoinAndSelect( qb.leftJoinAndSelect('c.spaces', 'space', 'space.disabled = false')
'c.spaces',
'space',
'space.disabled = :disabled AND space.spaceName != :orphanSpaceName',
{ disabled: false, orphanSpaceName: ORPHAN_SPACE_NAME },
)
.leftJoinAndSelect('space.parent', 'parent') .leftJoinAndSelect('space.parent', 'parent')
.leftJoinAndSelect( .leftJoinAndSelect(
'space.children', 'space.children',
'children', 'children',
'children.disabled = :disabled', 'children.disabled = :disabled',
{ disabled: false }, { disabled: false },
); )
// .leftJoinAndSelect('space.spaceModel', 'spaceModel') // .leftJoinAndSelect('space.spaceModel', 'spaceModel')
.andWhere('space.spaceName != :orphanSpaceName', {
orphanSpaceName: ORPHAN_SPACE_NAME,
})
.andWhere('space.disabled = :disabled', { disabled: false });
} }
if (search) { if (search) {
qb.andWhere( qb.andWhere(
`c.name ILIKE :search ${includeSpaces ? 'OR space.space_name ILIKE :search' : ''}`, `c.name ILIKE :search ${includeSpaces ? 'OR space.space_name ILIKE :search' : ''}`,
{ search }, { search: `%${search}%` },
); );
} }
@ -217,6 +216,7 @@ export class CommunityService {
const { baseResponseDto, paginationResponseDto } = const { baseResponseDto, paginationResponseDto } =
await customModel.findAll({ ...pageable, modelName: 'community' }, qb); await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
if (includeSpaces) { if (includeSpaces) {
baseResponseDto.data = baseResponseDto.data.map((community) => ({ baseResponseDto.data = baseResponseDto.data.map((community) => ({
...community, ...community,

View File

@ -1,11 +1,11 @@
import { ControllerRoute } from '@app/common/constants/controller-route';
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Permissions } from 'src/decorators/permissions.decorator';
import { PermissionsGuard } from 'src/guards/permissions.guard';
import { GetDevicesFilterDto, ProjectParam } from '../dtos';
import { DeviceService } from '../services/device.service'; import { DeviceService } from '../services/device.service';
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
import { ControllerRoute } from '@app/common/constants/controller-route';
import { PermissionsGuard } from 'src/guards/permissions.guard';
import { Permissions } from 'src/decorators/permissions.decorator';
import { GetDoorLockDevices, ProjectParam } from '../dtos';
@ApiTags('Device Module') @ApiTags('Device Module')
@Controller({ @Controller({
@ -25,7 +25,7 @@ export class DeviceProjectController {
}) })
async getAllDevices( async getAllDevices(
@Param() param: ProjectParam, @Param() param: ProjectParam,
@Query() query: GetDevicesFilterDto, @Query() query: GetDoorLockDevices,
) { ) {
return await this.deviceService.getAllDevices(param, query); return await this.deviceService.getAllDevices(param, query);
} }

View File

@ -1,7 +1,6 @@
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum'; import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { import {
IsArray,
IsEnum, IsEnum,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional,
@ -42,7 +41,16 @@ export class GetDeviceLogsDto {
@IsOptional() @IsOptional()
public endTime: string; public endTime: string;
} }
export class GetDoorLockDevices {
@ApiProperty({
description: 'Device Type',
enum: DeviceTypeEnum,
required: false,
})
@IsEnum(DeviceTypeEnum)
@IsOptional()
public deviceType: DeviceTypeEnum;
}
export class GetDevicesBySpaceOrCommunityDto { export class GetDevicesBySpaceOrCommunityDto {
@ApiProperty({ @ApiProperty({
description: 'Device Product Type', description: 'Device Product Type',
@ -64,23 +72,3 @@ export class GetDevicesBySpaceOrCommunityDto {
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' }) @IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
requireEither?: never; // This ensures at least one of them is provided requireEither?: never; // This ensures at least one of them is provided
} }
export class GetDevicesFilterDto {
@ApiProperty({
description: 'Device Type',
enum: DeviceTypeEnum,
required: false,
})
@IsEnum(DeviceTypeEnum)
@IsOptional()
public deviceType: DeviceTypeEnum;
@ApiProperty({
description: 'List of Space IDs to filter devices',
required: false,
example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'],
})
@IsOptional()
@IsArray()
@IsUUID('4', { each: true })
public spaces?: string[];
}

View File

@ -53,7 +53,7 @@ import { DeviceSceneParamDto } from '../dtos/device.param.dto';
import { import {
GetDeviceLogsDto, GetDeviceLogsDto,
GetDevicesBySpaceOrCommunityDto, GetDevicesBySpaceOrCommunityDto,
GetDevicesFilterDto, GetDoorLockDevices,
} from '../dtos/get.device.dto'; } from '../dtos/get.device.dto';
import { import {
controlDeviceInterface, controlDeviceInterface,
@ -955,20 +955,19 @@ export class DeviceService {
async getAllDevices( async getAllDevices(
param: ProjectParam, param: ProjectParam,
{ deviceType, spaces }: GetDevicesFilterDto, query: GetDoorLockDevices,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
try { try {
await this.validateProject(param.projectUuid); await this.validateProject(param.projectUuid);
if (deviceType === DeviceTypeEnum.DOOR_LOCK) { if (query.deviceType === DeviceTypeEnum.DOOR_LOCK) {
return await this.getDoorLockDevices(param.projectUuid, spaces); return await this.getDoorLockDevices(param.projectUuid);
} else if (!deviceType) { } else if (!query.deviceType) {
const devices = await this.deviceRepository.find({ const devices = await this.deviceRepository.find({
where: { where: {
isActive: true, isActive: true,
spaceDevice: { spaceDevice: {
uuid: spaces && spaces.length ? In(spaces) : undefined,
spaceName: Not(ORPHAN_SPACE_NAME),
community: { project: { uuid: param.projectUuid } }, community: { project: { uuid: param.projectUuid } },
spaceName: Not(ORPHAN_SPACE_NAME),
}, },
}, },
relations: [ relations: [
@ -1564,7 +1563,7 @@ export class DeviceService {
} }
} }
async getDoorLockDevices(projectUuid: string, spaces?: string[]) { async getDoorLockDevices(projectUuid: string) {
await this.validateProject(projectUuid); await this.validateProject(projectUuid);
const devices = await this.deviceRepository.find({ const devices = await this.deviceRepository.find({
@ -1574,7 +1573,6 @@ export class DeviceService {
}, },
spaceDevice: { spaceDevice: {
spaceName: Not(ORPHAN_SPACE_NAME), spaceName: Not(ORPHAN_SPACE_NAME),
uuid: spaces && spaces.length ? In(spaces) : undefined,
community: { community: {
project: { project: {
uuid: projectUuid, uuid: projectUuid,