fixed user invite

This commit is contained in:
hannathkadher
2024-11-06 19:03:00 +04:00
parent 6df2f11371
commit cd7e7181ba
+30 -18
View File
@@ -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<SpaceEntity> {
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<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 });
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);