mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 09:34:54 +00:00
Merge pull request #141 from SyncrowIOT/feature/space-management
Feature/space management
This commit is contained in:
@ -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`,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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(
|
throw new HttpException(
|
||||||
'Invalid invitation code',
|
`An unexpected error occurred: ${err.message}`,
|
||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async findUnitByInviteCode(inviteCode: string): Promise<SpaceEntity> {
|
private async findSpaceByInviteCode(
|
||||||
|
inviteCode: string,
|
||||||
|
): Promise<SpaceEntity> {
|
||||||
|
try {
|
||||||
const space = await this.spaceRepository.findOneOrFail({
|
const space = await this.spaceRepository.findOneOrFail({
|
||||||
where: {
|
where: {
|
||||||
invitationCode: inviteCode,
|
invitationCode: inviteCode,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return space;
|
return space;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Space with the provided invite code not found',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async addUserToSpace(userUuid: string, spaceUuid: string) {
|
private async addUserToSpace(userUuid: string, spaceUuid: string) {
|
||||||
|
try {
|
||||||
const user = await this.addUserSpace({ userUuid, spaceUuid });
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user