mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 15:24:54 +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 =
|
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 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 {
|
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 { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
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 { CheckEmailDto } from '../dtos/check-email.dto';
|
||||||
|
|
||||||
@ApiTags('Invite User Module')
|
@ApiTags('Invite User Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -34,4 +36,19 @@ export class InviteUserController {
|
|||||||
user.role.type,
|
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()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public roleUuid: string;
|
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({
|
@ApiProperty({
|
||||||
description: 'The array of space UUIDs (at least one required)',
|
description: 'The array of space UUIDs (at least one required)',
|
||||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
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,
|
InviteUserRepository,
|
||||||
InviteUserSpaceRepository,
|
InviteUserSpaceRepository,
|
||||||
} from '@app/common/modules/Invite-user/repositiories';
|
} from '@app/common/modules/Invite-user/repositiories';
|
||||||
|
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class InviteUserService {
|
export class InviteUserService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly inviteUserRepository: InviteUserRepository,
|
private readonly inviteUserRepository: InviteUserRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
@ -33,6 +36,7 @@ export class InviteUserService {
|
|||||||
phoneNumber,
|
phoneNumber,
|
||||||
roleUuid,
|
roleUuid,
|
||||||
spaceUuids,
|
spaceUuids,
|
||||||
|
projectUuid,
|
||||||
} = dto;
|
} = dto;
|
||||||
|
|
||||||
const invitationCode = generateRandomString(6);
|
const invitationCode = generateRandomString(6);
|
||||||
@ -67,6 +71,7 @@ export class InviteUserService {
|
|||||||
status: UserStatusEnum.INVITED,
|
status: UserStatusEnum.INVITED,
|
||||||
invitationCode,
|
invitationCode,
|
||||||
invitedBy: roleType,
|
invitedBy: roleType,
|
||||||
|
project: { uuid: projectUuid },
|
||||||
});
|
});
|
||||||
|
|
||||||
const invitedUser = await queryRunner.manager.save(inviteUser);
|
const invitedUser = await queryRunner.manager.save(inviteUser);
|
||||||
@ -105,4 +110,46 @@ export class InviteUserService {
|
|||||||
await queryRunner.release();
|
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