mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 14:57:54 +00:00
Fixed user space permission
This commit is contained in:
@@ -2,10 +2,20 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { AddUserSpaceDto, AddUserSpaceUsingCodeDto } from '../dtos';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
|
||||
@Injectable()
|
||||
export class UserSpaceService {
|
||||
constructor(private readonly userSpaceRepository: UserSpaceRepository) {}
|
||||
constructor(
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly userDevicePermissionService: UserDevicePermissionService,
|
||||
) {}
|
||||
|
||||
async getSpacesForUser(userUuid: string): Promise<BaseResponseDto> {
|
||||
const userSpaces = await this.userSpaceRepository.find({
|
||||
@@ -25,4 +35,110 @@ export class UserSpaceService {
|
||||
message: `Spaces for user ${userUuid} retrieved successfully`,
|
||||
});
|
||||
}
|
||||
|
||||
async verifyCodeAndAddUserSpace(
|
||||
params: AddUserSpaceUsingCodeDto,
|
||||
userUuid: string,
|
||||
) {
|
||||
try {
|
||||
const space = await this.findUnitByInviteCode(params.inviteCode);
|
||||
|
||||
await this.addUserToSpace(userUuid, space.uuid);
|
||||
|
||||
await this.clearUnitInvitationCode(space.uuid);
|
||||
|
||||
const deviceUUIDs = await this.getDeviceUUIDsForSpace(space.uuid);
|
||||
|
||||
await this.addUserPermissionsToDevices(userUuid, deviceUUIDs);
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
'Invalid invitation code',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
const user = await this.addUserSpace({ userUuid, spaceUuid });
|
||||
|
||||
if (user.uuid) {
|
||||
return user;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'Failed to add user to unit',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async addUserSpace(addUserSpaceDto: AddUserSpaceDto) {
|
||||
try {
|
||||
return await this.userSpaceRepository.save({
|
||||
user: { uuid: addUserSpaceDto.userUuid },
|
||||
space: { uuid: addUserSpaceDto.spaceUuid },
|
||||
});
|
||||
} catch (err) {
|
||||
if (err.code === CommonErrorCodes.DUPLICATE_ENTITY) {
|
||||
throw new HttpException(
|
||||
'User already belongs to this unit',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
throw new HttpException(
|
||||
err.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async clearUnitInvitationCode(spaceUuid: string) {
|
||||
await this.spaceRepository.update(
|
||||
{ uuid: spaceUuid },
|
||||
{ invitationCode: null },
|
||||
);
|
||||
}
|
||||
|
||||
private async getDeviceUUIDsForSpace(
|
||||
unitUuid: string,
|
||||
): Promise<{ uuid: string }[]> {
|
||||
const devices = await this.spaceRepository.find({
|
||||
where: { uuid: unitUuid },
|
||||
relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'],
|
||||
});
|
||||
|
||||
const allDevices = devices.flatMap((space) => space.devices);
|
||||
|
||||
return allDevices.map((device) => ({ uuid: device.uuid }));
|
||||
}
|
||||
|
||||
private async addUserPermissionsToDevices(
|
||||
userUuid: string,
|
||||
deviceUUIDs: { uuid: string }[],
|
||||
): Promise<void> {
|
||||
const permissionPromises = deviceUUIDs.map(async (device) => {
|
||||
try {
|
||||
await this.userDevicePermissionService.addUserPermission({
|
||||
userUuid,
|
||||
deviceUuid: device.uuid,
|
||||
permissionType: PermissionType.CONTROLLABLE,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to add permission for device ${device.uuid}: ${error.message}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(permissionPromises);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user