Skip to content

Commit

Permalink
Check if users are connected when doing an user search
Browse files Browse the repository at this point in the history
  • Loading branch information
adelinaenache committed May 3, 2024
1 parent 2f3f175 commit fd13e99
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/schemas/user.properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const userProperties = {
export const userExtraProps = {
connectionsCount: { type: 'number' },
ratingsCount: { type: 'number' },
isConnection: { type: 'boolean' },
};
11 changes: 8 additions & 3 deletions src/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Put,
Query,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { CurrentUser } from '../../decorators/current-user.decorator';
Expand Down Expand Up @@ -37,6 +38,7 @@ import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Multer } from 'multer';
import { Public } from '../../decorators/public.decorator';
import { AuthGuard } from '../../auth/guard/auth/auth.guard';

@Controller('user')
@ApiBearerAuth()
Expand Down Expand Up @@ -265,7 +267,6 @@ export class UserController {
return this.userService.getAvgUserRatings(user, false, userId);
}

@Public()
@Get('search')
@ApiOperation({
summary: 'Search users',
Expand All @@ -285,7 +286,11 @@ export class UserController {
name: 'query',
type: 'string',
})
async searchUsers(@Query() { query }: { query: string }) {
return this.userService.searchUsers(query);
@UseGuards(AuthGuard)
async searchUsers(
@CurrentUser() user: User,
@Query() { query }: { query: string },
) {
return this.userService.searchUsers(user.id, query);
}
}
14 changes: 10 additions & 4 deletions src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class UserService {
}));
}

async searchUsers(searchTerm?: string) {
async searchUsers(userId: User['id'], searchTerm?: string) {
if (!searchTerm) {
throw new BadRequestException('Search term is required');
}
Expand All @@ -149,17 +149,23 @@ export class UserService {
isEmailVerified: true,
jobTitle: true,
profilePictureUrl: true,
connections: {
where: {
followerId: userId,
},
},
_count: {
select: {
connections: true,
ratings: true,
ratingsReceived: true,
},
},
},
});
return users.map(({ _count, ...user }) => ({
return users.map(({ _count, connections, ...user }) => ({
connectionsCount: _count.connections,
ratingsCount: _count.ratings,
ratingsCount: _count.ratingsReceived,
isConnection: connections.length == 0,
...user,
}));
}
Expand Down

0 comments on commit fd13e99

Please sign in to comment.