diff --git a/src/user-profiles/user-profiles.controller.ts b/src/user-profiles/user-profiles.controller.ts index 5cb9b611..1e8af51e 100644 --- a/src/user-profiles/user-profiles.controller.ts +++ b/src/user-profiles/user-profiles.controller.ts @@ -146,6 +146,21 @@ export class UserProfilesController { }); } + @UserPermissions(Permissions.REFERER) + @Get('refered') + async findReferedCandidates( + @UserPayload('id', new ParseUUIDPipe()) userId: string, + @Query('limit', new ParseIntPipe()) + limit: number, + @Query('offset', new ParseIntPipe()) + offset: number + ) { + return this.userProfilesService.findAllReferedCandidates(userId, { + offset, + limit, + }); + } + @Self('params.userId') @UseGuards(SelfGuard) @UseInterceptors(FileInterceptor('profileImage', { dest: 'uploads/' })) diff --git a/src/user-profiles/user-profiles.service.ts b/src/user-profiles/user-profiles.service.ts index 5981ceb4..e4d21311 100644 --- a/src/user-profiles/user-profiles.service.ts +++ b/src/user-profiles/user-profiles.service.ts @@ -210,6 +210,55 @@ export class UserProfilesService { ); } + async findAllReferedCandidates( + userId: string, + query: { + offset: number; + limit: number; + } + ): Promise { + const { offset, limit } = query; + + const profiles = await this.userProfileModel.findAll({ + attributes: UserProfilesAttributes, + order: sequelize.literal('"user.createdAt" DESC'), + include: [ + ...getUserProfileInclude(), + { + model: User, + as: 'user', + attributes: UserProfilesUserAttributes, + where: { + refererId: userId, + }, + }, + ], + limit, + offset, + }); + + return Promise.all( + profiles.map(async (profile): Promise => { + const lastSentMessage = await this.getLastContact( + userId, + profile.user.id + ); + const lastReceivedMessage = await this.getLastContact( + profile.user.id, + userId + ); + + const { user, ...restProfile }: UserProfile = profile.toJSON(); + return { + ...user, + ...restProfile, + lastSentMessage: lastSentMessage?.createdAt || null, + lastReceivedMessage: lastReceivedMessage?.createdAt || null, + }; + }) + ); + } + async findRecommendationsByUserId( userId: string ): Promise {