mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Merge branch 'dev' into SP-232-consume-the-tuya-messaging-service-for-device-logs
This commit is contained in:
10
libs/common/src/helper/randomString.ts
Normal file
10
libs/common/src/helper/randomString.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export function generateRandomString(length: number): string {
|
||||||
|
const characters =
|
||||||
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||||
|
let randomString = '';
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||||
|
randomString += characters.charAt(randomIndex);
|
||||||
|
}
|
||||||
|
return randomString;
|
||||||
|
}
|
@ -9,6 +9,10 @@ export class DeviceDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
spaceUuid: string;
|
spaceUuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
userUuid: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
deviceTuyaUuid: string;
|
deviceTuyaUuid: string;
|
||||||
|
@ -6,9 +6,10 @@ import { SpaceEntity } from '../../space/entities';
|
|||||||
import { ProductEntity } from '../../product/entities';
|
import { ProductEntity } from '../../product/entities';
|
||||||
import { DeviceUserPermissionEntity } from '../../device-user-permission/entities';
|
import { DeviceUserPermissionEntity } from '../../device-user-permission/entities';
|
||||||
import { DeviceNotificationEntity } from '../../device-notification/entities';
|
import { DeviceNotificationEntity } from '../../device-notification/entities';
|
||||||
|
import { UserEntity } from '../../user/entities';
|
||||||
|
|
||||||
@Entity({ name: 'device' })
|
@Entity({ name: 'device' })
|
||||||
@Unique(['spaceDevice', 'deviceTuyaUuid'])
|
@Unique(['deviceTuyaUuid'])
|
||||||
export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
@ -21,6 +22,9 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
})
|
})
|
||||||
isActive: true;
|
isActive: true;
|
||||||
|
|
||||||
|
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: false })
|
||||||
|
user: UserEntity;
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DeviceUserPermissionEntity,
|
() => DeviceUserPermissionEntity,
|
||||||
(permission) => permission.device,
|
(permission) => permission.device,
|
||||||
@ -44,7 +48,7 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
userGroupDevices: GroupDeviceEntity[];
|
userGroupDevices: GroupDeviceEntity[];
|
||||||
|
|
||||||
@ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, {
|
@ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, {
|
||||||
nullable: false,
|
nullable: true,
|
||||||
})
|
})
|
||||||
spaceDevice: SpaceEntity;
|
spaceDevice: SpaceEntity;
|
||||||
|
|
||||||
|
@ -16,4 +16,8 @@ export class SpaceDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public spaceTypeUuid: string;
|
public spaceTypeUuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public invitationCode: string;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
import { Column, Entity, ManyToOne, OneToMany, Unique } 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';
|
||||||
@ -6,6 +6,7 @@ import { UserSpaceEntity } from '../../user-space/entities';
|
|||||||
import { DeviceEntity } from '../../device/entities';
|
import { DeviceEntity } from '../../device/entities';
|
||||||
|
|
||||||
@Entity({ name: 'space' })
|
@Entity({ name: 'space' })
|
||||||
|
@Unique(['invitationCode'])
|
||||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||||
@Column({
|
@Column({
|
||||||
type: 'uuid',
|
type: 'uuid',
|
||||||
@ -18,6 +19,11 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
public spaceName: string;
|
public spaceName: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
public invitationCode: string;
|
||||||
@ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true })
|
@ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true })
|
||||||
parent: SpaceEntity;
|
parent: SpaceEntity;
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import { UserSpaceEntity } from '../../user-space/entities';
|
|||||||
import { UserRoleEntity } from '../../user-role/entities';
|
import { UserRoleEntity } from '../../user-role/entities';
|
||||||
import { DeviceNotificationEntity } from '../../device-notification/entities';
|
import { DeviceNotificationEntity } from '../../device-notification/entities';
|
||||||
import { UserNotificationEntity } from '../../user-notification/entities';
|
import { UserNotificationEntity } from '../../user-notification/entities';
|
||||||
|
import { DeviceEntity } from '../../device/entities';
|
||||||
|
|
||||||
@Entity({ name: 'user' })
|
@Entity({ name: 'user' })
|
||||||
export class UserEntity extends AbstractEntity<UserDto> {
|
export class UserEntity extends AbstractEntity<UserDto> {
|
||||||
@ -55,6 +56,15 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
|
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
|
||||||
userSpaces: UserSpaceEntity[];
|
userSpaces: UserSpaceEntity[];
|
||||||
|
|
||||||
|
@OneToMany(() => DeviceEntity, (userDevice) => userDevice.user)
|
||||||
|
userDevice: DeviceEntity[];
|
||||||
|
|
||||||
|
@OneToMany(() => DeviceEntity, (userDevice) => userDevice.user)
|
||||||
|
userDevice: DeviceEntity[];
|
||||||
|
|
||||||
|
@OneToMany(() => DeviceEntity, (userDevice) => userDevice.user)
|
||||||
|
userDevice: DeviceEntity[];
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => UserNotificationEntity,
|
() => UserNotificationEntity,
|
||||||
(userNotification) => userNotification.user,
|
(userNotification) => userNotification.user,
|
||||||
|
31
package-lock.json
generated
31
package-lock.json
generated
@ -5365,20 +5365,6 @@
|
|||||||
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
|
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/fsevents": {
|
|
||||||
"version": "2.3.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
|
||||||
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
|
|
||||||
"dev": true,
|
|
||||||
"hasInstallScript": true,
|
|
||||||
"optional": true,
|
|
||||||
"os": [
|
|
||||||
"darwin"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/function-bind": {
|
"node_modules/function-bind": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
||||||
@ -7378,6 +7364,23 @@
|
|||||||
"thenify-all": "^1.0.0"
|
"thenify-all": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/nanoid": {
|
||||||
|
"version": "5.0.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.0.7.tgz",
|
||||||
|
"integrity": "sha512-oLxFY2gd2IqnjcYyOXD8XGCftpGtZP2AbHbOkthDkvRywH5ayNtPVy9YlOPcHckXzbLTCHpkb7FB+yuxKV13pQ==",
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/ai"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bin": {
|
||||||
|
"nanoid": "bin/nanoid.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^18 || >=20"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/natural-compare": {
|
"node_modules/natural-compare": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||||
|
@ -30,7 +30,7 @@ export class BuildingController {
|
|||||||
constructor(private readonly buildingService: BuildingService) {}
|
constructor(private readonly buildingService: BuildingService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard, CheckCommunityTypeGuard)
|
@UseGuards(JwtAuthGuard, CheckCommunityTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -32,7 +32,7 @@ export class CommunityController {
|
|||||||
constructor(private readonly communityService: CommunityService) {}
|
constructor(private readonly communityService: CommunityService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addCommunity(@Body() addCommunityDto: AddCommunityDto) {
|
async addCommunity(@Body() addCommunityDto: AddCommunityDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -10,11 +10,13 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
Req,
|
Req,
|
||||||
|
Put,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
|
AddDeviceDto,
|
||||||
AddDeviceInGroupDto,
|
AddDeviceInGroupDto,
|
||||||
AddDeviceInRoomDto,
|
UpdateDeviceInRoomDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import {
|
import {
|
||||||
GetDeviceByGroupIdDto,
|
GetDeviceByGroupIdDto,
|
||||||
@ -26,6 +28,8 @@ import { CheckGroupGuard } from 'src/guards/group.guard';
|
|||||||
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
||||||
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { CheckDeviceGuard } from 'src/guards/device.guard';
|
||||||
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -34,7 +38,39 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
|||||||
})
|
})
|
||||||
export class DeviceController {
|
export class DeviceController {
|
||||||
constructor(private readonly deviceService: DeviceService) {}
|
constructor(private readonly deviceService: DeviceService) {}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(SuperAdminRoleGuard, CheckDeviceGuard)
|
||||||
|
@Post()
|
||||||
|
async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) {
|
||||||
|
try {
|
||||||
|
const device = await this.deviceService.addDeviceUser(addDeviceDto);
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'device added successfully',
|
||||||
|
data: device,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('user/:userUuid')
|
||||||
|
async getDevicesByUser(@Param('userUuid') userUuid: string) {
|
||||||
|
try {
|
||||||
|
return await this.deviceService.getDevicesByUser(userUuid);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||||
@Get('room')
|
@Get('room')
|
||||||
@ -58,16 +94,19 @@ export class DeviceController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||||
@Post('room')
|
@Put('room')
|
||||||
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
|
async updateDeviceInRoom(
|
||||||
|
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const device =
|
const device = await this.deviceService.updateDeviceInRoom(
|
||||||
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
|
updateDeviceInRoomDto,
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'device added in room successfully',
|
message: 'device updated in room successfully',
|
||||||
data: device,
|
data: device,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class AddDeviceInRoomDto {
|
export class AddDeviceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'deviceTuyaUuid',
|
description: 'deviceTuyaUuid',
|
||||||
required: true,
|
required: true,
|
||||||
@ -10,6 +10,23 @@ export class AddDeviceInRoomDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public deviceTuyaUuid: string;
|
public deviceTuyaUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
}
|
||||||
|
export class UpdateDeviceInRoomDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'deviceUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public deviceUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'roomUuid',
|
description: 'roomUuid',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -8,8 +8,9 @@ import {
|
|||||||
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 {
|
import {
|
||||||
|
AddDeviceDto,
|
||||||
AddDeviceInGroupDto,
|
AddDeviceInGroupDto,
|
||||||
AddDeviceInRoomDto,
|
UpdateDeviceInRoomDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import {
|
import {
|
||||||
DeviceInstructionResponse,
|
DeviceInstructionResponse,
|
||||||
@ -47,6 +48,82 @@ export class DeviceService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async addDeviceUser(addDeviceDto: AddDeviceDto) {
|
||||||
|
try {
|
||||||
|
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
||||||
|
addDeviceDto.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!device.productUuid) {
|
||||||
|
throw new Error('Product UUID is missing for the device.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.deviceRepository.save({
|
||||||
|
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
|
||||||
|
productDevice: { uuid: device.productUuid },
|
||||||
|
user: {
|
||||||
|
uuid: addDeviceDto.userUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'Device already exists',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Failed to add device in room',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getDevicesByUser(
|
||||||
|
userUuid: string,
|
||||||
|
): Promise<GetDeviceDetailsInterface[]> {
|
||||||
|
try {
|
||||||
|
const devices = await this.deviceRepository.find({
|
||||||
|
where: {
|
||||||
|
user: { uuid: userUuid },
|
||||||
|
permission: {
|
||||||
|
userUuid,
|
||||||
|
permissionType: {
|
||||||
|
type: In([PermissionType.READ, PermissionType.CONTROLLABLE]),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relations: [
|
||||||
|
'spaceDevice',
|
||||||
|
'productDevice',
|
||||||
|
'permission',
|
||||||
|
'permission.permissionType',
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const devicesData = await Promise.all(
|
||||||
|
devices.map(async (device) => {
|
||||||
|
return {
|
||||||
|
haveRoom: device.spaceDevice ? true : false,
|
||||||
|
productUuid: device.productDevice.uuid,
|
||||||
|
productType: device.productDevice.prodType,
|
||||||
|
permissionType: device.permission[0].permissionType.type,
|
||||||
|
...(await this.getDeviceDetailsByDeviceIdTuya(
|
||||||
|
device.deviceTuyaUuid,
|
||||||
|
)),
|
||||||
|
uuid: device.uuid,
|
||||||
|
} as GetDeviceDetailsInterface;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return devicesData;
|
||||||
|
} catch (error) {
|
||||||
|
// Handle the error here
|
||||||
|
throw new HttpException(
|
||||||
|
'User does not have any devices',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async getDevicesByRoomId(
|
async getDevicesByRoomId(
|
||||||
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
||||||
userUuid: string,
|
userUuid: string,
|
||||||
@ -72,13 +149,14 @@ export class DeviceService {
|
|||||||
const devicesData = await Promise.all(
|
const devicesData = await Promise.all(
|
||||||
devices.map(async (device) => {
|
devices.map(async (device) => {
|
||||||
return {
|
return {
|
||||||
|
haveRoom: device.spaceDevice ? true : false,
|
||||||
|
productUuid: device.productDevice.uuid,
|
||||||
|
productType: device.productDevice.prodType,
|
||||||
|
permissionType: device.permission[0].permissionType.type,
|
||||||
...(await this.getDeviceDetailsByDeviceIdTuya(
|
...(await this.getDeviceDetailsByDeviceIdTuya(
|
||||||
device.deviceTuyaUuid,
|
device.deviceTuyaUuid,
|
||||||
)),
|
)),
|
||||||
uuid: device.uuid,
|
uuid: device.uuid,
|
||||||
productUuid: device.productDevice.uuid,
|
|
||||||
productType: device.productDevice.prodType,
|
|
||||||
permissionType: device.permission[0].permissionType.type,
|
|
||||||
} as GetDeviceDetailsInterface;
|
} as GetDeviceDetailsInterface;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -92,7 +170,31 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async updateDeviceInRoom(updateDeviceInRoomDto: UpdateDeviceInRoomDto) {
|
||||||
|
try {
|
||||||
|
await this.deviceRepository.update(
|
||||||
|
{ uuid: updateDeviceInRoomDto.deviceUuid },
|
||||||
|
{
|
||||||
|
spaceDevice: { uuid: updateDeviceInRoomDto.roomUuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const device = await this.deviceRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: updateDeviceInRoomDto.deviceUuid,
|
||||||
|
},
|
||||||
|
relations: ['spaceDevice'],
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
uuid: device.uuid,
|
||||||
|
roomUuid: device.spaceDevice.uuid,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Failed to add device in room',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async getDevicesByGroupId(
|
async getDevicesByGroupId(
|
||||||
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
||||||
userUuid: string,
|
userUuid: string,
|
||||||
@ -126,7 +228,6 @@ export class DeviceService {
|
|||||||
uuid: device.device.uuid,
|
uuid: device.device.uuid,
|
||||||
productUuid: device.device.productDevice.uuid,
|
productUuid: device.device.productDevice.uuid,
|
||||||
productType: device.device.productDevice.prodType,
|
productType: device.device.productDevice.prodType,
|
||||||
permissionType: device.device.permission[0].permissionType.type,
|
|
||||||
} as GetDeviceDetailsInterface;
|
} as GetDeviceDetailsInterface;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -139,35 +240,6 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async addDeviceInRoom(addDeviceInRoomDto: AddDeviceInRoomDto) {
|
|
||||||
try {
|
|
||||||
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
|
||||||
addDeviceInRoomDto.deviceTuyaUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!device.productUuid) {
|
|
||||||
throw new Error('Product UUID is missing for the device.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return await this.deviceRepository.save({
|
|
||||||
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
|
|
||||||
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
|
|
||||||
productDevice: { uuid: device.productUuid },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
if (error.code === '23505') {
|
|
||||||
throw new HttpException(
|
|
||||||
'Device already exists in the room',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
'Failed to add device in room',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -30,7 +30,7 @@ export class FloorController {
|
|||||||
constructor(private readonly floorService: FloorService) {}
|
constructor(private readonly floorService: FloorService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard, CheckBuildingTypeGuard)
|
@UseGuards(JwtAuthGuard, CheckBuildingTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
||||||
try {
|
try {
|
||||||
|
89
src/guards/device.guard.ts
Normal file
89
src/guards/device.guard.ts
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
|
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckDeviceGuard implements CanActivate {
|
||||||
|
private tuya: TuyaContext;
|
||||||
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
private readonly deviceRepository: DeviceRepository,
|
||||||
|
) {
|
||||||
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
|
this.tuya = new TuyaContext({
|
||||||
|
baseUrl: 'https://openapi.tuyaeu.com',
|
||||||
|
accessKey,
|
||||||
|
secretKey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (req.body && req.body.userUuid && req.body.deviceTuyaUuid) {
|
||||||
|
const { userUuid, deviceTuyaUuid } = req.body;
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
|
||||||
|
} else {
|
||||||
|
throw new BadRequestException('Invalid request parameters');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const user = await this.userRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: userUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!user) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
||||||
|
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
throw new NotFoundException('Device not found from Tuya');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: error.message || 'Invalid UUID',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -74,7 +74,7 @@ export class CheckGroupGuard implements CanActivate {
|
|||||||
} else {
|
} else {
|
||||||
response.status(HttpStatus.BAD_REQUEST).json({
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
message: 'Invalid UUID',
|
message: error.message || 'Invalid UUID',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,29 +4,17 @@ import {
|
|||||||
Injectable,
|
Injectable,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
||||||
|
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CheckRoomGuard implements CanActivate {
|
export class CheckRoomGuard implements CanActivate {
|
||||||
private tuya: TuyaContext;
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
) {
|
) {}
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
|
||||||
this.tuya = new TuyaContext({
|
|
||||||
baseUrl: 'https://openapi.tuyaeu.com',
|
|
||||||
accessKey,
|
|
||||||
secretKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
const req = context.switchToHttp().getRequest();
|
const req = context.switchToHttp().getRequest();
|
||||||
@ -35,10 +23,10 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
if (req.query && req.query.roomUuid) {
|
if (req.query && req.query.roomUuid) {
|
||||||
const { roomUuid } = req.query;
|
const { roomUuid } = req.query;
|
||||||
await this.checkRoomIsFound(roomUuid);
|
await this.checkRoomIsFound(roomUuid);
|
||||||
} else if (req.body && req.body.roomUuid && req.body.deviceTuyaUuid) {
|
} else if (req.body && req.body.roomUuid && req.body.deviceUuid) {
|
||||||
const { roomUuid, deviceTuyaUuid } = req.body;
|
const { roomUuid, deviceUuid } = req.body;
|
||||||
await this.checkRoomIsFound(roomUuid);
|
await this.checkRoomIsFound(roomUuid);
|
||||||
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
|
await this.checkDeviceIsFound(deviceUuid);
|
||||||
} else {
|
} else {
|
||||||
throw new BadRequestException('Invalid request parameters');
|
throw new BadRequestException('Invalid request parameters');
|
||||||
}
|
}
|
||||||
@ -63,14 +51,14 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
throw new NotFoundException('Room not found');
|
throw new NotFoundException('Room not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
async checkDeviceIsFound(deviceUuid: string) {
|
||||||
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
const response = await this.deviceRepository.findOne({
|
||||||
const response = await this.tuya.request({
|
where: {
|
||||||
method: 'GET',
|
uuid: deviceUuid,
|
||||||
path,
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.uuid) {
|
||||||
throw new NotFoundException('Device not found');
|
throw new NotFoundException('Device not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,7 +76,7 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
} else {
|
} else {
|
||||||
response.status(HttpStatus.BAD_REQUEST).json({
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
message: 'Invalid UUID',
|
message: error.message || 'Invalid UUID',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ export class RoomController {
|
|||||||
constructor(private readonly roomService: RoomService) {}
|
constructor(private readonly roomService: RoomService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard, CheckUnitTypeGuard)
|
@UseGuards(JwtAuthGuard, CheckUnitTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -12,7 +12,11 @@ import {
|
|||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AddUnitDto, AddUserUnitDto } from '../dtos/add.unit.dto';
|
import {
|
||||||
|
AddUnitDto,
|
||||||
|
AddUserUnitDto,
|
||||||
|
AddUserUnitUsingCodeDto,
|
||||||
|
} 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';
|
||||||
@ -30,7 +34,7 @@ export class UnitController {
|
|||||||
constructor(private readonly unitService: UnitService) {}
|
constructor(private readonly unitService: UnitService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard, CheckFloorTypeGuard)
|
@UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
||||||
try {
|
try {
|
||||||
@ -147,4 +151,39 @@ export class UnitController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
|
@Get(':unitUuid/invitation-code')
|
||||||
|
async getUnitInvitationCode(@Param('unitUuid') unitUuid: string) {
|
||||||
|
try {
|
||||||
|
const unit = await this.unitService.getUnitInvitationCode(unitUuid);
|
||||||
|
return unit;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('user/verify-code')
|
||||||
|
async verifyCodeAndAddUserUnit(
|
||||||
|
@Body() addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'user unit added successfully',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,3 +40,29 @@ export class AddUserUnitDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddUserUnitUsingCodeDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'unitUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public unitUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'inviteCode',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public inviteCode: string;
|
||||||
|
constructor(dto: Partial<AddUserUnitDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@ 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, AddUserUnitDto } from '../dtos';
|
import { AddUnitDto, AddUserUnitDto, AddUserUnitUsingCodeDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
UnitChildInterface,
|
UnitChildInterface,
|
||||||
UnitParentInterface,
|
UnitParentInterface,
|
||||||
@ -18,6 +18,7 @@ import {
|
|||||||
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';
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
|
import { generateRandomString } from '@app/common/helper/randomString';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UnitService {
|
export class UnitService {
|
||||||
@ -227,7 +228,7 @@ export class UnitService {
|
|||||||
|
|
||||||
async addUserUnit(addUserUnitDto: AddUserUnitDto) {
|
async addUserUnit(addUserUnitDto: AddUserUnitDto) {
|
||||||
try {
|
try {
|
||||||
await this.userSpaceRepository.save({
|
return await this.userSpaceRepository.save({
|
||||||
user: { uuid: addUserUnitDto.userUuid },
|
user: { uuid: addUserUnitDto.userUuid },
|
||||||
space: { uuid: addUserUnitDto.unitUuid },
|
space: { uuid: addUserUnitDto.unitUuid },
|
||||||
});
|
});
|
||||||
@ -282,4 +283,61 @@ export class UnitService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getUnitInvitationCode(unitUuid: string): Promise<any> {
|
||||||
|
try {
|
||||||
|
// Generate a 6-character random invitation code
|
||||||
|
const invitationCode = generateRandomString(6);
|
||||||
|
|
||||||
|
// Update the unit with the new invitation code
|
||||||
|
await this.spaceRepository.update({ uuid: unitUuid }, { invitationCode });
|
||||||
|
|
||||||
|
// Fetch the updated unit
|
||||||
|
const updatedUnit = await this.spaceRepository.findOneOrFail({
|
||||||
|
where: { uuid: unitUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
uuid: updatedUnit.uuid,
|
||||||
|
invitationCode: updatedUnit.invitationCode,
|
||||||
|
type: updatedUnit.spaceType.type,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async verifyCodeAndAddUserUnit(
|
||||||
|
addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const unit = await this.spaceRepository.findOneOrFail({
|
||||||
|
where: {
|
||||||
|
invitationCode: addUserUnitUsingCodeDto.inviteCode,
|
||||||
|
spaceType: { type: 'unit' },
|
||||||
|
},
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (unit.invitationCode) {
|
||||||
|
const user = await this.addUserUnit({
|
||||||
|
userUuid: addUserUnitUsingCodeDto.userUuid,
|
||||||
|
unitUuid: unit.uuid,
|
||||||
|
});
|
||||||
|
if (user.uuid) {
|
||||||
|
await this.spaceRepository.update(
|
||||||
|
{ uuid: unit.uuid },
|
||||||
|
{ invitationCode: null },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Invalid invitation code',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user