mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 12:54:53 +00:00
Add endpoint to get units by user ID
This commit is contained in:
@ -10,11 +10,12 @@ import {
|
|||||||
Put,
|
Put,
|
||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
|
ValidationPipe,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddUnitDto } from '../dtos/add.unit.dto';
|
import { AddUnitDto } from '../dtos/add.unit.dto';
|
||||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
import { GetUnitByUserIdDto, GetUnitChildDto } from '../dtos/get.unit.dto';
|
||||||
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||||
import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
|
import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
|
||||||
|
|
||||||
@ -88,6 +89,20 @@ export class UnitController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get()
|
||||||
|
async getUnitsByUserId(@Query(ValidationPipe) dto: GetUnitByUserIdDto) {
|
||||||
|
try {
|
||||||
|
return await this.unitService.getUnitsByUserId(dto.userUuid);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Put('rename/:unitUuid')
|
@Put('rename/:unitUuid')
|
||||||
|
|||||||
@ -49,3 +49,12 @@ export class GetUnitChildDto {
|
|||||||
})
|
})
|
||||||
public includeSubSpaces: boolean = false;
|
public includeSubSpaces: boolean = false;
|
||||||
}
|
}
|
||||||
|
export class GetUnitByUserIdDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
}
|
||||||
|
|||||||
@ -24,3 +24,8 @@ export interface RenameUnitByUuidInterface {
|
|||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
export interface GetUnitByUserUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
|||||||
@ -13,15 +13,18 @@ import {
|
|||||||
UnitParentInterface,
|
UnitParentInterface,
|
||||||
GetUnitByUuidInterface,
|
GetUnitByUuidInterface,
|
||||||
RenameUnitByUuidInterface,
|
RenameUnitByUuidInterface,
|
||||||
|
GetUnitByUserUuidInterface,
|
||||||
} from '../interface/unit.interface';
|
} from '../interface/unit.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||||
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UnitService {
|
export class UnitService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||||
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async addUnit(addUnitDto: AddUnitDto) {
|
async addUnit(addUnitDto: AddUnitDto) {
|
||||||
@ -195,7 +198,36 @@ export class UnitService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getUnitsByUserId(
|
||||||
|
userUuid: string,
|
||||||
|
): Promise<GetUnitByUserUuidInterface[]> {
|
||||||
|
try {
|
||||||
|
const units = await this.userSpaceRepository.find({
|
||||||
|
relations: ['space', 'space.spaceType'],
|
||||||
|
where: {
|
||||||
|
user: { uuid: userUuid },
|
||||||
|
space: { spaceType: { type: 'unit' } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (units.length === 0) {
|
||||||
|
throw new HttpException('this user has no units', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
const spaces = units.map((unit) => ({
|
||||||
|
uuid: unit.space.uuid,
|
||||||
|
name: unit.space.spaceName,
|
||||||
|
type: unit.space.spaceType.type,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return spaces;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async renameUnitByUuid(
|
async renameUnitByUuid(
|
||||||
unitUuid: string,
|
unitUuid: string,
|
||||||
updateUnitNameDto: UpdateUnitNameDto,
|
updateUnitNameDto: UpdateUnitNameDto,
|
||||||
|
|||||||
@ -6,11 +6,23 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
|||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
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({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
SpaceRepositoryModule,
|
||||||
|
SpaceTypeRepositoryModule,
|
||||||
|
UserSpaceRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [UnitController],
|
controllers: [UnitController],
|
||||||
providers: [UnitService, SpaceRepository, SpaceTypeRepository],
|
providers: [
|
||||||
|
UnitService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceTypeRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
],
|
||||||
exports: [UnitService],
|
exports: [UnitService],
|
||||||
})
|
})
|
||||||
export class UnitModule {}
|
export class UnitModule {}
|
||||||
|
|||||||
Reference in New Issue
Block a user