Skip to content

Commit

Permalink
fix(members): fixed members request
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Oct 26, 2023
1 parent f0a174e commit d93ebb3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
13 changes: 6 additions & 7 deletions src/mails/mails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,27 +402,26 @@ export class MailsService {
return status;
});

const { companiesAdminMail } = getAdminMailsFromDepartment(
opportunity.department
);

if (
allStatus.every((status) => {
return status < 0;
})
) {
const toEmail: CustomMailParams['toEmail'] = {
to: opportunity.contactMail || opportunity.recruiterMail,
bcc: process.env[
`ADMIN_COMPANIES_${getZoneFromDepartment(opportunity.department)}`
],
bcc: companiesAdminMail,
};

await this.queuesService.addToWorkQueue(Jobs.SEND_MAIL, {
toEmail,
templateId: opportunity.isPublic
? MailjetTemplates.OFFER_PUBLIC_NO_RESPONSE
: MailjetTemplates.OFFER_PRIVATE_NO_RESPONSE,
replyTo:
process.env[
`ADMIN_COMPANIES_${getZoneFromDepartment(opportunity.department)}`
],
replyTo: companiesAdminMail,
variables: getMailjetVariablesForPrivateOrPublicOffer(
opportunity.toJSON()
),
Expand Down
1 change: 0 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { User, UserCandidat } from './models';
import { UserCandidatsService } from './user-candidats.service';
import { UsersService } from './users.service';
import {
AllUserRoles,
CandidateUserRoles,
CoachUserRoles,
MemberFilterKey,
Expand Down
51 changes: 26 additions & 25 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Cache } from 'cache-manager';
import _ from 'lodash';
import { Includeable, Op, QueryTypes, WhereOptions } from 'sequelize';
import { Op, QueryTypes, WhereOptions } from 'sequelize';
import { FindOptions, Order } from 'sequelize/types/model';
import { getPublishedCVQuery } from '../cvs/cvs.utils';
import { BusinessLine } from 'src/common/business-lines/models';
Expand All @@ -12,7 +11,6 @@ import { MailsService } from 'src/mails/mails.service';
import { Organization } from 'src/organizations/models';
import { QueuesService } from 'src/queues/producers/queues.service';
import { Jobs } from 'src/queues/queues.types';
import { getFiltersObjectsFromQueryParams } from 'src/utils/misc';
import { AdminZone, FilterParams, RedisKeys } from 'src/utils/types';
import { UpdateUserDto } from './dto';
import {
Expand All @@ -27,16 +25,13 @@ import {
CandidateUserRoles,
CoachUserRoles,
CVStatuses,
MemberConstantType,
MemberFilterKey,
MemberFilters,
UserRole,
UserRoles,
} from './users.types';

import {
getCommonMembersFilterOptions,
getMemberOptions,
lastCVVersionWhereOptions,
userSearchQuery,
} from './users.utils';
Expand Down Expand Up @@ -86,25 +81,32 @@ export class UsersService {
): Promise<User[]> {
const { options, filterOptions } = getCommonMembersFilterOptions(params);

const userCandidatOptions: FindOptions<UserCandidat> = {};
let userCandidatWhereOptions: FindOptions<UserCandidat> = {};

if (filterOptions.associatedUser) {
userCandidatOptions.where = {
candidat: filterOptions.associatedUser.candidate,
userCandidatWhereOptions = {
where: {
...userCandidatWhereOptions.where,
...filterOptions.associatedUser.candidat,
},
};
}

if (filterOptions.hidden || filterOptions.employed) {
if (filterOptions.hidden) {
userCandidatOptions.where = {
...userCandidatOptions.where,
hidden: filterOptions.hidden,
userCandidatWhereOptions = {
where: {
...userCandidatWhereOptions.where,
hidden: filterOptions.hidden,
},
};
}
if (filterOptions.employed) {
userCandidatOptions.where = {
...userCandidatOptions.where,
employed: filterOptions.employed,
userCandidatWhereOptions = {
where: {
...userCandidatWhereOptions.where,
employed: filterOptions.employed,
},
};
}
}
Expand All @@ -116,7 +118,7 @@ export class UsersService {
model: UserCandidat,
as: 'candidat',
attributes: ['coachId', 'candidatId', ...UserCandidatAttributes],
...userCandidatOptions,
...userCandidatWhereOptions,
include: [
{
model: CV,
Expand Down Expand Up @@ -178,23 +180,22 @@ export class UsersService {
): Promise<User[]> {
const { options, filterOptions } = getCommonMembersFilterOptions(params);

const userCandidatOptions: FindOptions<UserCandidat> = {};

if (filterOptions.associatedUser) {
userCandidatOptions.where = {
coach: filterOptions.associatedUser.coach,
};
}
const associatedUserWhereOptions = filterOptions.associatedUser
? filterOptions.associatedUser.coach
: {};

return this.userModel.findAll({
...options,
where: {
...options.where,
...associatedUserWhereOptions,
},
include: [
{
model: UserCandidat,
as: 'coaches',
...userCandidatOptions,
required: !_.isEmpty(filterOptions.associatedUser),
attributes: ['coachId', 'candidatId', ...UserCandidatAttributes],
duplicating: false,
include: [
{
model: User,
Expand Down
13 changes: 10 additions & 3 deletions src/users/users.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Op } from 'sequelize';
import { Where } from 'sequelize/types/utils';
import {
BusinessLineFilters,
BusinessLineValue,
Expand Down Expand Up @@ -119,13 +118,21 @@ export const CVStatusFilters: FilterConstant<CVStatus>[] = [
CVStatuses.NEW,
];

type AssociatedUserWhereOptions = {
[K in string]: { [Op.is]: null } | { [Op.not]: null };
};

export interface MemberOptions {
role: { [Op.or]: UserRole[] };
zone: { [Op.or]: AdminZone[] };
businessLines: { [Op.in]: BusinessLineValue[] };
associatedUser: {
candidate: { [Op.or]: Where[] };
coach: { [Op.or]: Where[] };
candidat: {
[Op.or]: AssociatedUserWhereOptions[];
};
coach: {
[Op.or]: AssociatedUserWhereOptions[];
};
};
hidden: { [Op.or]: boolean[] };
employed: { [Op.or]: boolean[] };
Expand Down
25 changes: 12 additions & 13 deletions src/users/users.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,19 @@ export function getMemberOptions(
for (let i = 0; i < keys.length; i += 1) {
if (filtersObj[keys[i]].length > 0) {
if (keys[i] === 'associatedUser') {
// These options don't work
whereOptions = {
...whereOptions,
[keys[i]]: {
coach: {
[Op.or]: filtersObj[keys[i]].map((currentFilter) => {
return where(
col(`coaches.candidatId`),
currentFilter.value ? Op.not : Op.is,
null
);
return {
'$coaches.candidatId$': {
[currentFilter.value ? Op.not : Op.is]: null,
},
};
}),
},
candidate: {
candidat: {
[Op.or]: filtersObj[keys[i]].map((currentFilter) => {
return where(
col(`candidat.coachId`),
Expand All @@ -160,7 +159,7 @@ export function getMemberOptions(
);
}),
},
} as MemberOptions['associatedUser'],
},
};
} else {
whereOptions = {
Expand Down Expand Up @@ -265,10 +264,10 @@ export const lastCVVersionWhereOptions: WhereOptions<UserCandidat> = {
version: {
[Op.in]: [
literal(`
SELECT MAX("CVs"."version")
FROM "CVs"
WHERE "User".id = "CVs"."UserId"
GROUP BY "CVs"."UserId"
SELECT MAX("CVs"."version")
FROM "CVs"
WHERE "User".id = "CVs"."UserId"
GROUP BY "CVs"."UserId"
`),
],
},
Expand Down Expand Up @@ -328,7 +327,7 @@ export function getCommonMembersFilterOptions(
search: string;
order: Order;
} & FilterParams<MemberFilterKey>
): { options: FindOptions<User>, filterOptions: MemberOptions } {
): { options: FindOptions<User>; filterOptions: MemberOptions } {
const { limit, offset, search, order, ...restParams } = params;

const filtersObj = getFiltersObjectsFromQueryParams<
Expand Down

0 comments on commit d93ebb3

Please sign in to comment.