From d93ebb3053d83a8b79fe4d7bfee37919389c577e Mon Sep 17 00:00:00 2001 From: Emile Bex Date: Thu, 26 Oct 2023 16:28:00 +0200 Subject: [PATCH] fix(members): fixed members request --- src/mails/mails.service.ts | 13 +++++---- src/users/users.controller.ts | 1 - src/users/users.service.ts | 51 ++++++++++++++++++----------------- src/users/users.types.ts | 13 ++++++--- src/users/users.utils.ts | 25 +++++++++-------- 5 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/mails/mails.service.ts b/src/mails/mails.service.ts index e9e725c0..6755255f 100644 --- a/src/mails/mails.service.ts +++ b/src/mails/mails.service.ts @@ -402,6 +402,10 @@ export class MailsService { return status; }); + const { companiesAdminMail } = getAdminMailsFromDepartment( + opportunity.department + ); + if ( allStatus.every((status) => { return status < 0; @@ -409,9 +413,7 @@ export class MailsService { ) { 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, { @@ -419,10 +421,7 @@ export class MailsService { 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() ), diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 3f75a19e..7cb2b3b2 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -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, diff --git a/src/users/users.service.ts b/src/users/users.service.ts index d2cf9d16..2ec39e20 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -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'; @@ -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 { @@ -27,16 +25,13 @@ import { CandidateUserRoles, CoachUserRoles, CVStatuses, - MemberConstantType, MemberFilterKey, - MemberFilters, UserRole, UserRoles, } from './users.types'; import { getCommonMembersFilterOptions, - getMemberOptions, lastCVVersionWhereOptions, userSearchQuery, } from './users.utils'; @@ -86,25 +81,32 @@ export class UsersService { ): Promise { const { options, filterOptions } = getCommonMembersFilterOptions(params); - const userCandidatOptions: FindOptions = {}; + let userCandidatWhereOptions: FindOptions = {}; 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, + }, }; } } @@ -116,7 +118,7 @@ export class UsersService { model: UserCandidat, as: 'candidat', attributes: ['coachId', 'candidatId', ...UserCandidatAttributes], - ...userCandidatOptions, + ...userCandidatWhereOptions, include: [ { model: CV, @@ -178,23 +180,22 @@ export class UsersService { ): Promise { const { options, filterOptions } = getCommonMembersFilterOptions(params); - const userCandidatOptions: FindOptions = {}; - - 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, diff --git a/src/users/users.types.ts b/src/users/users.types.ts index a3ae8775..36bac1fb 100644 --- a/src/users/users.types.ts +++ b/src/users/users.types.ts @@ -1,5 +1,4 @@ import { Op } from 'sequelize'; -import { Where } from 'sequelize/types/utils'; import { BusinessLineFilters, BusinessLineValue, @@ -119,13 +118,21 @@ export const CVStatusFilters: FilterConstant[] = [ 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[] }; diff --git a/src/users/users.utils.ts b/src/users/users.utils.ts index c3134ab1..685d7a1b 100644 --- a/src/users/users.utils.ts +++ b/src/users/users.utils.ts @@ -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`), @@ -160,7 +159,7 @@ export function getMemberOptions( ); }), }, - } as MemberOptions['associatedUser'], + }, }; } else { whereOptions = { @@ -265,10 +264,10 @@ export const lastCVVersionWhereOptions: WhereOptions = { 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" `), ], }, @@ -328,7 +327,7 @@ export function getCommonMembersFilterOptions( search: string; order: Order; } & FilterParams -): { options: FindOptions, filterOptions: MemberOptions } { +): { options: FindOptions; filterOptions: MemberOptions } { const { limit, offset, search, order, ...restParams } = params; const filtersObj = getFiltersObjectsFromQueryParams<