diff --git a/src/scene/services/scene.service.ts b/src/scene/services/scene.service.ts index 0208700..6ec33dd 100644 --- a/src/scene/services/scene.service.ts +++ b/src/scene/services/scene.service.ts @@ -457,6 +457,9 @@ export class SceneService { const space = await this.getSpaceByUuid(scene.spaceUuid); await this.delete(scene.sceneTuyaUuid, space.spaceTuyaUuid); + + await this.sceneRepository.remove(scene); + return new SuccessResponseDto({ message: `Scene with ID ${sceneUuid} deleted successfully`, }); diff --git a/src/users/services/user-space.service.ts b/src/users/services/user-space.service.ts index ca3b8d9..d6945c9 100644 --- a/src/users/services/user-space.service.ts +++ b/src/users/services/user-space.service.ts @@ -41,7 +41,7 @@ export class UserSpaceService { userUuid: string, ) { try { - const space = await this.findUnitByInviteCode(params.inviteCode); + const space = await this.findSpaceByInviteCode(params.inviteCode); await this.addUserToSpace(userUuid, space.uuid); @@ -51,31 +51,43 @@ export class UserSpaceService { await this.addUserPermissionsToDevices(userUuid, deviceUUIDs); } catch (err) { + if (err instanceof HttpException) { + throw err; + } else { + throw new HttpException( + `An unexpected error occurred: ${err.message}`, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } + + private async findSpaceByInviteCode( + inviteCode: string, + ): Promise { + try { + const space = await this.spaceRepository.findOneOrFail({ + where: { + invitationCode: inviteCode, + }, + }); + return space; + } catch (error) { throw new HttpException( - 'Invalid invitation code', - HttpStatus.BAD_REQUEST, + 'Space with the provided invite code not found', + HttpStatus.NOT_FOUND, ); } } - private async findUnitByInviteCode(inviteCode: string): Promise { - const space = await this.spaceRepository.findOneOrFail({ - where: { - invitationCode: inviteCode, - }, - }); - - return space; - } - private async addUserToSpace(userUuid: string, spaceUuid: string) { - const user = await this.addUserSpace({ userUuid, spaceUuid }); + try { + const user = await this.addUserSpace({ userUuid, spaceUuid }); - if (user.uuid) { return user; - } else { + } catch (error) { throw new HttpException( - 'Failed to add user to unit', + `An error occurred while adding user to space ${spaceUuid} : ${error.message}}`, HttpStatus.INTERNAL_SERVER_ERROR, ); } @@ -113,7 +125,7 @@ export class UserSpaceService { ): Promise<{ uuid: string }[]> { const devices = await this.spaceRepository.find({ where: { uuid: unitUuid }, - relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'], + relations: ['devices', 'devices.productDevice'], }); const allDevices = devices.flatMap((space) => space.devices);