mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Add endpoint to check email and project existence
This commit is contained in:
@ -733,6 +733,10 @@ export class ControllerRoute {
|
||||
|
||||
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
||||
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
||||
public static readonly CHECK_EMAIL_SUMMARY = 'Check email';
|
||||
|
||||
public static readonly CHECK_EMAIL_DESCRIPTION =
|
||||
'This endpoint checks if an email already exists and have a project in the system.';
|
||||
};
|
||||
};
|
||||
static PERMISSION = class {
|
||||
|
@ -6,6 +6,8 @@ import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||
|
||||
@ApiTags('Invite User Module')
|
||||
@Controller({
|
||||
@ -34,4 +36,19 @@ export class InviteUserController {
|
||||
user.role.type,
|
||||
);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('check-email')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.INVITE_USER.ACTIONS.CHECK_EMAIL_SUMMARY,
|
||||
description: ControllerRoute.INVITE_USER.ACTIONS.CHECK_EMAIL_DESCRIPTION,
|
||||
})
|
||||
async checkEmailAndProject(
|
||||
@Body() addUserInvitationDto: CheckEmailDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.inviteUserService.checkEmailAndProject(
|
||||
addUserInvitationDto,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,14 @@ export class AddUserInvitationDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roleUuid: string;
|
||||
@ApiProperty({
|
||||
description: 'The project uuid of the user',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public projectUuid: string;
|
||||
@ApiProperty({
|
||||
description: 'The array of space UUIDs (at least one required)',
|
||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
||||
|
16
src/invite-user/dtos/check-email.dto.ts
Normal file
16
src/invite-user/dtos/check-email.dto.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEmail, IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class CheckEmailDto {
|
||||
@ApiProperty({
|
||||
description: 'The email of the user',
|
||||
example: 'OqM9A@example.com',
|
||||
required: true,
|
||||
})
|
||||
@IsEmail()
|
||||
@IsNotEmpty()
|
||||
email: string;
|
||||
constructor(dto: Partial<CheckEmailDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
@ -12,11 +12,14 @@ import {
|
||||
InviteUserRepository,
|
||||
InviteUserSpaceRepository,
|
||||
} from '@app/common/modules/Invite-user/repositiories';
|
||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class InviteUserService {
|
||||
constructor(
|
||||
private readonly inviteUserRepository: InviteUserRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
@ -33,6 +36,7 @@ export class InviteUserService {
|
||||
phoneNumber,
|
||||
roleUuid,
|
||||
spaceUuids,
|
||||
projectUuid,
|
||||
} = dto;
|
||||
|
||||
const invitationCode = generateRandomString(6);
|
||||
@ -67,6 +71,7 @@ export class InviteUserService {
|
||||
status: UserStatusEnum.INVITED,
|
||||
invitationCode,
|
||||
invitedBy: roleType,
|
||||
project: { uuid: projectUuid },
|
||||
});
|
||||
|
||||
const invitedUser = await queryRunner.manager.save(inviteUser);
|
||||
@ -105,4 +110,46 @@ export class InviteUserService {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
async checkEmailAndProject(dto: CheckEmailDto): Promise<BaseResponseDto> {
|
||||
const { email } = dto;
|
||||
|
||||
try {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { email },
|
||||
relations: ['project'],
|
||||
});
|
||||
|
||||
if (user?.project) {
|
||||
throw new HttpException(
|
||||
'This email already has a project',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
const invitedUser = await this.inviteUserRepository.findOne({
|
||||
where: { email },
|
||||
relations: ['project'],
|
||||
});
|
||||
|
||||
if (invitedUser?.project) {
|
||||
throw new HttpException(
|
||||
'This email already has a project',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
success: true,
|
||||
message: 'Valid email',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error checking email and project:', error);
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while checking the email',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user