Skip to content

Commit

Permalink
fix(members): try to fix member request limit
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Oct 23, 2023
1 parent 9f76bb8 commit f3395a1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
86 changes: 54 additions & 32 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Cache } from 'cache-manager';
import _ from 'lodash';
import { Op, QueryTypes, WhereOptions } from 'sequelize';
import { FindOptions, Order } from 'sequelize/types/model';
import { getPublishedCVQuery } from '../cvs/cvs.utils';
Expand Down Expand Up @@ -95,7 +96,7 @@ export class UsersService {
const filterOptions = getMemberOptions(filtersObj);

const options: FindOptions<User> = {
subQuery: false,
//subQuery: false,
order,
offset,
limit,
Expand All @@ -119,81 +120,100 @@ export class UsersService {
};
}

const associatedUserOptions: {
coach: WhereOptions<UserCandidat>;
candidat: WhereOptions<UserCandidat>;
} = {
coach: {},
candidat: {},
};

if (filterOptions.role && isRoleIncluded(AllUserRoles, role)) {
options.where = {
...options.where,
role: filterOptions.role,
};

if (filterOptions.associatedUser) {
if (isRoleIncluded(CandidateUserRoles, role)) {
options.where = {
...(options.where || {}),
...filterOptions.associatedUser.candidate,
associatedUserOptions.candidat = {
candidat: filterOptions.associatedUser.candidate,
};
}
if (isRoleIncluded(CoachUserRoles, role)) {
options.where = {
...(options.where || {}),
...filterOptions.associatedUser.coach,
associatedUserOptions.coach = {
coach: filterOptions.associatedUser.coach,
};
}
}
}

const userCandidatOptions: FindOptions<UserCandidat> = {};
if (
isRoleIncluded(CandidateUserRoles, role) &&
(filterOptions.hidden || filterOptions.employed)
) {
userCandidatOptions.where = {};
if (filterOptions.hidden) {
userCandidatOptions.where = {
...userCandidatOptions.where,
hidden: filterOptions.hidden,
};
}
if (filterOptions.employed) {
userCandidatOptions.where = {
...userCandidatOptions.where,
employed: filterOptions.employed,
};
if (isRoleIncluded(CandidateUserRoles, role)) {
userCandidatOptions.where = { ...associatedUserOptions.candidat };
if (filterOptions.hidden || filterOptions.employed) {
if (filterOptions.hidden) {
userCandidatOptions.where = {
...userCandidatOptions.where,
hidden: filterOptions.hidden,
};
}
if (filterOptions.employed) {
userCandidatOptions.where = {
...userCandidatOptions.where,
employed: filterOptions.employed,
};
}
}
}

/*
if (filterOptions.cvStatus) {
options.where = {
...(options.where || {}),
'$candidat.cvs.status$': filterOptions.cvStatus,
};
}
}*/

if (filterOptions.businessLines) {
/* if (filterOptions.businessLines) {
options.where = {
...(options.where || {}),
'$candidat.cvs.businessLines.name$': filterOptions.businessLines,
...(options.where || {}),K
name: filterOptions.businessLines,
};
}
}*/

options.include = [
{
model: UserCandidat,
as: 'candidat',
attributes: ['coachId', ...UserCandidatAttributes],
...userCandidatOptions,
required: !_.isEmpty(associatedUserOptions.candidat),
include: [
{
model: CV,
as: 'cvs',
attributes: ['version', 'status', 'urlImg'],
required: !!filterOptions.cvStatus || !!filterOptions.businessLines,
where: lastCVVersionWhereOptions,
required:
isRoleIncluded(CandidateUserRoles, role) &&
(!!filterOptions.cvStatus || !!filterOptions.businessLines),
where: {
...(isRoleIncluded(CandidateUserRoles, role)
? lastCVVersionWhereOptions
: {}),
...(filterOptions.cvStatus
? { status: filterOptions.cvStatus }
: {}),
},
include: [
{
model: BusinessLine,
as: 'businessLines',
attributes: ['name', 'order'],
required: !!filterOptions.businessLines,
where: filterOptions.businessLines
? {
name: filterOptions.businessLines,
}
: {},
},
],
},
Expand All @@ -214,6 +234,8 @@ export class UsersService {
{
model: UserCandidat,
as: 'coaches',
where: associatedUserOptions.coach,
required: !_.isEmpty(associatedUserOptions.coach),
attributes: ['candidatId', ...UserCandidatAttributes],
include: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/users/users.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const lastCVVersionWhereOptions: WhereOptions<UserCandidat> = {
literal(`
SELECT MAX("CVs"."version")
FROM "CVs"
WHERE "candidat"."candidatId" = "CVs"."UserId"
WHERE "candidatId" = "CVs"."UserId"
GROUP BY "CVs"."UserId"
`),
],
Expand Down

0 comments on commit f3395a1

Please sign in to comment.