mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
Merge pull request #24 from SyncrowIOT/SP-171-be-create-groups-to-control-devices-per-group
Sp 171 be create groups to control devices per group
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import { PassportModule } from '@nestjs/passport';
|
import { PassportModule } from '@nestjs/passport';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { HelperModule } from '../helper/helper.module';
|
import { HelperModule } from '../helper/helper.module';
|
||||||
import { JwtStrategy } from './strategies/jwt.strategy';
|
import { JwtStrategy } from './strategies/jwt.strategy';
|
||||||
|
@ -7,10 +7,16 @@ import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
|||||||
import { UserOtpEntity } from '../modules/user-otp/entities';
|
import { UserOtpEntity } from '../modules/user-otp/entities';
|
||||||
import { HomeEntity } from '../modules/home/entities';
|
import { HomeEntity } from '../modules/home/entities';
|
||||||
import { ProductEntity } from '../modules/product/entities';
|
import { ProductEntity } from '../modules/product/entities';
|
||||||
import { DeviceEntity, DeviceUserPermissionEntity } from '../modules/device/entities';
|
import {
|
||||||
|
DeviceEntity,
|
||||||
|
DeviceUserPermissionEntity,
|
||||||
|
} from '../modules/device/entities';
|
||||||
import { PermissionTypeEntity } from '../modules/permission/entities';
|
import { PermissionTypeEntity } from '../modules/permission/entities';
|
||||||
import { SpaceEntity } from '../modules/space/entities';
|
import { SpaceEntity } from '../modules/space/entities';
|
||||||
import { SpaceTypeEntity } from '../modules/space-type/entities';
|
import { SpaceTypeEntity } from '../modules/space-type/entities';
|
||||||
|
import { UserSpaceEntity } from '../modules/user-space/entities';
|
||||||
|
import { GroupEntity } from '../modules/group/entities';
|
||||||
|
import { GroupDeviceEntity } from '../modules/group-device/entities';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -36,6 +42,9 @@ import { SpaceTypeEntity } from '../modules/space-type/entities';
|
|||||||
PermissionTypeEntity,
|
PermissionTypeEntity,
|
||||||
SpaceEntity,
|
SpaceEntity,
|
||||||
SpaceTypeEntity,
|
SpaceTypeEntity,
|
||||||
|
UserSpaceEntity,
|
||||||
|
GroupEntity,
|
||||||
|
GroupDeviceEntity,
|
||||||
],
|
],
|
||||||
namingStrategy: new SnakeNamingStrategy(),
|
namingStrategy: new SnakeNamingStrategy(),
|
||||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||||
|
@ -1,20 +1,19 @@
|
|||||||
import { IsNotEmpty, IsString } from "class-validator";
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class DeviceDto{
|
export class DeviceDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public uuid: string;
|
spaceUuid: string;
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
spaceUuid:string;
|
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
deviceTuyaUuid:string;
|
deviceTuyaUuid: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
productUuid:string;
|
productUuid: string;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import { Column, Entity, OneToMany } from 'typeorm';
|
|||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { DeviceDto } from '../dtos/device.dto';
|
import { DeviceDto } from '../dtos/device.dto';
|
||||||
import { DeviceUserPermissionEntity } from './device-user-type.entity';
|
import { DeviceUserPermissionEntity } from './device-user-type.entity';
|
||||||
|
import { GroupDeviceEntity } from '../../group-device/entities';
|
||||||
|
|
||||||
@Entity({ name: 'device' })
|
@Entity({ name: 'device' })
|
||||||
export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||||
@ -37,6 +38,12 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
)
|
)
|
||||||
permission: DeviceUserPermissionEntity[];
|
permission: DeviceUserPermissionEntity[];
|
||||||
|
|
||||||
|
@OneToMany(
|
||||||
|
() => GroupDeviceEntity,
|
||||||
|
(userGroupDevices) => userGroupDevices.device,
|
||||||
|
)
|
||||||
|
userGroupDevices: GroupDeviceEntity[];
|
||||||
|
|
||||||
constructor(partial: Partial<DeviceEntity>) {
|
constructor(partial: Partial<DeviceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class GroupDeviceDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public deviceUuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public groupUuid: string;
|
||||||
|
}
|
1
libs/common/src/modules/group-device/dtos/index.ts
Normal file
1
libs/common/src/modules/group-device/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.device.dto';
|
@ -0,0 +1,36 @@
|
|||||||
|
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||||
|
import { GroupDeviceDto } from '../dtos';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { DeviceEntity } from '../../device/entities';
|
||||||
|
import { GroupEntity } from '../../group/entities';
|
||||||
|
|
||||||
|
@Entity({ name: 'group-device' })
|
||||||
|
@Unique(['device', 'group'])
|
||||||
|
export class GroupDeviceEntity extends AbstractEntity<GroupDeviceDto> {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => DeviceEntity, (device) => device.userGroupDevices, {
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
device: DeviceEntity;
|
||||||
|
|
||||||
|
@ManyToOne(() => GroupEntity, (group) => group.groupDevices, {
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
group: GroupEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
default: true,
|
||||||
|
})
|
||||||
|
public isActive: boolean;
|
||||||
|
constructor(partial: Partial<GroupDeviceEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
1
libs/common/src/modules/group-device/entities/index.ts
Normal file
1
libs/common/src/modules/group-device/entities/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.device.entity';
|
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { GroupDeviceEntity } from './entities/group.device.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([GroupDeviceEntity])],
|
||||||
|
})
|
||||||
|
export class GroupDeviceRepositoryModule {}
|
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { GroupDeviceEntity } from '../entities/group.device.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class GroupDeviceRepository extends Repository<GroupDeviceEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(GroupDeviceEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
export * from './group.device.repository';
|
11
libs/common/src/modules/group/dtos/group.dto.ts
Normal file
11
libs/common/src/modules/group/dtos/group.dto.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class GroupDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public groupName: string;
|
||||||
|
}
|
1
libs/common/src/modules/group/dtos/index.ts
Normal file
1
libs/common/src/modules/group/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.dto';
|
35
libs/common/src/modules/group/entities/group.entity.ts
Normal file
35
libs/common/src/modules/group/entities/group.entity.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { Column, Entity, OneToMany } from 'typeorm';
|
||||||
|
import { GroupDto } from '../dtos';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { GroupDeviceEntity } from '../../group-device/entities';
|
||||||
|
|
||||||
|
@Entity({ name: 'group' })
|
||||||
|
export class GroupEntity extends AbstractEntity<GroupDto> {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public groupName: string;
|
||||||
|
|
||||||
|
@OneToMany(() => GroupDeviceEntity, (groupDevice) => groupDevice.group, {
|
||||||
|
cascade: true,
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
})
|
||||||
|
groupDevices: GroupDeviceEntity[];
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
default: true,
|
||||||
|
})
|
||||||
|
public isActive: boolean;
|
||||||
|
constructor(partial: Partial<GroupEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
1
libs/common/src/modules/group/entities/index.ts
Normal file
1
libs/common/src/modules/group/entities/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.entity';
|
11
libs/common/src/modules/group/group.repository.module.ts
Normal file
11
libs/common/src/modules/group/group.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { GroupEntity } from './entities/group.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([GroupEntity])],
|
||||||
|
})
|
||||||
|
export class GroupRepositoryModule {}
|
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { GroupEntity } from '../entities/group.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class GroupRepository extends Repository<GroupEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(GroupEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
1
libs/common/src/modules/group/repositories/index.ts
Normal file
1
libs/common/src/modules/group/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './group.repository';
|
@ -1 +1 @@
|
|||||||
export * from './permission.dto'
|
export * from './permission.dto';
|
||||||
|
@ -1 +1 @@
|
|||||||
export * from './permission.entity'
|
export * from './permission.entity';
|
||||||
|
@ -1 +1 @@
|
|||||||
export * from './permission.repository'
|
export * from './permission.repository';
|
||||||
|
@ -2,6 +2,7 @@ import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
|||||||
import { SpaceDto } from '../dtos';
|
import { SpaceDto } from '../dtos';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { SpaceTypeEntity } from '../../space-type/entities';
|
import { SpaceTypeEntity } from '../../space-type/entities';
|
||||||
|
import { UserSpaceEntity } from '../../user-space/entities';
|
||||||
|
|
||||||
@Entity({ name: 'space' })
|
@Entity({ name: 'space' })
|
||||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||||
@ -26,6 +27,9 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
})
|
})
|
||||||
spaceType: SpaceTypeEntity;
|
spaceType: SpaceTypeEntity;
|
||||||
|
|
||||||
|
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.space)
|
||||||
|
userSpaces: UserSpaceEntity[];
|
||||||
|
|
||||||
constructor(partial: Partial<SpaceEntity>) {
|
constructor(partial: Partial<SpaceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
1
libs/common/src/modules/user-space/dtos/index.ts
Normal file
1
libs/common/src/modules/user-space/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './user.space.dto';
|
15
libs/common/src/modules/user-space/dtos/user.space.dto.ts
Normal file
15
libs/common/src/modules/user-space/dtos/user.space.dto.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class UserSpaceDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public spaceUuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
}
|
1
libs/common/src/modules/user-space/entities/index.ts
Normal file
1
libs/common/src/modules/user-space/entities/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './user.space.entity';
|
@ -0,0 +1,29 @@
|
|||||||
|
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||||
|
import { UserSpaceDto } from '../dtos';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { SpaceEntity } from '../../space/entities';
|
||||||
|
import { UserEntity } from '../../user/entities';
|
||||||
|
|
||||||
|
@Entity({ name: 'user-space' })
|
||||||
|
@Unique(['user', 'space'])
|
||||||
|
export class UserSpaceEntity extends AbstractEntity<UserSpaceDto> {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: false })
|
||||||
|
user: UserEntity;
|
||||||
|
|
||||||
|
@ManyToOne(() => SpaceEntity, (space) => space.userSpaces, {
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
space: SpaceEntity;
|
||||||
|
|
||||||
|
constructor(partial: Partial<UserSpaceEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
1
libs/common/src/modules/user-space/repositories/index.ts
Normal file
1
libs/common/src/modules/user-space/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './user.space.repository';
|
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { UserSpaceEntity } from '../entities/user.space.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UserSpaceRepository extends Repository<UserSpaceEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(UserSpaceEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { UserSpaceEntity } from './entities/user.space.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([UserSpaceEntity])],
|
||||||
|
})
|
||||||
|
export class UserSpaceRepositoryModule {}
|
@ -0,0 +1 @@
|
|||||||
|
export * from './user.entity';
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Column, Entity } from 'typeorm';
|
import { Column, Entity, OneToMany } from 'typeorm';
|
||||||
import { UserDto } from '../dtos';
|
import { UserDto } from '../dtos';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { UserSpaceEntity } from '../../user-space/entities';
|
||||||
|
|
||||||
@Entity({ name: 'user' })
|
@Entity({ name: 'user' })
|
||||||
export class UserEntity extends AbstractEntity<UserDto> {
|
export class UserEntity extends AbstractEntity<UserDto> {
|
||||||
@ -47,6 +48,9 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
})
|
})
|
||||||
public isActive: boolean;
|
public isActive: boolean;
|
||||||
|
|
||||||
|
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
|
||||||
|
userSpaces: UserSpaceEntity[];
|
||||||
|
|
||||||
constructor(partial: Partial<UserEntity>) {
|
constructor(partial: Partial<UserEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -29,7 +29,7 @@ import { UnitModule } from './unit/unit.module';
|
|||||||
RoomModule,
|
RoomModule,
|
||||||
GroupModule,
|
GroupModule,
|
||||||
DeviceModule,
|
DeviceModule,
|
||||||
UserDevicePermissionModule
|
UserDevicePermissionModule,
|
||||||
],
|
],
|
||||||
controllers: [AuthenticationController],
|
controllers: [AuthenticationController],
|
||||||
})
|
})
|
||||||
|
@ -16,7 +16,6 @@ import { ResponseMessage } from '../../../libs/common/src/response/response.deco
|
|||||||
import { UserLoginDto } from '../dtos/user-login.dto';
|
import { UserLoginDto } from '../dtos/user-login.dto';
|
||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||||
import { Request } from 'express';
|
|
||||||
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -101,7 +100,7 @@ export class UserAuthController {
|
|||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('user/list')
|
@Get('user/list')
|
||||||
async userList(@Req() req) {
|
async userList() {
|
||||||
const userList = await this.userAuthService.userList();
|
const userList = await this.userAuthService.userList();
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.OK,
|
statusCode: HttpStatus.OK,
|
||||||
|
@ -6,11 +6,27 @@ 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';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
SpaceRepositoryModule,
|
||||||
|
SpaceTypeRepositoryModule,
|
||||||
|
UserSpaceRepositoryModule,
|
||||||
|
UserRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [BuildingController],
|
controllers: [BuildingController],
|
||||||
providers: [BuildingService, SpaceRepository, SpaceTypeRepository],
|
providers: [
|
||||||
|
BuildingService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceTypeRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
UserRepository,
|
||||||
|
],
|
||||||
exports: [BuildingService],
|
exports: [BuildingService],
|
||||||
})
|
})
|
||||||
export class BuildingModule {}
|
export class BuildingModule {}
|
||||||
|
@ -13,10 +13,11 @@ import {
|
|||||||
} 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 { AddBuildingDto } from '../dtos/add.building.dto';
|
import { AddBuildingDto, AddUserBuildingDto } from '../dtos/add.building.dto';
|
||||||
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
||||||
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
||||||
import { CheckCommunityTypeGuard } from 'src/guards/community.type.guard';
|
import { CheckCommunityTypeGuard } from 'src/guards/community.type.guard';
|
||||||
|
import { CheckUserBuildingGuard } from 'src/guards/user.building.guard';
|
||||||
|
|
||||||
@ApiTags('Building Module')
|
@ApiTags('Building Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -92,6 +93,34 @@ export class BuildingController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, CheckUserBuildingGuard)
|
||||||
|
@Post('user')
|
||||||
|
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
|
||||||
|
try {
|
||||||
|
await this.buildingService.addUserBuilding(addUserBuildingDto);
|
||||||
|
return { message: 'user building added successfully' };
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('user/:userUuid')
|
||||||
|
async getBuildingsByUserId(@Param('userUuid') userUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.buildingService.getBuildingsByUserId(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/:buildingUuid')
|
@Put('rename/:buildingUuid')
|
||||||
|
@ -21,3 +21,22 @@ export class AddBuildingDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddUserBuildingDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'buildingUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public buildingUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
constructor(dto: Partial<AddUserBuildingDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -24,3 +24,8 @@ export interface RenameBuildingByUuidInterface {
|
|||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
export interface GetBuildingByUserUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
@ -7,21 +7,24 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddBuildingDto } from '../dtos';
|
import { AddBuildingDto, AddUserBuildingDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
BuildingChildInterface,
|
BuildingChildInterface,
|
||||||
BuildingParentInterface,
|
BuildingParentInterface,
|
||||||
|
GetBuildingByUserUuidInterface,
|
||||||
GetBuildingByUuidInterface,
|
GetBuildingByUuidInterface,
|
||||||
RenameBuildingByUuidInterface,
|
RenameBuildingByUuidInterface,
|
||||||
} from '../interface/building.interface';
|
} from '../interface/building.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
||||||
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BuildingService {
|
export class BuildingService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||||
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async addBuilding(addBuildingDto: AddBuildingDto) {
|
async addBuilding(addBuildingDto: AddBuildingDto) {
|
||||||
@ -210,6 +213,58 @@ export class BuildingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getBuildingsByUserId(
|
||||||
|
userUuid: string,
|
||||||
|
): Promise<GetBuildingByUserUuidInterface[]> {
|
||||||
|
try {
|
||||||
|
const buildings = await this.userSpaceRepository.find({
|
||||||
|
relations: ['space', 'space.spaceType'],
|
||||||
|
where: {
|
||||||
|
user: { uuid: userUuid },
|
||||||
|
space: { spaceType: { type: 'building' } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (buildings.length === 0) {
|
||||||
|
throw new HttpException(
|
||||||
|
'this user has no buildings',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const spaces = buildings.map((building) => ({
|
||||||
|
uuid: building.space.uuid,
|
||||||
|
name: building.space.spaceName,
|
||||||
|
type: building.space.spaceType.type,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return spaces;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async addUserBuilding(addUserBuildingDto: AddUserBuildingDto) {
|
||||||
|
try {
|
||||||
|
await this.userSpaceRepository.save({
|
||||||
|
user: { uuid: addUserBuildingDto.userUuid },
|
||||||
|
space: { uuid: addUserBuildingDto.buildingUuid },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'User already belongs to this building',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || 'Internal Server Error',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async renameBuildingByUuid(
|
async renameBuildingByUuid(
|
||||||
buildingUuid: string,
|
buildingUuid: string,
|
||||||
updateBuildingNameDto: UpdateBuildingNameDto,
|
updateBuildingNameDto: UpdateBuildingNameDto,
|
||||||
|
@ -6,11 +6,27 @@ 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';
|
||||||
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
SpaceRepositoryModule,
|
||||||
|
SpaceTypeRepositoryModule,
|
||||||
|
UserSpaceRepositoryModule,
|
||||||
|
UserRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [CommunityController],
|
controllers: [CommunityController],
|
||||||
providers: [CommunityService, SpaceRepository, SpaceTypeRepository],
|
providers: [
|
||||||
|
CommunityService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceTypeRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
UserRepository,
|
||||||
|
],
|
||||||
exports: [CommunityService],
|
exports: [CommunityService],
|
||||||
})
|
})
|
||||||
export class CommunityModule {}
|
export class CommunityModule {}
|
||||||
|
@ -13,9 +13,13 @@ import {
|
|||||||
} 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 { AddCommunityDto } from '../dtos/add.community.dto';
|
import {
|
||||||
|
AddCommunityDto,
|
||||||
|
AddUserCommunityDto,
|
||||||
|
} from '../dtos/add.community.dto';
|
||||||
import { GetCommunityChildDto } from '../dtos/get.community.dto';
|
import { GetCommunityChildDto } from '../dtos/get.community.dto';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||||
|
|
||||||
@ApiTags('Community Module')
|
@ApiTags('Community Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -77,6 +81,33 @@ export class CommunityController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('user/:userUuid')
|
||||||
|
async getCommunitiesByUserId(@Param('userUuid') userUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.communityService.getCommunitiesByUserId(userUuid);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, CheckUserCommunityGuard)
|
||||||
|
@Post('user')
|
||||||
|
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
|
||||||
|
try {
|
||||||
|
await this.communityService.addUserCommunity(addUserCommunityDto);
|
||||||
|
return { message: 'user community added successfully' };
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Put('rename/:communityUuid')
|
@Put('rename/:communityUuid')
|
||||||
|
@ -14,3 +14,22 @@ export class AddCommunityDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddUserCommunityDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'communityUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public communityUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
constructor(dto: Partial<AddUserCommunityDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -18,3 +18,9 @@ export interface RenameCommunityByUuidInterface {
|
|||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GetCommunityByUserUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
@ -7,20 +7,23 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddCommunityDto } from '../dtos';
|
import { AddCommunityDto, AddUserCommunityDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
CommunityChildInterface,
|
CommunityChildInterface,
|
||||||
|
GetCommunityByUserUuidInterface,
|
||||||
GetCommunityByUuidInterface,
|
GetCommunityByUuidInterface,
|
||||||
RenameCommunityByUuidInterface,
|
RenameCommunityByUuidInterface,
|
||||||
} from '../interface/community.interface';
|
} from '../interface/community.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CommunityService {
|
export class CommunityService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||||
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async addCommunity(addCommunityDto: AddCommunityDto) {
|
async addCommunity(addCommunityDto: AddCommunityDto) {
|
||||||
@ -151,6 +154,60 @@ export class CommunityService {
|
|||||||
|
|
||||||
return childHierarchies;
|
return childHierarchies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCommunitiesByUserId(
|
||||||
|
userUuid: string,
|
||||||
|
): Promise<GetCommunityByUserUuidInterface[]> {
|
||||||
|
try {
|
||||||
|
const communities = await this.userSpaceRepository.find({
|
||||||
|
relations: ['space', 'space.spaceType'],
|
||||||
|
where: {
|
||||||
|
user: { uuid: userUuid },
|
||||||
|
space: { spaceType: { type: 'community' } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (communities.length === 0) {
|
||||||
|
throw new HttpException(
|
||||||
|
'this user has no communities',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const spaces = communities.map((community) => ({
|
||||||
|
uuid: community.space.uuid,
|
||||||
|
name: community.space.spaceName,
|
||||||
|
type: community.space.spaceType.type,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return spaces;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async addUserCommunity(addUserCommunityDto: AddUserCommunityDto) {
|
||||||
|
try {
|
||||||
|
await this.userSpaceRepository.save({
|
||||||
|
user: { uuid: addUserCommunityDto.userUuid },
|
||||||
|
space: { uuid: addUserCommunityDto.communityUuid },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'User already belongs to this community',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || 'Internal Server Error',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async renameCommunityByUuid(
|
async renameCommunityByUuid(
|
||||||
communityUuid: string,
|
communityUuid: string,
|
||||||
updateCommunityDto: UpdateCommunityNameDto,
|
updateCommunityDto: UpdateCommunityNameDto,
|
||||||
|
@ -13,10 +13,11 @@ import {
|
|||||||
} 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 { AddFloorDto } from '../dtos/add.floor.dto';
|
import { AddFloorDto, AddUserFloorDto } from '../dtos/add.floor.dto';
|
||||||
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||||
import { CheckBuildingTypeGuard } from 'src/guards/building.type.guard';
|
import { CheckBuildingTypeGuard } from 'src/guards/building.type.guard';
|
||||||
|
import { CheckUserFloorGuard } from 'src/guards/user.floor.guard';
|
||||||
|
|
||||||
@ApiTags('Floor Module')
|
@ApiTags('Floor Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -91,6 +92,35 @@ export class FloorController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, CheckUserFloorGuard)
|
||||||
|
@Post('user')
|
||||||
|
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
|
||||||
|
try {
|
||||||
|
await this.floorService.addUserFloor(addUserFloorDto);
|
||||||
|
return { message: 'user floor added successfully' };
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('user/:userUuid')
|
||||||
|
async getFloorsByUserId(@Param('userUuid') userUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.floorService.getFloorsByUserId(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/:floorUuid')
|
@Put('rename/:floorUuid')
|
||||||
|
@ -21,3 +21,22 @@ export class AddFloorDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddUserFloorDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'floorUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public floorUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
constructor(dto: Partial<AddUserFloorDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,11 +6,27 @@ 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';
|
||||||
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
SpaceRepositoryModule,
|
||||||
|
SpaceTypeRepositoryModule,
|
||||||
|
UserSpaceRepositoryModule,
|
||||||
|
UserRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [FloorController],
|
controllers: [FloorController],
|
||||||
providers: [FloorService, SpaceRepository, SpaceTypeRepository],
|
providers: [
|
||||||
|
FloorService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceTypeRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
UserRepository,
|
||||||
|
],
|
||||||
exports: [FloorService],
|
exports: [FloorService],
|
||||||
})
|
})
|
||||||
export class FloorModule {}
|
export class FloorModule {}
|
||||||
|
@ -24,3 +24,9 @@ export interface RenameFloorByUuidInterface {
|
|||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GetFloorByUserUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
@ -7,21 +7,24 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddFloorDto } from '../dtos';
|
import { AddFloorDto, AddUserFloorDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
FloorChildInterface,
|
FloorChildInterface,
|
||||||
FloorParentInterface,
|
FloorParentInterface,
|
||||||
|
GetFloorByUserUuidInterface,
|
||||||
GetFloorByUuidInterface,
|
GetFloorByUuidInterface,
|
||||||
RenameFloorByUuidInterface,
|
RenameFloorByUuidInterface,
|
||||||
} from '../interface/floor.interface';
|
} from '../interface/floor.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||||
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FloorService {
|
export class FloorService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||||
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async addFloor(addFloorDto: AddFloorDto) {
|
async addFloor(addFloorDto: AddFloorDto) {
|
||||||
@ -195,6 +198,58 @@ 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 addUserFloor(addUserFloorDto: AddUserFloorDto) {
|
||||||
|
try {
|
||||||
|
await this.userSpaceRepository.save({
|
||||||
|
user: { uuid: addUserFloorDto.userUuid },
|
||||||
|
space: { uuid: addUserFloorDto.floorUuid },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'User already belongs to this floor',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || 'Internal Server Error',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async renameFloorByUuid(
|
async renameFloorByUuid(
|
||||||
floorUuid: string,
|
floorUuid: string,
|
||||||
updateFloorDto: UpdateFloorNameDto,
|
updateFloorDto: UpdateFloorNameDto,
|
||||||
|
@ -5,17 +5,18 @@ import {
|
|||||||
Get,
|
Get,
|
||||||
Post,
|
Post,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
Query,
|
|
||||||
Param,
|
Param,
|
||||||
Put,
|
Put,
|
||||||
Delete,
|
Delete,
|
||||||
|
HttpException,
|
||||||
|
HttpStatus,
|
||||||
} 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 { AddGroupDto } from '../dtos/add.group.dto';
|
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||||
import { GetGroupDto } from '../dtos/get.group.dto';
|
|
||||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||||
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||||
|
import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.guard';
|
||||||
|
|
||||||
@ApiTags('Group Module')
|
@ApiTags('Group Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -27,32 +28,41 @@ export class GroupController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get()
|
@Get('space/:spaceUuid')
|
||||||
async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) {
|
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.getGroupsByHomeId(getGroupsDto);
|
return await this.groupService.getGroupsBySpaceUuid(spaceUuid);
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
throw new Error(err);
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get(':groupId')
|
@Get(':groupUuid')
|
||||||
async getGroupsByGroupId(@Param('groupId') groupId: number) {
|
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.getGroupsByGroupId(groupId);
|
return await this.groupService.getGroupsByGroupUuid(groupUuid);
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
throw new Error(err);
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckProductUuidForAllDevicesGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addGroup(@Body() addGroupDto: AddGroupDto) {
|
async addGroup(@Body() addGroupDto: AddGroupDto) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.addGroup(addGroupDto);
|
return await this.groupService.addGroup(addGroupDto);
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
throw new Error(err);
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,30 +72,45 @@ export class GroupController {
|
|||||||
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
|
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.controlGroup(controlGroupDto);
|
return await this.groupService.controlGroup(controlGroupDto);
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
throw new Error(err);
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Put('rename')
|
@Put('rename/:groupUuid')
|
||||||
async renameGroup(@Body() renameGroupDto: RenameGroupDto) {
|
async renameGroupByUuid(
|
||||||
|
@Param('groupUuid') groupUuid: string,
|
||||||
|
@Body() renameGroupDto: RenameGroupDto,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.renameGroup(renameGroupDto);
|
return await this.groupService.renameGroupByUuid(
|
||||||
} catch (err) {
|
groupUuid,
|
||||||
throw new Error(err);
|
renameGroupDto,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Delete(':groupId')
|
@Delete(':groupUuid')
|
||||||
async deleteGroup(@Param('groupId') groupId: number) {
|
async deleteGroup(@Param('groupUuid') groupUuid: string) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.deleteGroup(groupId);
|
return await this.groupService.deleteGroup(groupUuid);
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
throw new Error(err);
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
import { IsNotEmpty, IsString, IsArray } from 'class-validator';
|
||||||
|
|
||||||
export class AddGroupDto {
|
export class AddGroupDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -11,26 +11,10 @@ export class AddGroupDto {
|
|||||||
public groupName: string;
|
public groupName: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'homeId',
|
description: 'deviceUuids',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsNumberString()
|
@IsArray()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public homeId: string;
|
public deviceUuids: [string];
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'productId',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public productId: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The list of up to 20 device IDs, separated with commas (,)',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public deviceIds: string;
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,26 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsObject, IsNumberString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class ControlGroupDto {
|
export class ControlGroupDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'groupId',
|
description: 'groupUuid',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsNumberString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public groupId: string;
|
public groupUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'example {"switch_1":true,"add_ele":300}',
|
description: 'code',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsObject()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public properties: object;
|
public code: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'value',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
public value: any;
|
||||||
}
|
}
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { IsNotEmpty, IsNumberString } from 'class-validator';
|
|
||||||
|
|
||||||
export class GetGroupDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'homeId',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsNumberString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public homeId: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'pageSize',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsNumberString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public pageSize: number;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'pageNo',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsNumberString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public pageNo: number;
|
|
||||||
}
|
|
@ -1,15 +1,7 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class RenameGroupDto {
|
export class RenameGroupDto {
|
||||||
@ApiProperty({
|
|
||||||
description: 'groupId',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsNumberString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public groupId: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'groupName',
|
description: 'groupName',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -2,10 +2,26 @@ import { Module } from '@nestjs/common';
|
|||||||
import { GroupService } from './services/group.service';
|
import { GroupService } from './services/group.service';
|
||||||
import { GroupController } from './controllers/group.controller';
|
import { GroupController } from './controllers/group.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
import { GroupRepositoryModule } from '@app/common/modules/group/group.repository.module';
|
||||||
|
import { GroupRepository } from '@app/common/modules/group/repositories';
|
||||||
|
import { GroupDeviceRepositoryModule } from '@app/common/modules/group-device/group.device.repository.module';
|
||||||
|
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
||||||
|
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
GroupRepositoryModule,
|
||||||
|
GroupDeviceRepositoryModule,
|
||||||
|
DeviceRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [GroupController],
|
controllers: [GroupController],
|
||||||
providers: [GroupService],
|
providers: [
|
||||||
|
GroupService,
|
||||||
|
GroupRepository,
|
||||||
|
GroupDeviceRepository,
|
||||||
|
DeviceRepository,
|
||||||
|
],
|
||||||
exports: [GroupService],
|
exports: [GroupService],
|
||||||
})
|
})
|
||||||
export class GroupModule {}
|
export class GroupModule {}
|
||||||
|
@ -1,25 +1,15 @@
|
|||||||
export class GetGroupDetailsInterface {
|
export interface GetGroupDetailsInterface {
|
||||||
result: {
|
groupUuid: string;
|
||||||
id: string;
|
groupName: string;
|
||||||
name: string;
|
createdAt: Date;
|
||||||
};
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
export class GetGroupsInterface {
|
export interface GetGroupsBySpaceUuidInterface {
|
||||||
result: {
|
groupUuid: string;
|
||||||
count: number;
|
groupName: string;
|
||||||
data_list: [];
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class addGroupInterface {
|
export interface controlGroupInterface {
|
||||||
success: boolean;
|
|
||||||
msg: string;
|
|
||||||
result: {
|
|
||||||
id: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class controlGroupInterface {
|
|
||||||
success: boolean;
|
success: boolean;
|
||||||
result: boolean;
|
result: boolean;
|
||||||
msg: string;
|
msg: string;
|
||||||
|
@ -1,21 +1,31 @@
|
|||||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
import {
|
||||||
|
Injectable,
|
||||||
|
HttpException,
|
||||||
|
HttpStatus,
|
||||||
|
BadRequestException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||||
import {
|
import {
|
||||||
GetGroupDetailsInterface,
|
GetGroupDetailsInterface,
|
||||||
GetGroupsInterface,
|
GetGroupsBySpaceUuidInterface,
|
||||||
addGroupInterface,
|
|
||||||
controlGroupInterface,
|
|
||||||
} from '../interfaces/get.group.interface';
|
} from '../interfaces/get.group.interface';
|
||||||
import { GetGroupDto } from '../dtos/get.group.dto';
|
|
||||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||||
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||||
|
import { GroupRepository } from '@app/common/modules/group/repositories';
|
||||||
|
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
||||||
|
import { controlDeviceInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
|
import { ControlDeviceDto } from 'src/device/dtos';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupService {
|
export class GroupService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
constructor(private readonly configService: ConfigService) {
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
private readonly groupRepository: GroupRepository,
|
||||||
|
private readonly groupDeviceRepository: GroupDeviceRepository,
|
||||||
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
||||||
@ -26,88 +36,100 @@ export class GroupService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupsByHomeId(getGroupDto: GetGroupDto) {
|
async getGroupsBySpaceUuid(
|
||||||
|
spaceUuid: string,
|
||||||
|
): Promise<GetGroupsBySpaceUuidInterface[]> {
|
||||||
try {
|
try {
|
||||||
const response = await this.getGroupsTuya(getGroupDto);
|
const groupDevices = await this.groupDeviceRepository.find({
|
||||||
|
relations: ['group', 'device'],
|
||||||
const groups = response.result.data_list.map((group: any) => ({
|
where: {
|
||||||
groupId: group.id,
|
device: { spaceUuid },
|
||||||
groupName: group.name,
|
isActive: true,
|
||||||
}));
|
|
||||||
|
|
||||||
return {
|
|
||||||
count: response.result.count,
|
|
||||||
groups: groups,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error fetching groups',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getGroupsTuya(getGroupDto: GetGroupDto): Promise<GetGroupsInterface> {
|
|
||||||
try {
|
|
||||||
const path = `/v2.0/cloud/thing/group`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'GET',
|
|
||||||
path,
|
|
||||||
query: {
|
|
||||||
space_id: getGroupDto.homeId,
|
|
||||||
page_size: getGroupDto.pageSize,
|
|
||||||
page_no: getGroupDto.pageNo,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return response as unknown as GetGroupsInterface;
|
|
||||||
|
// Extract and return only the group entities
|
||||||
|
const groups = groupDevices.map((groupDevice) => {
|
||||||
|
return {
|
||||||
|
groupUuid: groupDevice.uuid,
|
||||||
|
groupName: groupDevice.group.groupName,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
if (groups.length > 0) {
|
||||||
|
return groups;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
'this space has no groups',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching groups ',
|
error.message || 'Error fetching groups',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addGroup(addGroupDto: AddGroupDto) {
|
async addGroup(addGroupDto: AddGroupDto) {
|
||||||
const response = await this.addGroupTuya(addGroupDto);
|
|
||||||
|
|
||||||
if (response.success) {
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
groupId: response.result.id,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
response.msg || 'Unknown error',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async addGroupTuya(addGroupDto: AddGroupDto): Promise<addGroupInterface> {
|
|
||||||
try {
|
try {
|
||||||
const path = `/v2.0/cloud/thing/group`;
|
const group = await this.groupRepository.save({
|
||||||
const response = await this.tuya.request({
|
groupName: addGroupDto.groupName,
|
||||||
method: 'POST',
|
|
||||||
path,
|
|
||||||
body: {
|
|
||||||
space_id: addGroupDto.homeId,
|
|
||||||
name: addGroupDto.groupName,
|
|
||||||
product_id: addGroupDto.productId,
|
|
||||||
device_ids: addGroupDto.deviceIds,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return response as addGroupInterface;
|
const groupDevicePromises = addGroupDto.deviceUuids.map(
|
||||||
} catch (error) {
|
async (deviceUuid) => {
|
||||||
|
await this.saveGroupDevice(group.uuid, deviceUuid);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await Promise.all(groupDevicePromises);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'User already belongs to this group',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error adding group',
|
err.message || 'Internal Server Error',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async controlGroup(controlGroupDto: ControlGroupDto) {
|
private async saveGroupDevice(groupUuid: string, deviceUuid: string) {
|
||||||
const response = await this.controlGroupTuya(controlGroupDto);
|
try {
|
||||||
|
await this.groupDeviceRepository.save({
|
||||||
|
group: {
|
||||||
|
uuid: groupUuid,
|
||||||
|
},
|
||||||
|
device: {
|
||||||
|
uuid: deviceUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getDevicesByGroupUuid(groupUuid: string) {
|
||||||
|
try {
|
||||||
|
const devices = await this.groupDeviceRepository.find({
|
||||||
|
relations: ['device'],
|
||||||
|
where: {
|
||||||
|
group: {
|
||||||
|
uuid: groupUuid,
|
||||||
|
},
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return devices;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async controlDevice(controlDeviceDto: ControlDeviceDto) {
|
||||||
|
const response = await this.controlDeviceTuya(controlDeviceDto);
|
||||||
|
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
return response;
|
return response;
|
||||||
@ -118,128 +140,120 @@ export class GroupService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async controlGroupTuya(
|
async controlDeviceTuya(
|
||||||
controlGroupDto: ControlGroupDto,
|
controlDeviceDto: ControlDeviceDto,
|
||||||
): Promise<controlGroupInterface> {
|
): Promise<controlDeviceInterface> {
|
||||||
try {
|
try {
|
||||||
const path = `/v2.0/cloud/thing/group/properties`;
|
const path = `/v1.0/iot-03/devices/${controlDeviceDto.deviceId}/commands`;
|
||||||
const response = await this.tuya.request({
|
const response = await this.tuya.request({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
path,
|
path,
|
||||||
body: {
|
body: {
|
||||||
group_id: controlGroupDto.groupId,
|
commands: [
|
||||||
properties: controlGroupDto.properties,
|
{ code: controlDeviceDto.code, value: controlDeviceDto.value },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return response as controlGroupInterface;
|
return response as controlDeviceInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error control group',
|
'Error control device from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async controlGroup(controlGroupDto: ControlGroupDto) {
|
||||||
|
const devices = await this.getDevicesByGroupUuid(controlGroupDto.groupUuid);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.all(
|
||||||
|
devices.map(async (device) => {
|
||||||
|
return this.controlDevice({
|
||||||
|
deviceId: device.device.deviceTuyaUuid,
|
||||||
|
code: controlGroupDto.code,
|
||||||
|
value: controlGroupDto.value,
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error controlling devices',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async renameGroup(renameGroupDto: RenameGroupDto) {
|
async renameGroupByUuid(
|
||||||
const response = await this.renameGroupTuya(renameGroupDto);
|
groupUuid: string,
|
||||||
|
|
||||||
if (response.success) {
|
|
||||||
return {
|
|
||||||
success: response.success,
|
|
||||||
result: response.result,
|
|
||||||
msg: response.msg,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
response.msg || 'Unknown error',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async renameGroupTuya(
|
|
||||||
renameGroupDto: RenameGroupDto,
|
renameGroupDto: RenameGroupDto,
|
||||||
): Promise<controlGroupInterface> {
|
): Promise<GetGroupsBySpaceUuidInterface> {
|
||||||
try {
|
try {
|
||||||
const path = `/v2.0/cloud/thing/group/${renameGroupDto.groupId}/${renameGroupDto.groupName}`;
|
await this.groupRepository.update(
|
||||||
const response = await this.tuya.request({
|
{ uuid: groupUuid },
|
||||||
method: 'PUT',
|
{ groupName: renameGroupDto.groupName },
|
||||||
path,
|
);
|
||||||
|
|
||||||
|
// Fetch the updated floor
|
||||||
|
const updatedGroup = await this.groupRepository.findOneOrFail({
|
||||||
|
where: { uuid: groupUuid },
|
||||||
});
|
});
|
||||||
|
|
||||||
return response as controlGroupInterface;
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error rename group',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async deleteGroup(groupId: number) {
|
|
||||||
const response = await this.deleteGroupTuya(groupId);
|
|
||||||
|
|
||||||
if (response.success) {
|
|
||||||
return {
|
return {
|
||||||
success: response.success,
|
groupUuid: updatedGroup.uuid,
|
||||||
result: response.result,
|
groupName: updatedGroup.groupName,
|
||||||
msg: response.msg,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
response.msg || 'Unknown error',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async deleteGroupTuya(groupId: number): Promise<controlGroupInterface> {
|
|
||||||
try {
|
|
||||||
const path = `/v2.0/cloud/thing/group/${groupId}`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'DELETE',
|
|
||||||
path,
|
|
||||||
});
|
|
||||||
|
|
||||||
return response as controlGroupInterface;
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error delete group',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getGroupsByGroupId(groupId: number) {
|
|
||||||
try {
|
|
||||||
const response = await this.getGroupsByGroupIdTuya(groupId);
|
|
||||||
|
|
||||||
return {
|
|
||||||
groupId: response.result.id,
|
|
||||||
groupName: response.result.name,
|
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteGroup(groupUuid: string) {
|
||||||
|
try {
|
||||||
|
const group = await this.getGroupsByGroupUuid(groupUuid);
|
||||||
|
|
||||||
|
if (!group) {
|
||||||
|
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.groupRepository.update(
|
||||||
|
{ uuid: groupUuid },
|
||||||
|
{ isActive: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
return { message: 'Group deleted successfully' };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching group',
|
error.message || 'Error deleting group',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupsByGroupIdTuya(
|
async getGroupsByGroupUuid(
|
||||||
groupId: number,
|
groupUuid: string,
|
||||||
): Promise<GetGroupDetailsInterface> {
|
): Promise<GetGroupDetailsInterface> {
|
||||||
try {
|
try {
|
||||||
const path = `/v2.0/cloud/thing/group/${groupId}`;
|
const group = await this.groupRepository.findOne({
|
||||||
const response = await this.tuya.request({
|
where: {
|
||||||
method: 'GET',
|
uuid: groupUuid,
|
||||||
path,
|
isActive: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return response as GetGroupDetailsInterface;
|
if (!group) {
|
||||||
} catch (error) {
|
throw new BadRequestException('Invalid group UUID');
|
||||||
throw new HttpException(
|
}
|
||||||
'Error fetching group ',
|
return {
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
groupUuid: group.uuid,
|
||||||
);
|
groupName: group.groupName,
|
||||||
|
createdAt: group.createdAt,
|
||||||
|
updatedAt: group.updatedAt,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
71
src/guards/device.product.guard.ts
Normal file
71
src/guards/device.product.guard.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
CanActivate,
|
||||||
|
HttpStatus,
|
||||||
|
BadRequestException,
|
||||||
|
ExecutionContext,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||||
|
constructor(private readonly deviceRepository: DeviceRepository) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { deviceUuids } = req.body;
|
||||||
|
console.log(deviceUuids);
|
||||||
|
|
||||||
|
await this.checkAllDevicesHaveSameProductUuid(deviceUuids);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
|
||||||
|
const firstDevice = await this.deviceRepository.findOne({
|
||||||
|
where: { uuid: deviceUuids[0] },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!firstDevice) {
|
||||||
|
throw new BadRequestException('First device not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const firstProductUuid = firstDevice.productUuid;
|
||||||
|
|
||||||
|
for (let i = 1; i < deviceUuids.length; i++) {
|
||||||
|
const device = await this.deviceRepository.findOne({
|
||||||
|
where: { uuid: deviceUuids[i] },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!device) {
|
||||||
|
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device.productUuid !== firstProductUuid) {
|
||||||
|
throw new BadRequestException(`Devices have different product UUIDs`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.NOT_FOUND).json({
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
message: 'Device not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/guards/user.building.guard.ts
Normal file
70
src/guards/user.building.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckUserBuildingGuard implements CanActivate {
|
||||||
|
constructor(
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { userUuid, buildingUuid } = req.body;
|
||||||
|
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
|
||||||
|
await this.checkBuildingIsFound(buildingUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const userData = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid },
|
||||||
|
});
|
||||||
|
if (!userData) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkBuildingIsFound(spaceUuid: string) {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid, spaceType: { type: 'building' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new NotFoundException('Building not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (
|
||||||
|
error instanceof BadRequestException ||
|
||||||
|
error instanceof NotFoundException
|
||||||
|
) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: 'invalid userUuid or buildingUuid',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/guards/user.community.guard.ts
Normal file
70
src/guards/user.community.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckUserCommunityGuard implements CanActivate {
|
||||||
|
constructor(
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { userUuid, communityUuid } = req.body;
|
||||||
|
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
|
||||||
|
await this.checkCommunityIsFound(communityUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const userData = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid },
|
||||||
|
});
|
||||||
|
if (!userData) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkCommunityIsFound(spaceUuid: string) {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid, spaceType: { type: 'community' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new NotFoundException('Community not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (
|
||||||
|
error instanceof BadRequestException ||
|
||||||
|
error instanceof NotFoundException
|
||||||
|
) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: 'invalid userUuid or communityUuid',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/guards/user.floor.guard.ts
Normal file
70
src/guards/user.floor.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckUserFloorGuard implements CanActivate {
|
||||||
|
constructor(
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { userUuid, floorUuid } = req.body;
|
||||||
|
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
|
||||||
|
await this.checkFloorIsFound(floorUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const userData = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid },
|
||||||
|
});
|
||||||
|
if (!userData) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkFloorIsFound(spaceUuid: string) {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid, spaceType: { type: 'floor' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new NotFoundException('Floor not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (
|
||||||
|
error instanceof BadRequestException ||
|
||||||
|
error instanceof NotFoundException
|
||||||
|
) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: 'invalid userUuid or floorUuid',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/guards/user.room.guard.ts
Normal file
70
src/guards/user.room.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckUserRoomGuard implements CanActivate {
|
||||||
|
constructor(
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { userUuid, roomUuid } = req.body;
|
||||||
|
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
|
||||||
|
await this.checkRoomIsFound(roomUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const userData = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid },
|
||||||
|
});
|
||||||
|
if (!userData) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkRoomIsFound(spaceUuid: string) {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid, spaceType: { type: 'room' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new NotFoundException('Room not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (
|
||||||
|
error instanceof BadRequestException ||
|
||||||
|
error instanceof NotFoundException
|
||||||
|
) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: 'invalid userUuid or roomUuid',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/guards/user.unit.guard.ts
Normal file
70
src/guards/user.unit.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckUserUnitGuard implements CanActivate {
|
||||||
|
constructor(
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { userUuid, unitUuid } = req.body;
|
||||||
|
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
|
||||||
|
await this.checkUnitIsFound(unitUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const userData = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid },
|
||||||
|
});
|
||||||
|
if (!userData) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUnitIsFound(spaceUuid: string) {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid, spaceType: { type: 'unit' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new NotFoundException('Unit not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (
|
||||||
|
error instanceof BadRequestException ||
|
||||||
|
error instanceof NotFoundException
|
||||||
|
) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: 'invalid userUuid or unitUuid',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,9 +12,10 @@ import {
|
|||||||
} 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 { AddRoomDto } from '../dtos/add.room.dto';
|
import { AddRoomDto, AddUserRoomDto } from '../dtos/add.room.dto';
|
||||||
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
||||||
import { CheckUnitTypeGuard } from 'src/guards/unit.type.guard';
|
import { CheckUnitTypeGuard } from 'src/guards/unit.type.guard';
|
||||||
|
import { CheckUserRoomGuard } from 'src/guards/user.room.guard';
|
||||||
|
|
||||||
@ApiTags('Room Module')
|
@ApiTags('Room Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -68,6 +69,33 @@ export class RoomController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, CheckUserRoomGuard)
|
||||||
|
@Post('user')
|
||||||
|
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
|
||||||
|
try {
|
||||||
|
await this.roomService.addUserRoom(addUserRoomDto);
|
||||||
|
return { message: 'user room added successfully' };
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('user/:userUuid')
|
||||||
|
async getRoomsByUserId(@Param('userUuid') userUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.roomService.getRoomsByUserId(userUuid);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@ -21,3 +21,22 @@ export class AddRoomDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddUserRoomDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'roomUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public roomUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
constructor(dto: Partial<AddUserRoomDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -17,3 +17,8 @@ export interface RenameRoomByUuidInterface {
|
|||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
export interface GetRoomByUserUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
@ -6,11 +6,27 @@ 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';
|
||||||
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
SpaceRepositoryModule,
|
||||||
|
SpaceTypeRepositoryModule,
|
||||||
|
UserSpaceRepositoryModule,
|
||||||
|
UserRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [RoomController],
|
controllers: [RoomController],
|
||||||
providers: [RoomService, SpaceRepository, SpaceTypeRepository],
|
providers: [
|
||||||
|
RoomService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceTypeRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
UserRepository,
|
||||||
|
],
|
||||||
exports: [RoomService],
|
exports: [RoomService],
|
||||||
})
|
})
|
||||||
export class RoomModule {}
|
export class RoomModule {}
|
||||||
|
@ -6,19 +6,22 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddRoomDto } from '../dtos';
|
import { AddRoomDto, AddUserRoomDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
RoomParentInterface,
|
RoomParentInterface,
|
||||||
GetRoomByUuidInterface,
|
GetRoomByUuidInterface,
|
||||||
RenameRoomByUuidInterface,
|
RenameRoomByUuidInterface,
|
||||||
|
GetRoomByUserUuidInterface,
|
||||||
} from '../interface/room.interface';
|
} from '../interface/room.interface';
|
||||||
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
||||||
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RoomService {
|
export class RoomService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||||
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async addRoom(addRoomDto: AddRoomDto) {
|
async addRoom(addRoomDto: AddRoomDto) {
|
||||||
@ -103,6 +106,56 @@ export class RoomService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getRoomsByUserId(
|
||||||
|
userUuid: string,
|
||||||
|
): Promise<GetRoomByUserUuidInterface[]> {
|
||||||
|
try {
|
||||||
|
const rooms = await this.userSpaceRepository.find({
|
||||||
|
relations: ['space', 'space.spaceType'],
|
||||||
|
where: {
|
||||||
|
user: { uuid: userUuid },
|
||||||
|
space: { spaceType: { type: 'room' } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (rooms.length === 0) {
|
||||||
|
throw new HttpException('this user has no rooms', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
const spaces = rooms.map((room) => ({
|
||||||
|
uuid: room.space.uuid,
|
||||||
|
name: room.space.spaceName,
|
||||||
|
type: room.space.spaceType.type,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return spaces;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async addUserRoom(addUserRoomDto: AddUserRoomDto) {
|
||||||
|
try {
|
||||||
|
await this.userSpaceRepository.save({
|
||||||
|
user: { uuid: addUserRoomDto.userUuid },
|
||||||
|
space: { uuid: addUserRoomDto.roomUuid },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'User already belongs to this room',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || 'Internal Server Error',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async renameRoomByUuid(
|
async renameRoomByUuid(
|
||||||
roomUuid: string,
|
roomUuid: string,
|
||||||
updateRoomNameDto: UpdateRoomNameDto,
|
updateRoomNameDto: UpdateRoomNameDto,
|
||||||
|
@ -13,10 +13,11 @@ import {
|
|||||||
} 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, AddUserUnitDto } from '../dtos/add.unit.dto';
|
||||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
import { 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';
|
||||||
|
import { CheckUserUnitGuard } from 'src/guards/user.unit.guard';
|
||||||
|
|
||||||
@ApiTags('Unit Module')
|
@ApiTags('Unit Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -87,6 +88,33 @@ export class UnitController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, CheckUserUnitGuard)
|
||||||
|
@Post('user')
|
||||||
|
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
|
||||||
|
try {
|
||||||
|
await this.unitService.addUserUnit(addUserUnitDto);
|
||||||
|
return { message: 'user unit added successfully' };
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('user/:userUuid')
|
||||||
|
async getUnitsByUserId(@Param('userUuid') userUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.unitService.getUnitsByUserId(userUuid);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@ -21,3 +21,22 @@ export class AddUnitDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddUserUnitDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'unitUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public unitUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
constructor(dto: Partial<AddUserUnitDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
@ -7,21 +7,24 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddUnitDto } from '../dtos';
|
import { AddUnitDto, AddUserUnitDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
UnitChildInterface,
|
UnitChildInterface,
|
||||||
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,56 @@ 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 addUserUnit(addUserUnitDto: AddUserUnitDto) {
|
||||||
|
try {
|
||||||
|
await this.userSpaceRepository.save({
|
||||||
|
user: { uuid: addUserUnitDto.userUuid },
|
||||||
|
space: { uuid: addUserUnitDto.unitUuid },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'User already belongs to this unit',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || 'Internal Server Error',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async renameUnitByUuid(
|
async renameUnitByUuid(
|
||||||
unitUuid: string,
|
unitUuid: string,
|
||||||
updateUnitNameDto: UpdateUnitNameDto,
|
updateUnitNameDto: UpdateUnitNameDto,
|
||||||
|
@ -6,11 +6,27 @@ 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';
|
||||||
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
SpaceRepositoryModule,
|
||||||
|
SpaceTypeRepositoryModule,
|
||||||
|
UserSpaceRepositoryModule,
|
||||||
|
UserRepositoryModule,
|
||||||
|
],
|
||||||
controllers: [UnitController],
|
controllers: [UnitController],
|
||||||
providers: [UnitService, SpaceRepository, SpaceTypeRepository],
|
providers: [
|
||||||
|
UnitService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceTypeRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
UserRepository,
|
||||||
|
],
|
||||||
exports: [UnitService],
|
exports: [UnitService],
|
||||||
})
|
})
|
||||||
export class UnitModule {}
|
export class UnitModule {}
|
||||||
|
Reference in New Issue
Block a user