Skip to content

Commit

Permalink
Merge pull request #368 from credebl/error-handling-of-user-module
Browse files Browse the repository at this point in the history
refactor: Fetch user profile API
  • Loading branch information
vivekayanworks authored Dec 26, 2023
2 parents bfd5ceb + 4b8a8a5 commit d560ce4
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 54 deletions.
15 changes: 9 additions & 6 deletions apps/api-gateway/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { UserService } from './user.service';
import {
ApiBearerAuth,
ApiExcludeEndpoint,
ApiForbiddenResponse,
ApiOperation,
ApiParam,
Expand Down Expand Up @@ -69,7 +70,8 @@ export class UserController {
* @returns Users list of organization
*/
@Get('/public-profiles')
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiExcludeEndpoint()
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
@ApiOperation({ summary: 'Get users list', description: 'Get users list.' })
@ApiQuery({
name: 'pageNumber',
Expand Down Expand Up @@ -102,6 +104,7 @@ export class UserController {
}

@Get('public-profiles/:username')
@ApiExcludeEndpoint()
@ApiOperation({
summary: 'Fetch user details',
description: 'Fetch user details'
Expand Down Expand Up @@ -130,13 +133,13 @@ export class UserController {
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
async getProfile(@User() reqUser: user, @Res() res: Response): Promise<object> {
async getProfile(@User() reqUser: user, @Res() res: Response): Promise<Response> {
const userData = await this.userService.getProfile(reqUser.id);

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

return res.status(HttpStatus.OK).json(finalResponse);
Expand Down Expand Up @@ -164,8 +167,8 @@ export class UserController {

@Get('/activity')
@ApiOperation({
summary: 'organization invitations',
description: 'Fetch organization invitations'
summary: 'users activity',
description: 'Fetch users activity'
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
Expand All @@ -180,7 +183,7 @@ export class UserController {
const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.userActivity,
data: userDetails.response
data: userDetails
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand Down
16 changes: 9 additions & 7 deletions apps/api-gateway/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ 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 { IUsersActivity } from 'libs/user-activity/interface';

@Injectable()
export class UserService extends BaseService {
constructor(@Inject('NATS_CLIENT') private readonly serviceProxy: ClientProxy) {
super('User Service');
}

async getProfile(id: string): Promise<{ response: object }> {
async getProfile(id: string): Promise<IUsersProfile> {
const payload = { id };
return this.sendNats(this.serviceProxy, 'get-user-profile', payload);
return this.sendNatsMessage(this.serviceProxy, 'get-user-profile', payload);
}

async getPublicProfile(username: string): Promise<{ response: object }> {
const payload = { username };
return this.sendNats(this.serviceProxy, 'get-user-public-profile', payload);
const payload = { username };
return this.sendNatsMessage(this.serviceProxy, 'get-user-public-profile', payload);
}


Expand Down Expand Up @@ -69,17 +71,17 @@ export class UserService extends BaseService {
): Promise<{ response: object }> {
const { pageNumber, pageSize, search } = getAllUsersDto;
const payload = { pageNumber, pageSize, search };
return this.sendNats(this.serviceProxy, 'fetch-users', payload);
return this.sendNatsMessage(this.serviceProxy, 'fetch-users', payload);
}

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

async getUserActivities(userId: string, limit: number): Promise<{ response: object }> {
async getUserActivities(userId: string, limit: number): Promise<IUsersActivity[]> {
const payload = { userId, limit };
return this.sendNats(this.serviceProxy, 'get-user-activity', payload);
return this.sendNatsMessage(this.serviceProxy, 'get-user-activity', payload);
}

async addPasskey(userEmail: string, userInfo: AddPasskeyDetails): Promise<{ response: string }> {
Expand Down
44 changes: 34 additions & 10 deletions apps/user/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,39 @@ export interface Organisation {
logoUrl: string;
}

export interface UsersProfile {
id?: string;
username?: string;
email?: string;
firstName?: string;
lastName?: string;
supabaseUserId?: string;
userOrgRoles?: object;
}
export interface IUsersProfile {
id: string;
username?: string;
email?: string;
firstName?: string;
lastName?: string;
supabaseUserId?: string;
userOrgRoles?: IUserOrgRole[];
}

interface IUserOrgRole {
id: string;
userId: string;
orgRoleId: string;
orgId: string;
orgRole :IOrgRole;
organisation:IOrganisation;
}
export interface IOrgRole{
id: string;
name: string;
description: string;
};
export interface IOrganisation{
id: string;
name: string;
description: string;
orgSlug: string;
logoUrl: string;
website: string;
publicProfile: boolean;
};


export interface OrgInvitations {
id: string;
Expand All @@ -48,7 +72,7 @@ export interface UsersProfile {
username?: string;
}

export interface userInfo {
export interface IUserInformation {
email: string;
password: string;
firstName: string;
Expand Down
34 changes: 21 additions & 13 deletions apps/user/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
UpdateUserProfile,
UserCredentials,
UserEmailVerificationDto,
UsersProfile,
userInfo
IUsersProfile,
IUserInformation
} from '../interfaces/user.interface';

import { InternalServerErrorException } from '@nestjs/common';
Expand Down Expand Up @@ -97,7 +97,7 @@ export class UserRepository {
* @param id
* @returns User profile data
*/
async getUserById(id: string): Promise<UsersProfile> {
async getUserById(id: string): Promise<IUsersProfile> {
const queryOptions: UserQueryOptions = {
id
};
Expand All @@ -123,7 +123,7 @@ export class UserRepository {
* @param id
* @returns User profile data
*/
async getUserPublicProfile(username: string): Promise<UsersProfile> {
async getUserPublicProfile(username: string): Promise<IUsersProfile> {
const queryOptions: UserQueryOptions = {
username
};
Expand Down Expand Up @@ -203,7 +203,7 @@ export class UserRepository {
return this.findUser(queryOptions);
}

async findUser(queryOptions: UserQueryOptions): Promise<UsersProfile> {
async findUser(queryOptions: UserQueryOptions): Promise<IUsersProfile> {
return this.prisma.user.findFirst({
where: {
OR: [
Expand Down Expand Up @@ -254,7 +254,7 @@ export class UserRepository {
});
}

async findUserForPublicProfile(queryOptions: UserQueryOptions): Promise<UsersProfile> {
async findUserForPublicProfile(queryOptions: UserQueryOptions): Promise<IUsersProfile> {
return this.prisma.user.findFirst({
where: {
publicProfile: true,
Expand All @@ -279,22 +279,30 @@ export class UserRepository {
isEmailVerified: true,
publicProfile: true,
userOrgRoles: {
include: {
orgRole: true,
select:{
id: true,
userId:true,
orgRoleId:true,
orgId:true,
orgRole: {
select:{
id: true,
name: true,
description: true
}
},
organisation: {
select: {
id: true,
name: true,
description: true,
orgSlug:true,
logoUrl: true,
website: true,
orgSlug: true
},
where: {
publicProfile: true
}
}
}
}
}
}
});
Expand Down Expand Up @@ -330,7 +338,7 @@ export class UserRepository {
* @returns Updates user details
*/
// eslint-disable-next-line camelcase
async updateUserInfo(email: string, userInfo: userInfo): Promise<user> {
async updateUserInfo(email: string, userInfo: IUserInformation): Promise<user> {
try {
const updateUserDetails = await this.prisma.user.update({
where: {
Expand Down
12 changes: 6 additions & 6 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, userInfo, UsersProfile } from '../interfaces/user.interface';
import { AddPasskeyDetails, CheckUserDetails, PlatformSettings, ShareUserCertificate, UserInvitations, UpdateUserProfile, UserCredentials, UserEmailVerificationDto, IUserInformation, IUsersProfile } from '../interfaces/user.interface';
import {IOrgUsers, Payload} from '../interfaces/user.interface';

import { AcceptRejectInvitationDto } from '../dtos/accept-reject-invitation.dto';
Expand All @@ -8,7 +8,7 @@ import { MessagePattern } from '@nestjs/microservices';
import { UserService } from './user.service';
import { VerifyEmailTokenDto } from '../dtos/verify-email.dto';
import { user } from '@prisma/client';
import { UsersActivity } from 'libs/user-activity/interface';
import { IUsersActivity } from 'libs/user-activity/interface';

@Controller()
export class UserController {
Expand Down Expand Up @@ -40,12 +40,12 @@ export class UserController {
}

@MessagePattern({ cmd: 'get-user-profile' })
async getProfile(payload: { id }): Promise<UsersProfile> {
async getProfile(payload: { id }): Promise<IUsersProfile> {
return this.userService.getProfile(payload);
}

@MessagePattern({ cmd: 'get-user-public-profile' })
async getPublicProfile(payload: { username }): Promise<UsersProfile> {
async getPublicProfile(payload: { username }): Promise<IUsersProfile> {
return this.userService.getPublicProfile(payload);
}
@MessagePattern({ cmd: 'update-user-profile' })
Expand Down Expand Up @@ -126,13 +126,13 @@ export class UserController {
return this.userService.checkUserExist(payload.userEmail);
}
@MessagePattern({ cmd: 'add-user' })
async addUserDetailsInKeyCloak(payload: { userInfo: userInfo }): Promise<string | object> {
async addUserDetailsInKeyCloak(payload: { userInfo: IUserInformation }): Promise<string | object> {
return this.userService.createUserForToken(payload.userInfo);
}

// Fetch Users recent activities
@MessagePattern({ cmd: 'get-user-activity' })
async getUserActivity(payload: { userId: string, limit: number }): Promise<UsersActivity[]> {
async getUserActivity(payload: { userId: string, limit: number }): Promise<IUsersActivity[]> {
return this.userService.getUserActivity(payload.userId, payload.limit);
}

Expand Down
14 changes: 7 additions & 7 deletions apps/user/src/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import {
UpdateUserProfile,
UserCredentials,
UserEmailVerificationDto,
userInfo,
UsersProfile
IUserInformation,
IUsersProfile
} from '../interfaces/user.interface';
import { AcceptRejectInvitationDto } from '../dtos/accept-reject-invitation.dto';
import { UserActivityService } from '@credebl/user-activity';
Expand All @@ -55,7 +55,7 @@ import { DISALLOWED_EMAIL_DOMAIN } from '@credebl/common/common.constant';
import { AwsService } from '@credebl/aws';
import puppeteer from 'puppeteer';
import { WorldRecordTemplate } from '../templates/world-record-template';
import { UsersActivity } from 'libs/user-activity/interface';
import { IUsersActivity } from 'libs/user-activity/interface';

@Injectable()
export class UserService {
Expand Down Expand Up @@ -208,7 +208,7 @@ export class UserService {
}
}

async createUserForToken(userInfo: userInfo): Promise<string> {
async createUserForToken(userInfo: IUserInformation): Promise<string> {
try {
const { email } = userInfo;
if (!userInfo.email) {
Expand Down Expand Up @@ -368,7 +368,7 @@ export class UserService {
}
}

async getProfile(payload: { id }): Promise<UsersProfile> {
async getProfile(payload: { id }): Promise<IUsersProfile> {
try {
const userData = await this.userRepository.getUserById(payload.id);
const ecosystemSettingsList = await this.prisma.ecosystem_config.findMany({
Expand All @@ -388,7 +388,7 @@ export class UserService {
}
}

async getPublicProfile(payload: { username }): Promise<UsersProfile> {
async getPublicProfile(payload: { username }): Promise<IUsersProfile> {
try {
const userProfile = await this.userRepository.getUserPublicProfile(payload.username);

Expand Down Expand Up @@ -755,7 +755,7 @@ export class UserService {
}


async getUserActivity(userId: string, limit: number): Promise<UsersActivity[]> {
async getUserActivity(userId: string, limit: number): Promise<IUsersActivity[]> {
try {
return this.userActivityService.getUserActivity(userId, limit);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion libs/user-activity/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface UsersActivity {
export interface IUsersActivity {
id: string,
orgId: string,
userId: string,
Expand Down
4 changes: 2 additions & 2 deletions libs/user-activity/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable camelcase */
import { Injectable, Logger } from '@nestjs/common';
import { UsersActivity} from '../interface';
import { IUsersActivity} from '../interface';
import { PrismaService } from '@credebl/prisma-service';
import { user_activity } from '@prisma/client';

Expand All @@ -23,7 +23,7 @@ export class UserActivityRepository {
}


async getRecentActivities(userId: string, limit: number): Promise<UsersActivity[]> {
async getRecentActivities(userId: string, limit: number): Promise<IUsersActivity[]> {
return this.prisma.user_activity.findMany({
where: {
userId
Expand Down
Loading

0 comments on commit d560ce4

Please sign in to comment.