mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Refactor activation code handling and user space verification
This commit is contained in:
@ -76,9 +76,7 @@ export class InviteUserController {
|
||||
async activationCodeController(
|
||||
@Body() activateCodeDto: ActivateCodeDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.inviteUserService.activationCodeController(
|
||||
activateCodeDto,
|
||||
);
|
||||
return await this.inviteUserService.activationCode(activateCodeDto);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
|
@ -23,12 +23,14 @@ import { SpaceModelRepository } from '@app/common/modules/space-model';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { UserSpaceService } from 'src/users/services';
|
||||
import { UserService, UserSpaceService } from 'src/users/services';
|
||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import { DeviceUserPermissionRepository } from '@app/common/modules/device/repositories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import { ProjectUserService } from 'src/project/services/project-user.service';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, InviteUserRepositoryModule],
|
||||
@ -55,6 +57,9 @@ import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
ProjectUserService,
|
||||
RoleTypeRepository,
|
||||
InviteSpaceRepository,
|
||||
UserService,
|
||||
RegionRepository,
|
||||
TimeZoneRepository,
|
||||
],
|
||||
exports: [InviteUserService],
|
||||
})
|
||||
|
@ -29,6 +29,7 @@ import {
|
||||
UpdateUserInvitationDto,
|
||||
} from '../dtos/update.invite-user.dto';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { InviteUserEntity } from '@app/common/modules/Invite-user/entities';
|
||||
|
||||
@Injectable()
|
||||
export class InviteUserService {
|
||||
@ -188,76 +189,35 @@ export class InviteUserService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async activationCodeController(
|
||||
dto: ActivateCodeDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const { activationCode, userUuid } = dto;
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid, isActive: true, isUserVerified: true },
|
||||
});
|
||||
async activationCode(dto: ActivateCodeDto): Promise<BaseResponseDto> {
|
||||
const { activationCode, userUuid } = dto;
|
||||
|
||||
try {
|
||||
const user = await this.getUser(userUuid);
|
||||
|
||||
if (!user) {
|
||||
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
const { email } = user;
|
||||
const invitedUser = await this.inviteUserRepository.findOne({
|
||||
where: {
|
||||
email,
|
||||
invitationCode: activationCode,
|
||||
email: user.email,
|
||||
status: UserStatusEnum.INVITED,
|
||||
isActive: true,
|
||||
},
|
||||
relations: ['project', 'spaces.space.community', 'roleType'],
|
||||
});
|
||||
|
||||
if (!invitedUser) {
|
||||
throw new HttpException(
|
||||
'Invalid activation code',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
for (const invitedSpace of invitedUser.spaces) {
|
||||
try {
|
||||
const deviceUUIDs =
|
||||
await this.userSpaceService.getDeviceUUIDsForSpace(
|
||||
invitedSpace.space.uuid,
|
||||
);
|
||||
|
||||
await this.userSpaceService.addUserPermissionsToDevices(
|
||||
userUuid,
|
||||
deviceUUIDs,
|
||||
if (invitedUser) {
|
||||
if (invitedUser.invitationCode !== activationCode) {
|
||||
throw new HttpException(
|
||||
'Invalid activation code',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
|
||||
await this.spaceUserService.associateUserToSpace({
|
||||
communityUuid: invitedSpace.space.community.uuid,
|
||||
spaceUuid: invitedSpace.space.uuid,
|
||||
userUuid: user.uuid,
|
||||
projectUuid: invitedUser.project.uuid,
|
||||
});
|
||||
} catch (spaceError) {
|
||||
console.error(
|
||||
`Error processing space ${invitedSpace.space.uuid}:`,
|
||||
spaceError,
|
||||
);
|
||||
// Skip to the next space
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
await this.inviteUserRepository.update(
|
||||
{ uuid: invitedUser.uuid },
|
||||
{ status: UserStatusEnum.ACTIVE },
|
||||
);
|
||||
await this.userRepository.update(
|
||||
{ uuid: userUuid },
|
||||
{
|
||||
project: { uuid: invitedUser.project.uuid },
|
||||
inviteUser: { uuid: invitedUser.uuid },
|
||||
roleType: { uuid: invitedUser.roleType.uuid },
|
||||
},
|
||||
);
|
||||
// Handle invited user with valid activation code
|
||||
await this.handleInvitedUser(user, invitedUser);
|
||||
} else {
|
||||
// Handle case for non-invited user
|
||||
await this.handleNonInvitedUser(activationCode, userUuid);
|
||||
}
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
success: true,
|
||||
@ -272,6 +232,73 @@ export class InviteUserService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async getUser(userUuid: string): Promise<UserEntity> {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid, isActive: true, isUserVerified: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
private async handleNonInvitedUser(
|
||||
activationCode: string,
|
||||
userUuid: string,
|
||||
): Promise<void> {
|
||||
await this.userSpaceService.verifyCodeAndAddUserSpace(
|
||||
{ inviteCode: activationCode },
|
||||
userUuid,
|
||||
);
|
||||
}
|
||||
|
||||
private async handleInvitedUser(
|
||||
user: UserEntity,
|
||||
invitedUser: InviteUserEntity,
|
||||
): Promise<void> {
|
||||
for (const invitedSpace of invitedUser.spaces) {
|
||||
try {
|
||||
const deviceUUIDs = await this.userSpaceService.getDeviceUUIDsForSpace(
|
||||
invitedSpace.space.uuid,
|
||||
);
|
||||
|
||||
await this.userSpaceService.addUserPermissionsToDevices(
|
||||
user.uuid,
|
||||
deviceUUIDs,
|
||||
);
|
||||
|
||||
await this.spaceUserService.associateUserToSpace({
|
||||
communityUuid: invitedSpace.space.community.uuid,
|
||||
spaceUuid: invitedSpace.space.uuid,
|
||||
userUuid: user.uuid,
|
||||
projectUuid: invitedUser.project.uuid,
|
||||
});
|
||||
} catch (spaceError) {
|
||||
console.error(
|
||||
`Error processing space ${invitedSpace.space.uuid}:`,
|
||||
spaceError,
|
||||
);
|
||||
continue; // Skip to the next space
|
||||
}
|
||||
}
|
||||
|
||||
// Update invited user and associated user data
|
||||
await this.inviteUserRepository.update(
|
||||
{ uuid: invitedUser.uuid },
|
||||
{ status: UserStatusEnum.ACTIVE },
|
||||
);
|
||||
await this.userRepository.update(
|
||||
{ uuid: user.uuid },
|
||||
{
|
||||
project: { uuid: invitedUser.project.uuid },
|
||||
inviteUser: { uuid: invitedUser.uuid },
|
||||
roleType: { uuid: invitedUser.roleType.uuid },
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async updateUserInvitation(
|
||||
dto: UpdateUserInvitationDto,
|
||||
invitedUserUuid: string,
|
||||
|
Reference in New Issue
Block a user