mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 23:27:31 +00:00
Add activation code endpoint
This commit is contained in:
@ -760,6 +760,12 @@ export class ControllerRoute {
|
|||||||
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
||||||
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
||||||
|
|
||||||
|
public static readonly ACTIVATION_CODE_SUMMARY =
|
||||||
|
'Activate Invitation Code';
|
||||||
|
|
||||||
|
public static readonly ACTIVATION_CODE_DESCRIPTION =
|
||||||
|
'This endpoint activate invitation code';
|
||||||
|
|
||||||
public static readonly CHECK_EMAIL_SUMMARY = 'Check email';
|
public static readonly CHECK_EMAIL_SUMMARY = 'Check email';
|
||||||
|
|
||||||
public static readonly CHECK_EMAIL_DESCRIPTION =
|
public static readonly CHECK_EMAIL_DESCRIPTION =
|
||||||
|
@ -8,6 +8,7 @@ import { PermissionsGuard } from 'src/guards/permissions.guard';
|
|||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||||
|
import { ActivateCodeDto } from '../dtos/active-code.dto';
|
||||||
|
|
||||||
@ApiTags('Invite User Module')
|
@ApiTags('Invite User Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -51,4 +52,19 @@ export class InviteUserController {
|
|||||||
addUserInvitationDto,
|
addUserInvitationDto,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('activation')
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.INVITE_USER.ACTIONS.ACTIVATION_CODE_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.INVITE_USER.ACTIONS.ACTIVATION_CODE_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async activationCodeController(
|
||||||
|
@Body() activateCodeDto: ActivateCodeDto,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
return await this.inviteUserService.activationCodeController(
|
||||||
|
activateCodeDto,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
22
src/invite-user/dtos/active-code.dto.ts
Normal file
22
src/invite-user/dtos/active-code.dto.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsString, IsNotEmpty } from 'class-validator';
|
||||||
|
|
||||||
|
export class ActivateCodeDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'The activation code of the user',
|
||||||
|
example: '7CvRcA',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
activationCode: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'The UUID of the user',
|
||||||
|
example: 'd7a44e8a-32d5-4f39-ae2e-013f1245aead',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
userUuid: string;
|
||||||
|
}
|
@ -3,13 +3,27 @@ import { InviteUserService } from './services/invite-user.service';
|
|||||||
import { InviteUserController } from './controllers/invite-user.controller';
|
import { InviteUserController } from './controllers/invite-user.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
|
||||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
import {
|
||||||
|
UserRepository,
|
||||||
|
UserSpaceRepository,
|
||||||
|
} from '@app/common/modules/user/repositories';
|
||||||
import { InviteUserRepositoryModule } from '@app/common/modules/Invite-user/Invite-user.repository.module';
|
import { InviteUserRepositoryModule } from '@app/common/modules/Invite-user/Invite-user.repository.module';
|
||||||
import {
|
import {
|
||||||
InviteUserRepository,
|
InviteUserRepository,
|
||||||
InviteUserSpaceRepository,
|
InviteUserSpaceRepository,
|
||||||
} from '@app/common/modules/Invite-user/repositiories';
|
} from '@app/common/modules/Invite-user/repositiories';
|
||||||
import { EmailService } from '@app/common/util/email.service';
|
import { EmailService } from '@app/common/util/email.service';
|
||||||
|
import { SpaceUserService, ValidationService } from 'src/space/services';
|
||||||
|
import { CommunityService } from 'src/community/services';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space';
|
||||||
|
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 { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||||
|
import { DeviceUserPermissionRepository } from '@app/common/modules/device/repositories';
|
||||||
|
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, InviteUserRepositoryModule],
|
imports: [ConfigModule, InviteUserRepositoryModule],
|
||||||
@ -20,6 +34,19 @@ import { EmailService } from '@app/common/util/email.service';
|
|||||||
UserRepository,
|
UserRepository,
|
||||||
EmailService,
|
EmailService,
|
||||||
InviteUserSpaceRepository,
|
InviteUserSpaceRepository,
|
||||||
|
SpaceUserService,
|
||||||
|
ValidationService,
|
||||||
|
UserSpaceRepository,
|
||||||
|
CommunityService,
|
||||||
|
SpaceRepository,
|
||||||
|
SpaceModelRepository,
|
||||||
|
CommunityRepository,
|
||||||
|
ProjectRepository,
|
||||||
|
TuyaService,
|
||||||
|
UserSpaceService,
|
||||||
|
UserDevicePermissionService,
|
||||||
|
DeviceUserPermissionRepository,
|
||||||
|
PermissionTypeRepository,
|
||||||
],
|
],
|
||||||
exports: [InviteUserService],
|
exports: [InviteUserService],
|
||||||
})
|
})
|
||||||
|
@ -16,6 +16,9 @@ import { CheckEmailDto } from '../dtos/check-email.dto';
|
|||||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
import { EmailService } from '@app/common/util/email.service';
|
import { EmailService } from '@app/common/util/email.service';
|
||||||
import { SpaceEntity } from '@app/common/modules/space';
|
import { SpaceEntity } from '@app/common/modules/space';
|
||||||
|
import { ActivateCodeDto } from '../dtos/active-code.dto';
|
||||||
|
import { UserSpaceService } from 'src/users/services';
|
||||||
|
import { SpaceUserService } from 'src/space/services';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class InviteUserService {
|
export class InviteUserService {
|
||||||
@ -24,6 +27,9 @@ export class InviteUserService {
|
|||||||
private readonly userRepository: UserRepository,
|
private readonly userRepository: UserRepository,
|
||||||
private readonly emailService: EmailService,
|
private readonly emailService: EmailService,
|
||||||
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
||||||
|
private readonly userSpaceService: UserSpaceService,
|
||||||
|
private readonly spaceUserService: SpaceUserService,
|
||||||
|
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@ -171,4 +177,86 @@ 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 },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
const { email } = user;
|
||||||
|
const invitedUser = await this.inviteUserRepository.findOne({
|
||||||
|
where: {
|
||||||
|
email,
|
||||||
|
invitationCode: activationCode,
|
||||||
|
status: UserStatusEnum.INVITED,
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
relations: ['project', 'spaces.space.community'],
|
||||||
|
});
|
||||||
|
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
|
||||||
|
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 },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
success: true,
|
||||||
|
message: 'The code has been successfully activated',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error activating the code:', error);
|
||||||
|
throw new HttpException(
|
||||||
|
error.message ||
|
||||||
|
'An unexpected error occurred while activating the code',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,9 +117,7 @@ export class UserSpaceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getDeviceUUIDsForSpace(
|
async getDeviceUUIDsForSpace(unitUuid: string): Promise<{ uuid: string }[]> {
|
||||||
unitUuid: string,
|
|
||||||
): Promise<{ uuid: string }[]> {
|
|
||||||
const devices = await this.spaceRepository.find({
|
const devices = await this.spaceRepository.find({
|
||||||
where: { uuid: unitUuid },
|
where: { uuid: unitUuid },
|
||||||
relations: ['devices', 'devices.productDevice'],
|
relations: ['devices', 'devices.productDevice'],
|
||||||
@ -130,7 +128,7 @@ export class UserSpaceService {
|
|||||||
return allDevices.map((device) => ({ uuid: device.uuid }));
|
return allDevices.map((device) => ({ uuid: device.uuid }));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async addUserPermissionsToDevices(
|
async addUserPermissionsToDevices(
|
||||||
userUuid: string,
|
userUuid: string,
|
||||||
deviceUUIDs: { uuid: string }[],
|
deviceUUIDs: { uuid: string }[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
Reference in New Issue
Block a user