mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 20:47:56 +00:00
Refactor activation code handling and user space verification
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
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';
|
||||
@@ -11,6 +16,10 @@ 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 { InviteSpaceEntity } from '@app/common/modules/space/entities/invite-space.entity';
|
||||
import { UserService } from './user.service';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
|
||||
@Injectable()
|
||||
export class UserSpaceService {
|
||||
@@ -18,6 +27,8 @@ export class UserSpaceService {
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly inviteSpaceRepository: InviteSpaceRepository,
|
||||
private readonly userService: UserService,
|
||||
private readonly inviteUserRepository: InviteUserRepository,
|
||||
private readonly userDevicePermissionService: UserDevicePermissionService,
|
||||
) {}
|
||||
|
||||
@@ -41,17 +52,24 @@ export class UserSpaceService {
|
||||
params: AddUserSpaceUsingCodeDto,
|
||||
userUuid: string,
|
||||
) {
|
||||
const { inviteCode } = params;
|
||||
try {
|
||||
const inviteSpace = await this.findInviteSpaceByInviteCode(
|
||||
params.inviteCode,
|
||||
const inviteSpace = await this.findInviteSpaceByInviteCode(inviteCode);
|
||||
const user = await this.userService.getUserDetailsByUserUuid(
|
||||
userUuid,
|
||||
true,
|
||||
);
|
||||
|
||||
await this.checkSpaceMemberRole(user);
|
||||
await this.addUserToSpace(userUuid, inviteSpace.space.uuid);
|
||||
|
||||
const deviceUUIDs = await this.getDeviceUUIDsForSpace(inviteSpace.uuid);
|
||||
|
||||
await this.addUserPermissionsToDevices(userUuid, deviceUUIDs);
|
||||
|
||||
await this.addUserAsActiveInvitation(
|
||||
user,
|
||||
inviteSpace.space.uuid,
|
||||
inviteCode,
|
||||
);
|
||||
await this.clearSpaceInvitationCode(inviteSpace.uuid);
|
||||
} catch (err) {
|
||||
if (err instanceof HttpException) {
|
||||
@@ -64,7 +82,20 @@ export class UserSpaceService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async checkSpaceMemberRole(user: any) {
|
||||
try {
|
||||
if (user.role.type !== RoleType.SPACE_MEMBER) {
|
||||
throw new BadRequestException(
|
||||
'You have to be a space member to join this space',
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
err.message || 'User not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
private async findInviteSpaceByInviteCode(
|
||||
inviteCode: string,
|
||||
): Promise<InviteSpaceEntity> {
|
||||
@@ -90,7 +121,47 @@ export class UserSpaceService {
|
||||
{ isActive: false },
|
||||
);
|
||||
}
|
||||
async getProjectBySpaceUuid(spaceUuid: string) {
|
||||
try {
|
||||
const project = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: spaceUuid,
|
||||
},
|
||||
relations: ['community.project'],
|
||||
});
|
||||
return project;
|
||||
} catch (error) {
|
||||
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
private async addUserAsActiveInvitation(
|
||||
user: any,
|
||||
spaceUuid: string,
|
||||
inviteCode: string,
|
||||
) {
|
||||
try {
|
||||
const space = await this.getProjectBySpaceUuid(spaceUuid);
|
||||
|
||||
const inviteUser = this.inviteUserRepository.create({
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
email: user.email,
|
||||
jobTitle: null,
|
||||
phoneNumber: null,
|
||||
roleType: { uuid: user.role.uuid },
|
||||
status: UserStatusEnum.ACTIVE,
|
||||
invitationCode: inviteCode,
|
||||
invitedBy: RoleType.SPACE_OWNER,
|
||||
project: { uuid: space.community.project.uuid },
|
||||
});
|
||||
await this.inviteUserRepository.save(inviteUser);
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
err.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
private async addUserToSpace(userUuid: string, spaceUuid: string) {
|
||||
try {
|
||||
const user = await this.addUserSpace({ userUuid, spaceUuid });
|
||||
|
||||
@@ -23,13 +23,15 @@ export class UserService {
|
||||
private readonly regionRepository: RegionRepository,
|
||||
private readonly timeZoneRepository: TimeZoneRepository,
|
||||
) {}
|
||||
async getUserDetailsByUserUuid(userUuid: string) {
|
||||
async getUserDetailsByUserUuid(userUuid: string, withRole = false) {
|
||||
try {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: {
|
||||
uuid: userUuid,
|
||||
},
|
||||
relations: ['region', 'timezone'],
|
||||
...(withRole
|
||||
? { relations: ['roleType'] }
|
||||
: { relations: ['region', 'timezone'] }),
|
||||
});
|
||||
if (!user) {
|
||||
throw new BadRequestException('Invalid room UUID');
|
||||
@@ -39,13 +41,14 @@ export class UserService {
|
||||
const cleanedProfilePicture = removeBase64Prefix(user.profilePicture);
|
||||
|
||||
return {
|
||||
uuid: user.uuid,
|
||||
email: user.email,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
uuid: user?.uuid,
|
||||
email: user?.email,
|
||||
firstName: user?.firstName,
|
||||
lastName: user?.lastName,
|
||||
profilePicture: cleanedProfilePicture,
|
||||
region: user.region,
|
||||
timeZone: user.timezone,
|
||||
region: user?.region,
|
||||
timeZone: user?.timezone,
|
||||
...(withRole && { role: user?.roleType }),
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
|
||||
Reference in New Issue
Block a user