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

[EN-7483] Referer - Dashboard - Allow to filter profiles by refererId #236

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions src/user-profiles/user-profiles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ export class UserProfilesController {
@Query('departments')
departments: Department[],
@Query('businessLines')
businessLines: BusinessLineValue[],
@Query('refererId')
refererId: string
businessLines: BusinessLineValue[]
) {
if (!role || role.length === 0) {
throw new BadRequestException();
Expand All @@ -145,7 +143,21 @@ export class UserProfilesController {
helps,
departments,
businessLines,
refererId,
});
}

@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,
});
}

Expand Down
69 changes: 51 additions & 18 deletions src/user-profiles/user-profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,10 @@ export class UserProfilesService {
helps: HelpValue[];
departments: Department[];
businessLines: BusinessLineValue[];
refererId: string;
}
): Promise<PublicProfile[]> {
const {
role,
offset,
limit,
search,
helps,
departments,
businessLines,
refererId,
} = query;
const { role, offset, limit, search, helps, departments, businessLines } =
query;

const searchOptions = search
? { [Op.or]: userProfileSearchQuery(search) }
Expand All @@ -146,12 +137,6 @@ export class UserProfilesService {
}
: {};

const refererIdOptions: WhereOptions<User> = refererId
? {
refererId,
}
: {};

const helpsOptions: WhereOptions<HelpNeed | HelpOffer> =
helps?.length > 0
? {
Expand Down Expand Up @@ -182,7 +167,6 @@ export class UserProfilesService {
role,
lastConnection: { [Op.ne]: null },
...searchOptions,
...refererIdOptions,
},
},
],
Expand Down Expand Up @@ -226,6 +210,55 @@ export class UserProfilesService {
);
}

async findAllReferedCandidates(
userId: string,
query: {
offset: number;
limit: number;
}
): Promise<PublicProfile[]> {
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<PublicProfile> => {
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,
};
Comment on lines +241 to +257
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas sur que ce soit opti si y a beaucoup de referredUsers mais on verra en temps voulu.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, j'ai simplement utilisé la même logique de construction que dans le réseau d'entraide mais c'est clairement perfectible.

})
);
}

async findRecommendationsByUserId(
userId: string
): Promise<UserProfileRecommendation[]> {
Expand Down