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

feat: implemented username & orgslug feature for public profiles #85

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiExtraModels, ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsOptional, IsString, MaxLength, MinLength } from 'class-validator';
import { IsBoolean, IsNotEmpty, IsNumber, IsOptional, IsString, MaxLength, MinLength } from 'class-validator';

import { Transform } from 'class-transformer';
import { trim } from '@credebl/common/cast.helper';
Expand Down Expand Up @@ -37,4 +37,9 @@ export class UpdateOrganizationDto {
@IsOptional()
website: string;

@ApiPropertyOptional({ example: true })
@IsBoolean({ message: 'isPublic should be boolean' })
@IsOptional()
isPublic? = false;

}
17 changes: 9 additions & 8 deletions apps/api-gateway/src/organization/organization.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiBearerAuth, ApiForbiddenResponse, ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { ApiBearerAuth, ApiForbiddenResponse, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { CommonService } from '@credebl/common';
import { Controller, Get, Put, Param, UseGuards, UseFilters } from '@nestjs/common';
import { OrganizationService } from './organization.service';
Expand Down Expand Up @@ -79,7 +79,7 @@ export class OrganizationController {
* @param res
* @returns Users list of organization
*/
@Get('/public')
@Get('/public-profiles')
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiOperation({ summary: 'Get public organization list', description: 'Get users list.' })
@ApiQuery({
Expand Down Expand Up @@ -109,18 +109,19 @@ export class OrganizationController {
return res.status(HttpStatus.OK).json(finalResponse);
}

@Get('public-profile')
@Get('public-profiles/:orgSlug')
@ApiOperation({
summary: 'Fetch user details',
description: 'Fetch user details'
})
@ApiQuery({
name: 'id',
type: Number,

@ApiParam({
name: 'orgSlug',
type: String,
required: false
})
async getPublicProfile(@User() reqUser: user, @Query('id') id: number, @Res() res: Response): Promise<object> {
const userData = await this.organizationService.getPublicProfile(id);
async getPublicProfile(@Param('orgSlug') orgSlug: string, @Res() res: Response): Promise<object> {
const userData = await this.organizationService.getPublicProfile(orgSlug);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
Expand Down
4 changes: 2 additions & 2 deletions apps/api-gateway/src/organization/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class OrganizationService extends BaseService {
return this.sendNats(this.serviceProxy, 'get-public-organizations', payload);
}

async getPublicProfile(id: number): Promise<{ response: object }> {
const payload = { id };
async getPublicProfile(orgSlug: string): Promise<{ response: object }> {
nishad-ayanworks marked this conversation as resolved.
Show resolved Hide resolved
const payload = {orgSlug };
try {
return this.sendNats(this.serviceProxy, 'get-organization-public-profile', payload);
} catch (error) {
Expand Down
8 changes: 6 additions & 2 deletions apps/api-gateway/src/user/dto/update-user-profile.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsOptional, IsString} from 'class-validator';

import { IsBoolean, IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';

export class UpdateUserProfileDto {
@ApiProperty()
Expand All @@ -22,4 +21,9 @@ export class UpdateUserProfileDto {
@IsString({ message: 'lastName should be string' })
@IsOptional()
lastName?: string;

@ApiPropertyOptional({ example: true })
@IsBoolean({ message: 'isPublic should be boolean' })
@IsOptional()
isPublic? = false;
}
15 changes: 8 additions & 7 deletions apps/api-gateway/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ApiBody,
ApiForbiddenResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
Expand Down Expand Up @@ -118,7 +119,7 @@ export class UserController {
* @param res
* @returns Users list of organization
*/
@Get('/public')
@Get('/public-profiles')
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiOperation({ summary: 'Get users list', description: 'Get users list.' })
@ApiQuery({
Expand Down Expand Up @@ -222,18 +223,18 @@ export class UserController {

}

@Get('public-profile')
@Get('public-profiles/:username')
@ApiOperation({
summary: 'Fetch user details',
description: 'Fetch user details'
})
@ApiQuery({
name: 'id',
type: Number,
@ApiParam({
name: 'username',
type: String,
required: false
})
async getPublicProfile(@User() reqUser: user, @Query('id') id: number, @Res() res: Response): Promise<object> {
const userData = await this.userService.getPublicProfile(id);
async getPublicProfile(@Param('username') username: string, @Res() res: Response): Promise<object> {
nishad-ayanworks marked this conversation as resolved.
Show resolved Hide resolved
const userData = await this.userService.getPublicProfile(username);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
Expand Down
4 changes: 2 additions & 2 deletions apps/api-gateway/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class UserService extends BaseService {
}
}

async getPublicProfile(id: number): Promise<{ response: object }> {
const payload = { id };
async getPublicProfile(username: string): Promise<{ response: object }> {
nishad-ayanworks marked this conversation as resolved.
Show resolved Hide resolved
const payload = { username };
try {
return this.sendNats(this.serviceProxy, 'get-user-public-profile', payload);
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions apps/organization/dtos/create-organization.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class CreateOrganizationDto {
description?: string;
logo?: string;
website?: string;
orgSlug?:string;
}

export class CreateUserRoleOrgDto {
Expand Down
2 changes: 2 additions & 0 deletions apps/organization/interfaces/organization.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export interface IUpdateOrganization {
orgId: string;
logo?: string;
website?: string;
orgSlug?: string;
isPublic?:boolean
}
16 changes: 12 additions & 4 deletions apps/organization/repositories/organization.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class OrganizationRepository {
name: createOrgDto.name,
logoUrl: createOrgDto.logo,
description: createOrgDto.description,
website: createOrgDto.website
website: createOrgDto.website,
nishad-ayanworks marked this conversation as resolved.
Show resolved Hide resolved
orgSlug: createOrgDto.orgSlug
}
});
} catch (error) {
Expand All @@ -78,10 +79,11 @@ export class OrganizationRepository {
name: updateOrgDto.name,
logoUrl: updateOrgDto.logo,
description: updateOrgDto.description,
website: updateOrgDto.website
website: updateOrgDto.website,
orgSlug: updateOrgDto.orgSlug,
publicProfile: updateOrgDto.isPublic
}
});

} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
Expand Down Expand Up @@ -243,7 +245,7 @@ export class OrganizationRepository {
}
}

async getOrganization(queryObject: object): Promise<object> {
async getOrganization(queryObject: object): Promise<organisation> {
try {
return this.prisma.organisation.findFirst({
where: {
Expand All @@ -257,6 +259,12 @@ export class OrganizationRepository {
org_agent_type: true,
ledgers: true
}
},
userOrgRoles: {
include:{
user: true,
orgRole:true
}
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion apps/organization/src/organization.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class OrganizationController {
}

@MessagePattern({ cmd: 'get-organization-public-profile' })
async getPublicProfile(payload: { id }): Promise<object> {
async getPublicProfile(payload: { orgSlug }): Promise<object> {
return this.organizationService.getPublicProfile(payload);
}

Expand Down
33 changes: 31 additions & 2 deletions apps/organization/src/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class OrganizationService {
throw new ConflictException(ResponseMessages.organisation.error.exists);
}

const orgSlug = this.createOrgSlug(createOrgDto.name);
createOrgDto.orgSlug = orgSlug;

const organizationDetails = await this.organizationRepository.createOrganization(createOrgDto);

const ownerRoleData = await this.orgRoleService.getRole(OrgRoles.OWNER);
Expand All @@ -61,6 +64,21 @@ export class OrganizationService {
}
}


/**
*
* @param orgName
* @returns OrgSlug
*/
createOrgSlug(orgName: string): string {
return orgName
.toLowerCase() // Convert the input to lowercase
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/[^a-z0-9-]/g, '') // Remove non-alphanumeric characters except hyphens
.replace(/--+/g, '-') // Replace multiple consecutive hyphens with a single hyphen
.replace(/[^-+|-+$]/g, ''); // Trim hyphens from the beginning and end of the string
}

/**
*
* @param registerOrgDto
Expand All @@ -70,6 +88,16 @@ export class OrganizationService {
// eslint-disable-next-line camelcase
async updateOrganization(updateOrgDto: IUpdateOrganization, userId: number): Promise<organisation> {
try {

const organizationExist = await this.organizationRepository.checkOrganizationNameExist(updateOrgDto.name);

if (organizationExist) {
throw new ConflictException(ResponseMessages.organisation.error.exists);
}

const orgSlug = await this.createOrgSlug(updateOrgDto.name);
updateOrgDto.orgSlug = orgSlug;

const organizationDetails = await this.organizationRepository.updateOrganization(updateOrgDto);
await this.userActivityService.createActivity(userId, organizationDetails.id, `${organizationDetails.name} organization updated`, 'Organization details updated successfully');
return organizationDetails;
Expand Down Expand Up @@ -147,11 +175,12 @@ export class OrganizationService {
}
}

async getPublicProfile(payload: { id }): Promise<object> {
async getPublicProfile(payload: { orgSlug: string }): Promise<organisation> {
const {orgSlug} = payload;
try {

const query = {
id: payload.id,
orgSlug,
publicProfile: true
};

Expand Down
2 changes: 2 additions & 0 deletions apps/user/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface InvitationsI {

export interface UserEmailVerificationDto{
email:string
username?:string
}

export interface userInfo{
Expand All @@ -51,4 +52,5 @@ export interface UpdateUserProfile {
profileImg?: string;
firstName: string,
lastName: string,
isPublic: boolean,
}
34 changes: 21 additions & 13 deletions apps/user/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { InternalServerErrorException } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
// eslint-disable-next-line camelcase
import { user } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';

interface UserQueryOptions {
id?: number; // Use the appropriate type based on your data model
email?: string; // Use the appropriate type based on your data model
username?: string
// Add more properties if needed for other unique identifier fields
};

Expand All @@ -24,12 +24,11 @@ export class UserRepository {
* @param userEmailVerificationDto
* @returns user email
*/
async createUser(userEmailVerificationDto: UserEmailVerificationDto): Promise<user> {
async createUser(userEmailVerificationDto: UserEmailVerificationDto, verifyCode: string): Promise<user> {
try {
const verifyCode = uuidv4();
const saveResponse = await this.prisma.user.create({
data: {
username: userEmailVerificationDto.email,
username: userEmailVerificationDto.username,
email: userEmailVerificationDto.email,
verificationCode: verifyCode.toString()
}
Expand Down Expand Up @@ -99,19 +98,22 @@ export class UserRepository {
* @param id
* @returns User profile data
*/
async getUserPublicProfile(id: number): Promise<UserI> {
const queryOptions: UserQueryOptions = {
id
};
return this.findUserForPublicProfile(queryOptions);
}
async getUserPublicProfile(username: string): Promise<UserI> {

const queryOptions: UserQueryOptions = {
username
};

return this.findUserForPublicProfile(queryOptions);
}

/**
*
* @Body updateUserProfile
* @returns Update user profile data
*/
async updateUserProfile(updateUserProfile: UpdateUserProfile): Promise<UpdateUserProfile> {
async updateUserProfile(updateUserProfile: UpdateUserProfile): Promise<user> {

try {
const userdetails = await this.prisma.user.update({
where: {
Expand All @@ -120,7 +122,8 @@ export class UserRepository {
data: {
profileImg: updateUserProfile.profileImg,
firstName: updateUserProfile.firstName,
lastName: updateUserProfile.lastName
lastName: updateUserProfile.lastName,
publicProfile: updateUserProfile?.isPublic
}
});
return userdetails;
Expand Down Expand Up @@ -233,6 +236,9 @@ export class UserRepository {
},
{
email: queryOptions.email
},
{
username: queryOptions.username
}
]
},
Expand All @@ -253,7 +259,8 @@ export class UserRepository {
name: true,
description: true,
logoUrl: true,
website: true
website: true,
orgSlug: true
},
where:{
publicProfile: true
Expand Down Expand Up @@ -397,6 +404,7 @@ export class UserRepository {
email: true,
firstName: true,
lastName: true,
profileImg: true,
isEmailVerified: true,
clientId: false,
clientSecret: false,
Expand Down
Loading