Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor user and auth module APIs #377

Merged
merged 7 commits into from
Dec 27, 2023
19 changes: 8 additions & 11 deletions apps/api-gateway/src/authz/authz.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ export class AuthzController {
private readonly commonService: CommonService) { }

/**
*
* @param query
* @param res
* @returns User email verified
* @param email
* @param verificationcode
* @returns User's email verification status
*/
@Get('/verify')
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
@ApiOperation({ summary: 'Verify user’s email', description: 'Verify user’s email' })
async verifyEmail(@Query() query: EmailVerificationDto, @Res() res: Response): Promise<Response> {
await this.authzService.verifyEmail(query);
Expand All @@ -55,16 +54,14 @@ export class AuthzController {
}

/**
*
* @param email
* @param res
* @returns Email sent success
* @returns User's verification email sent status
*/
@Post('/verification-mail')
@ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto })
@ApiResponse({ status: HttpStatus.CREATED, description: 'Created', type: ApiResponseDto })
@ApiOperation({ summary: 'Send verification email', description: 'Send verification email to new user' })
async create(@Body() userEmailVerificationDto: UserEmailVerificationDto, @Res() res: Response): Promise<Response> {
await this.authzService.sendVerificationMail(userEmailVerificationDto);
async create(@Body() userEmailVerification: UserEmailVerificationDto, @Res() res: Response): Promise<Response> {
await this.authzService.sendVerificationMail(userEmailVerification);
const finalResponse: IResponseType = {
statusCode: HttpStatus.CREATED,
message: ResponseMessages.user.success.sendVerificationCode
Expand Down
11 changes: 5 additions & 6 deletions apps/api-gateway/src/authz/authz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ export class AuthzService extends BaseService {
return this.sendNats(this.authServiceProxy, 'get-user-by-keycloakUserId', keycloakUserId);
}

async sendVerificationMail(userEmailVerificationDto: UserEmailVerificationDto): Promise<object> {
const payload = { userEmailVerificationDto };
return this.sendNats(this.authServiceProxy, 'send-verification-mail', payload);
async sendVerificationMail(userEmailVerification: UserEmailVerificationDto): Promise<UserEmailVerificationDto> {
const payload = { userEmailVerification };
return this.sendNatsMessage(this.authServiceProxy, 'send-verification-mail', payload);
}

async verifyEmail(param: EmailVerificationDto): Promise<object> {
async verifyEmail(param: EmailVerificationDto): Promise<EmailVerificationDto> {
const payload = { param };
return this.sendNats(this.authServiceProxy, 'user-email-verification', payload);

return this.sendNatsMessage(this.authServiceProxy, 'user-email-verification', payload);
}

async login(email: string, password?: string, isPasskey = false): Promise<{ response: object }> {
Expand Down
3 changes: 2 additions & 1 deletion apps/api-gateway/src/user/dto/email-verify.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsEmail, IsNotEmpty, MaxLength } from 'class-validator';
import { IsEmail, IsNotEmpty, IsString, MaxLength } from 'class-validator';
import { toLowerCase, trim } from '@credebl/common/cast.helper';

import { ApiProperty } from '@nestjs/swagger';
Expand All @@ -17,5 +17,6 @@ export class EmailVerificationDto {
@ApiProperty()
@Transform(({ value }) => trim(value))
@IsNotEmpty({ message: 'Verification code is required.' })
@IsString({ message: 'Verification code should be string' })
verificationCode: string;
}
7 changes: 3 additions & 4 deletions apps/api-gateway/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,17 @@ export class UserController {
/**
*
* @param email
* @param res
* @returns User email check
* @returns User's email exist status
*/
@Get('/:email')
@ApiOperation({ summary: 'Check user exist', description: 'check user existence' })
@ApiOperation({ summary: 'Check if user exist', description: 'check user existence' })
async checkUserExist(@Param() emailParam: EmailValidator, @Res() res: Response): Promise<Response> {
const userDetails = await this.userService.checkUserExist(emailParam.email);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.checkEmail,
data: userDetails.response
data: userDetails
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand Down
6 changes: 3 additions & 3 deletions apps/api-gateway/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { UpdateUserProfileDto } from './dto/update-user-profile.dto';
import { AddPasskeyDetails } from './dto/add-user.dto';
import { UpdatePlatformSettingsDto } from './dto/update-platform-settings.dto';
import { CreateUserCertificateDto } from './dto/share-certificate.dto';
import { IUsersProfile } from 'apps/user/interfaces/user.interface';
import { IUsersProfile, ICheckUserDetails } from 'apps/user/interfaces/user.interface';
import { IUsersActivity } from 'libs/user-activity/interface';

@Injectable()
Expand Down Expand Up @@ -74,9 +74,9 @@ export class UserService extends BaseService {
return this.sendNatsMessage(this.serviceProxy, 'fetch-users', payload);
}

async checkUserExist(userEmail: string): Promise<{ response: string }> {
async checkUserExist(userEmail: string): Promise<ICheckUserDetails> {
const payload = { userEmail };
return this.sendNats(this.serviceProxy, 'check-user-exist', payload);
return this.sendNatsMessage(this.serviceProxy, 'check-user-exist', payload);
}

async getUserActivities(userId: string, limit: number): Promise<IUsersActivity[]> {
Expand Down
14 changes: 10 additions & 4 deletions apps/user/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ interface IUserOrgRole {
email?: string;
orgRoles: string[];
}
export interface UserEmailVerificationDto {

export interface ISendVerificationEmail {
email: string;
username?: string;
}
Expand Down Expand Up @@ -112,11 +112,11 @@ interface IUserOrgRole {
label: string;
}

export interface CheckUserDetails {
export interface ICheckUserDetails {
isExist: boolean;
isEmailVerified?: boolean;
isFidoVerified?: boolean;
isSupabase?: boolean;
isExist?: boolean;
}

export interface UserCredentials {
Expand Down Expand Up @@ -175,3 +175,9 @@ interface IUserOrgRole {
pageSize: number;
search: string;
}

export class IVerifyUserEmail {
email: string;
verificationCode: string;
}

34 changes: 17 additions & 17 deletions apps/user/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
ShareUserCertificate,
UpdateUserProfile,
UserCredentials,
UserEmailVerificationDto,
IUsersProfile,
IUserInformation
ISendVerificationEmail,
IUsersProfile,
IUserInformation,
IVerifyUserEmail
} from '../interfaces/user.interface';

import { InternalServerErrorException } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
// eslint-disable-next-line camelcase
Expand All @@ -27,21 +27,21 @@ interface UserQueryOptions {
@Injectable()
export class UserRepository {
constructor(
private readonly prisma: PrismaService,
private readonly prisma: PrismaService,
private readonly logger: Logger
) {}

/**
*
* @param userEmailVerificationDto
* @returns user email
* @param userEmailVerification
* @returns user's email
*/
async createUser(userEmailVerificationDto: UserEmailVerificationDto, verifyCode: string): Promise<user> {
async createUser(userEmailVerification:ISendVerificationEmail, verifyCode: string): Promise<user> {
try {
const saveResponse = await this.prisma.user.create({
data: {
username: userEmailVerificationDto.username,
email: userEmailVerificationDto.email,
username: userEmailVerification.username,
email: userEmailVerification.email,
verificationCode: verifyCode.toString(),
publicProfile: true
}
Expand Down Expand Up @@ -247,11 +247,11 @@ export class UserRepository {
website: true,
publicProfile: true
}
}
}
}
}
}
});
});
}

async findUserForPublicProfile(queryOptions: UserQueryOptions): Promise<IUsersProfile> {
Expand Down Expand Up @@ -301,8 +301,8 @@ export class UserRepository {
website: true,
publicProfile: true
}
}
}
}
}
}
});
Expand Down Expand Up @@ -375,15 +375,15 @@ export class UserRepository {
},
select: {
id: true,
username: true,
username: true,
email: true,
firstName: true,
lastName: true,
isEmailVerified: true,
userOrgRoles: {
where: {
where: {
...filterOptions
// Additional filtering conditions if needed
// Additional filtering conditions if needed
},
select: {
id: true,
Expand Down Expand Up @@ -526,7 +526,7 @@ username: true,
}
}

async verifyUser(email: string): Promise<user> {
async verifyUser(email: string): Promise<IVerifyUserEmail> {
try {
const updateUserDetails = await this.prisma.user.update({
where: {
Expand Down
28 changes: 16 additions & 12 deletions apps/user/src/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddPasskeyDetails, CheckUserDetails, PlatformSettings, ShareUserCertificate, UserInvitations, UpdateUserProfile, UserCredentials, UserEmailVerificationDto, IUserInformation, IUsersProfile } from '../interfaces/user.interface';
import { AddPasskeyDetails, ICheckUserDetails, PlatformSettings, ShareUserCertificate, UpdateUserProfile, UserCredentials, IUserInformation, IUsersProfile, UserInvitations, ISendVerificationEmail, IVerifyUserEmail} from '../interfaces/user.interface';
import {IOrgUsers, Payload} from '../interfaces/user.interface';

import { AcceptRejectInvitationDto } from '../dtos/accept-reject-invitation.dto';
Expand All @@ -16,21 +16,22 @@ export class UserController {

/**
* Description: Registers new user
* @param payload Registration Details
* @returns Get registered user response
* @param email
* @returns User's verification email sent status
*/
@MessagePattern({ cmd: 'send-verification-mail' })
async sendVerificationMail(payload: { userEmailVerificationDto: UserEmailVerificationDto }): Promise<object> {
return this.userService.sendVerificationMail(payload.userEmailVerificationDto);
async sendVerificationMail(payload: { userEmailVerification: ISendVerificationEmail }): Promise<ISendVerificationEmail> {
return this.userService.sendVerificationMail(payload.userEmailVerification);
}

/**
* Description: Verify user's email
* @param param
* @returns Get user's email verified
* @param email
* @param verificationcode
* @returns User's email verification status
*/
@MessagePattern({ cmd: 'user-email-verification' })
async verifyEmail(payload: { param: VerifyEmailTokenDto }): Promise<object> {
async verifyEmail(payload: { param: VerifyEmailTokenDto }): Promise<IVerifyUserEmail> {
return this.userService.verifyEmail(payload.param);
}

Expand Down Expand Up @@ -72,7 +73,7 @@ export class UserController {

@MessagePattern({ cmd: 'get-org-invitations' })
async invitations(payload: { id; status; pageNumber; pageSize; search; }): Promise<UserInvitations> {
return this.userService.invitations(payload);
return this.userService.invitations(payload);
}

/**
Expand Down Expand Up @@ -111,7 +112,6 @@ export class UserController {
}

/**
*
* @param payload
* @returns organization users list
*/
Expand All @@ -120,9 +120,13 @@ export class UserController {
const users = this.userService.get(payload.pageNumber, payload.pageSize, payload.search);
return users;
}


/**
* @param email
* @returns User's email exist status
* */
@MessagePattern({ cmd: 'check-user-exist' })
async checkUserExist(payload: { userEmail: string }): Promise<string | CheckUserDetails> {
async checkUserExist(payload: { userEmail: string }): Promise<ICheckUserDetails> {
return this.userService.checkUserExist(payload.userEmail);
}
@MessagePattern({ cmd: 'add-user' })
Expand Down
Loading