mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 18:04:54 +00:00
Add endpoint to get units by user ID
This commit is contained in:
@ -13,15 +13,18 @@ import {
|
||||
UnitParentInterface,
|
||||
GetUnitByUuidInterface,
|
||||
RenameUnitByUuidInterface,
|
||||
GetUnitByUserUuidInterface,
|
||||
} from '../interface/unit.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class UnitService {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
) {}
|
||||
|
||||
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(
|
||||
unitUuid: string,
|
||||
updateUnitNameDto: UpdateUnitNameDto,
|
||||
|
||||
Reference in New Issue
Block a user