Merge pull request #141 from SyncrowIOT/feature/space-management

Feature/space management
This commit is contained in:
faris Aljohari
2024-11-06 18:28:05 -06:00
committed by GitHub
2 changed files with 33 additions and 18 deletions

View File

@ -457,6 +457,9 @@ export class SceneService {
const space = await this.getSpaceByUuid(scene.spaceUuid); const space = await this.getSpaceByUuid(scene.spaceUuid);
await this.delete(scene.sceneTuyaUuid, space.spaceTuyaUuid); await this.delete(scene.sceneTuyaUuid, space.spaceTuyaUuid);
await this.sceneRepository.remove(scene);
return new SuccessResponseDto({ return new SuccessResponseDto({
message: `Scene with ID ${sceneUuid} deleted successfully`, message: `Scene with ID ${sceneUuid} deleted successfully`,
}); });

View File

@ -41,7 +41,7 @@ export class UserSpaceService {
userUuid: string, userUuid: string,
) { ) {
try { try {
const space = await this.findUnitByInviteCode(params.inviteCode); const space = await this.findSpaceByInviteCode(params.inviteCode);
await this.addUserToSpace(userUuid, space.uuid); await this.addUserToSpace(userUuid, space.uuid);
@ -51,31 +51,43 @@ export class UserSpaceService {
await this.addUserPermissionsToDevices(userUuid, deviceUUIDs); await this.addUserPermissionsToDevices(userUuid, deviceUUIDs);
} catch (err) { } 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<SpaceEntity> {
try {
const space = await this.spaceRepository.findOneOrFail({
where: {
invitationCode: inviteCode,
},
});
return space;
} catch (error) {
throw new HttpException( throw new HttpException(
'Invalid invitation code', 'Space with the provided invite code not found',
HttpStatus.BAD_REQUEST, HttpStatus.NOT_FOUND,
); );
} }
} }
private async findUnitByInviteCode(inviteCode: string): Promise<SpaceEntity> {
const space = await this.spaceRepository.findOneOrFail({
where: {
invitationCode: inviteCode,
},
});
return space;
}
private async addUserToSpace(userUuid: string, spaceUuid: string) { 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; return user;
} else { } catch (error) {
throw new HttpException( throw new HttpException(
'Failed to add user to unit', `An error occurred while adding user to space ${spaceUuid} : ${error.message}}`,
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
@ -113,7 +125,7 @@ export class UserSpaceService {
): Promise<{ uuid: string }[]> { ): Promise<{ uuid: string }[]> {
const devices = await this.spaceRepository.find({ const devices = await this.spaceRepository.find({
where: { uuid: unitUuid }, where: { uuid: unitUuid },
relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'], relations: ['devices', 'devices.productDevice'],
}); });
const allDevices = devices.flatMap((space) => space.devices); const allDevices = devices.flatMap((space) => space.devices);