mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 11:15:14 +00:00
Add GetFloorsByUserId endpoint and related functionality
This commit is contained in:
@ -10,11 +10,12 @@ import {
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddFloorDto } from '../dtos/add.floor.dto';
|
||||
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||
import { GetFloorByUserIdDto, GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||
import { CheckBuildingTypeGuard } from 'src/guards/building.type.guard';
|
||||
|
||||
@ -91,6 +92,20 @@ export class FloorController {
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
async getFloorsByUserId(@Query(ValidationPipe) dto: GetFloorByUserIdDto) {
|
||||
try {
|
||||
return await this.floorService.getFloorsByUserId(dto.userUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('rename/:floorUuid')
|
||||
|
@ -49,3 +49,13 @@ export class GetFloorChildDto {
|
||||
})
|
||||
public includeSubSpaces: boolean = false;
|
||||
}
|
||||
|
||||
export class GetFloorByUserIdDto {
|
||||
@ApiProperty({
|
||||
description: 'userUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
}
|
||||
|
@ -6,11 +6,23 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||
import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.space.repository.module';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||
imports: [
|
||||
ConfigModule,
|
||||
SpaceRepositoryModule,
|
||||
SpaceTypeRepositoryModule,
|
||||
UserSpaceRepositoryModule,
|
||||
],
|
||||
controllers: [FloorController],
|
||||
providers: [FloorService, SpaceRepository, SpaceTypeRepository],
|
||||
providers: [
|
||||
FloorService,
|
||||
SpaceRepository,
|
||||
SpaceTypeRepository,
|
||||
UserSpaceRepository,
|
||||
],
|
||||
exports: [FloorService],
|
||||
})
|
||||
export class FloorModule {}
|
||||
|
@ -24,3 +24,9 @@ export interface RenameFloorByUuidInterface {
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface GetFloorByUserUuidInterface {
|
||||
uuid: string;
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
@ -11,17 +11,20 @@ import { AddFloorDto } from '../dtos';
|
||||
import {
|
||||
FloorChildInterface,
|
||||
FloorParentInterface,
|
||||
GetFloorByUserUuidInterface,
|
||||
GetFloorByUuidInterface,
|
||||
RenameFloorByUuidInterface,
|
||||
} from '../interface/floor.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class FloorService {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
) {}
|
||||
|
||||
async addFloor(addFloorDto: AddFloorDto) {
|
||||
@ -195,6 +198,40 @@ export class FloorService {
|
||||
}
|
||||
}
|
||||
|
||||
async getFloorsByUserId(
|
||||
userUuid: string,
|
||||
): Promise<GetFloorByUserUuidInterface[]> {
|
||||
try {
|
||||
const floors = await this.userSpaceRepository.find({
|
||||
relations: ['space', 'space.spaceType'],
|
||||
where: {
|
||||
user: { uuid: userUuid },
|
||||
space: { spaceType: { type: 'floor' } },
|
||||
},
|
||||
});
|
||||
|
||||
if (floors.length === 0) {
|
||||
throw new HttpException(
|
||||
'this user has no floors',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
const spaces = floors.map((floor) => ({
|
||||
uuid: floor.space.uuid,
|
||||
name: floor.space.spaceName,
|
||||
type: floor.space.spaceType.type,
|
||||
}));
|
||||
|
||||
return spaces;
|
||||
} catch (err) {
|
||||
if (err instanceof HttpException) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async renameFloorByUuid(
|
||||
floorUuid: string,
|
||||
updateFloorDto: UpdateFloorNameDto,
|
||||
|
Reference in New Issue
Block a user