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 module APIs. #386

Merged
merged 8 commits into from
Dec 28, 2023
15 changes: 7 additions & 8 deletions apps/api-gateway/src/user/dto/add-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ export class AddUserDetailsDto {

@ApiProperty({ example: 'Alen' })
@IsNotEmpty({ message: 'First name is required' })
@MinLength(2, { message: 'First name must be at least 2 characters.' })
@MaxLength(50, { message: 'First name must be at most 50 characters.' })
@MinLength(2, { message: 'First name must be at least 2 characters' })
@MaxLength(50, { message: 'First name must be at most 50 characters' })
@IsString({ message: 'First name should be a string' })
firstName: string;

@ApiProperty({ example: 'Harvey' })
@IsNotEmpty({ message: 'Last name is required' })
@MinLength(2, { message: 'Last name must be at least 2 characters.' })
@MaxLength(50, { message: 'Last name must be at most 50 characters.' })
@MinLength(2, { message: 'Last name must be at least 2 characters' })
@MaxLength(50, { message: 'Last name must be at most 50 characters' })
@IsString({ message: 'Last name should be a string' })
lastName: string;

@ApiProperty()
@Transform(({ value }) => trim(value))
@IsNotEmpty({ message: 'Password is required.' })
@IsOptional()
password?: string;
@IsNotEmpty({ message: 'Password is required' })
password: string;

@ApiProperty({ example: 'false' })
@IsOptional()
Expand All @@ -40,7 +39,7 @@ export class AddUserDetailsDto {
export class AddPasskeyDetails {
@ApiProperty()
@Transform(({ value }) => trim(value))
@IsNotEmpty({ message: 'Password is required.' })
@IsNotEmpty({ message: 'Password is required' })
password: string;

}
63 changes: 40 additions & 23 deletions apps/api-gateway/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { UnauthorizedErrorDto } from '../dtos/unauthorized-error.dto';
import { ForbiddenErrorDto } from '../dtos/forbidden-error.dto';
import { Response } from 'express';
import { CommonService } from '@credebl/common';
import IResponseType from '@credebl/common/interfaces/response.interface';
import IResponse from '@credebl/common/interfaces/response.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { user } from '@prisma/client';
import { AuthGuard } from '@nestjs/passport';
Expand Down Expand Up @@ -94,7 +94,7 @@ export class UserController {
@Res() res: Response
): Promise<Response> {
const users = await this.userService.get(getAllUsersDto);
const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchUsers,
data: users.response
Expand All @@ -117,7 +117,7 @@ export class UserController {
async getPublicProfile(@Param('username') username: string, @Res() res: Response): Promise<object> {
const userData = await this.userService.getPublicProfile(username);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchProfile,
data: userData.response
Expand All @@ -136,7 +136,7 @@ export class UserController {
async getProfile(@User() reqUser: user, @Res() res: Response): Promise<Response> {
const userData = await this.userService.getProfile(reqUser.id);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchProfile,
data: userData
Expand All @@ -145,6 +145,9 @@ export class UserController {
return res.status(HttpStatus.OK).json(finalResponse);
}

/**
* @returns platform and ecosystem settings
*/
@Get('/platform-settings')
@ApiOperation({
summary: 'Get all platform and ecosystem settings',
Expand All @@ -159,7 +162,7 @@ export class UserController {
const finalResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchPlatformSettings,
data: settings.response
data: settings
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand All @@ -180,14 +183,17 @@ export class UserController {
): Promise<Response> {
const userDetails = await this.userService.getUserActivities(reqUser.id, limit);

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

return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* @returns Organization invitation data
*/

@Get('/org-invitations')
@ApiOperation({
Expand Down Expand Up @@ -220,7 +226,7 @@ export class UserController {
@Query() getAllInvitationsDto: GetAllInvitationsDto,
@User() reqUser: user,
@Res() res: Response
): Promise<object> {
): Promise<Response> {
if (!Object.values(Invitation).includes(getAllInvitationsDto.status)) {
throw new BadRequestException(ResponseMessages.user.error.invalidInvitationStatus);
}
Expand All @@ -231,10 +237,10 @@ export class UserController {
getAllInvitationsDto
);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchInvitations,
data: invitations.response
data: invitations
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand All @@ -250,25 +256,28 @@ export class UserController {
async checkUserExist(@Param() emailParam: EmailValidator, @Res() res: Response): Promise<Response> {
const userDetails = await this.userService.checkUserExist(emailParam.email);

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

return res.status(HttpStatus.OK).json(finalResponse);
}

/**
* @param credentialId
* @returns User credentials
*/
@Get('/user-credentials/:credentialId')
@ApiOperation({ summary: 'Get user credentials by Id', description: 'Get user credentials by Id' })
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
async getUserCredentialsById(@Param('credentialId') credentialId: string, @Res() res: Response): Promise<Response> {
const getUserCrdentialsById = await this.userService.getUserCredentialsById(credentialId);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.userCredentials,
data: getUserCrdentialsById.response
data: getUserCrdentialsById
};
return res.status(HttpStatus.OK).json(finalResponse);
}
Expand Down Expand Up @@ -296,33 +305,36 @@ export class UserController {
acceptRejectInvitation.invitationId = invitationId;
const invitationRes = await this.userService.acceptRejectInvitaion(acceptRejectInvitation, reqUser.id);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.CREATED,
message: invitationRes.response
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}

/**
* @Body shareUserCredentials
* @returns User certificate URL
*/
@Post('/certificate')
@ApiOperation({
summary: 'Share user certificate',
description: 'Share user certificate'
})
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
async shareUserCertificate(
@Body() shareUserCredentials: CreateUserCertificateDto,
@Res() res: Response
): Promise<object> {
): Promise<Response> {
const schemaIdParts = shareUserCredentials.schemaId.split(':');
// eslint-disable-next-line prefer-destructuring
const title = schemaIdParts[2];

const imageBuffer = await this.userService.shareUserCertificate(shareUserCredentials);
const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.CREATED,
message: 'Certificate url generated successfully',
message: ResponseMessages.user.success.shareUserCertificate,
label: title,
data: imageBuffer.response
data: imageBuffer
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}
Expand All @@ -344,7 +356,7 @@ export class UserController {
updateUserProfileDto.id = userId;
await this.userService.updateUserProfile(updateUserProfileDto);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.update
};
Expand All @@ -370,6 +382,11 @@ export class UserController {
return res.status(HttpStatus.OK).json(finalResponse);
}

/**
* @Body platformSettings
* @returns platform and ecosystem settings updated status
*/

@Put('/platform-settings')
@ApiOperation({
summary: 'Update platform and ecosystem settings',
Expand All @@ -386,7 +403,7 @@ export class UserController {

const finalResponse = {
statusCode: HttpStatus.OK,
message: result.response
message: result
};

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

@Injectable()
export class UserService extends BaseService {
Expand Down Expand Up @@ -45,10 +46,10 @@ export class UserService extends BaseService {
}


async invitations(id: string, status: string, getAllInvitationsDto: GetAllInvitationsDto): Promise<{ response: object }> {
async invitations(id: string, status: string, getAllInvitationsDto: GetAllInvitationsDto): Promise<IUserInvitations> {
const { pageNumber, pageSize, search } = getAllInvitationsDto;
const payload = { id, status, pageNumber, pageSize, search };
return this.sendNats(this.serviceProxy, 'get-org-invitations', payload);
return this.sendNatsMessage(this.serviceProxy, 'get-org-invitations', payload);
}

async acceptRejectInvitaion(
Expand All @@ -63,7 +64,7 @@ export class UserService extends BaseService {
shareUserCredentials: CreateUserCertificateDto
): Promise<{ response: Buffer }> {
const payload = { shareUserCredentials};
return this.sendNats(this.serviceProxy, 'share-user-certificate', payload);
return this.sendNatsMessage(this.serviceProxy, 'share-user-certificate', payload);
}

async get(
Expand Down Expand Up @@ -91,10 +92,10 @@ export class UserService extends BaseService {

async updatePlatformSettings(platformSettings: UpdatePlatformSettingsDto): Promise<{ response: string }> {
const payload = { platformSettings };
return this.sendNats(this.serviceProxy, 'update-platform-settings', payload);
return this.sendNatsMessage(this.serviceProxy, 'update-platform-settings', payload);
}

async getPlatformSettings(): Promise<{ response: object }> {
return this.sendNats(this.serviceProxy, 'fetch-platform-settings', '');
return this.sendNatsMessage(this.serviceProxy, 'fetch-platform-settings', '');
}
}
31 changes: 6 additions & 25 deletions apps/user/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
export interface UserInvitations {
totalPages:number;
userInvitationsData:UserInvitationsData[];
}
export interface UserInvitationsData {
orgRoles: OrgRole[];
status: string;
id: string;
orgId: string;
organisation: Organisation;
userId: string;
}
export interface OrgRole {
id: string;
name: string;
description: string;
}

export interface Organisation {
id: string;
name: string;
logoUrl: string;
}

export interface IUsersProfile {
id: string;
username?: string;
Expand Down Expand Up @@ -119,7 +95,7 @@ export interface ISendVerificationEmail {
isSupabase?: boolean;
}

export interface UserCredentials {
export interface IUserCredentials {
id: string;
imageUrl?: string;
credentialId?: string;
Expand Down Expand Up @@ -150,6 +126,11 @@ export interface ISendVerificationEmail {
orgRole: OrgRole;
organisation: Organization
}
interface OrgRole {
id: string;
name: string;
description: string;
}

interface Organization {
id: string,
Expand Down
4 changes: 2 additions & 2 deletions apps/user/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PlatformSettings,
ShareUserCertificate,
UpdateUserProfile,
UserCredentials,
IUserCredentials,
ISendVerificationEmail,
IUsersProfile,
IUserInformation,
Expand Down Expand Up @@ -110,7 +110,7 @@ export class UserRepository {
* @param id
* @returns User profile data
*/
async getUserCredentialsById(credentialId: string): Promise<UserCredentials> {
async getUserCredentialsById(credentialId: string): Promise<IUserCredentials> {
return this.prisma.user_credentials.findUnique({
where: {
credentialId
Expand Down
Loading