Skip to content

Commit

Permalink
Merge branch 'develop' into epic/EN-7455_orienter_candidats
Browse files Browse the repository at this point in the history
  • Loading branch information
guillobits authored Nov 8, 2024
2 parents 831a236 + 2e98116 commit 240341e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 59 deletions.
73 changes: 35 additions & 38 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {

import {
getCommonMembersFilterOptions,
getRawLastCVVersionWhereOptions,
lastCVVersionWhereOptions,
userSearchQuery,
userSearchQueryRaw,
Expand Down Expand Up @@ -74,19 +73,6 @@ export class UsersService {
});
}

async findAllLastCVVersions(): Promise<
{ candidateId: string; maxVersion: number }[]
> {
return this.userModel.sequelize.query(
`SELECT "CVs"."UserId" as "candidateId", MAX("CVs"."version") as "maxVersion"
FROM "CVs"
GROUP BY "CVs"."UserId"`,
{
type: QueryTypes.SELECT,
}
);
}

async findAllCandidateMembers(
params: {
limit: number;
Expand All @@ -101,24 +87,26 @@ export class UsersService {
role: [UserRoles.CANDIDATE],
});

const lastCVVersions = await this.findAllLastCVVersions();

const candidatesIds: { nameAndId: string; userId: string }[] =
const candidatesIds: { userId: string }[] =
await this.userModel.sequelize.query(
`
SELECT
DISTINCT("User"."firstName", "User"."id") as "nameAndId",
"User"."id" as "userId"
FROM "Users" AS "User"
LEFT OUTER JOIN "User_Candidats" AS "candidat"
ON "User"."id" = "candidat"."candidatId"
LEFT OUTER JOIN "CVs" AS "candidat->cvs"
ON "candidat"."candidatId" = "candidat->cvs"."UserId"
AND
"candidat->cvs"."deletedAt" IS NULL ${getRawLastCVVersionWhereOptions(
lastCVVersions
)}
AND "candidat->cvs"."deletedAt" IS NULL
AND ("UserId", "version") IN (
SELECT
"CVs"."UserId" AS "candidateId", MAX("CVs"."version") AS "maxVersion"
FROM "CVs"
GROUP BY
"CVs"."UserId"
)
LEFT OUTER JOIN "CV_BusinessLines" AS "candidat->cvs->businessLines->CVBusinessLine"
ON "candidat->cvs"."id" = "candidat->cvs->businessLines->CVBusinessLine"."CVId"
LEFT OUTER JOIN "BusinessLines" AS "candidat->cvs->businessLines"
Expand All @@ -137,7 +125,8 @@ export class UsersService {
search ? `AND ${userSearchQueryRaw(search, true)}` : ''
}
ORDER BY ("User"."firstName", "User"."id") ASC
GROUP BY "User"."id"
ORDER BY "User"."firstName" ASC
LIMIT ${limit}
OFFSET ${offset}
`,
Expand Down Expand Up @@ -226,25 +215,33 @@ export class UsersService {
role: [UserRoles.COACH],
});

const coachesIds: { nameAndId: string; userId: string }[] =
const coachesIds: { userId: string }[] =
await this.userModel.sequelize.query(
`
SELECT DISTINCT("User"."firstName", "User"."id") as "nameAndId", "User"."id" as "userId"
FROM "Users" as "User"
LEFT OUTER JOIN "User_Candidats" AS "coaches" ON "User"."id" = "coaches"."coachId"
LEFT OUTER JOIN "Users" AS "coaches->candidat"
ON "coaches"."candidatId" = "coaches->candidat"."id" AND
("coaches->candidat"."deletedAt" IS NULL)
LEFT OUTER JOIN "Organizations" AS "coaches->candidat->organization"
ON "coaches->candidat"."OrganizationId" = "coaches->candidat->organization"."id"
LEFT OUTER JOIN "Organizations" AS "organization" ON "User"."OrganizationId" = "organization"."id"
WHERE "User"."deletedAt" IS NULL
AND ${filterOptions.join(' AND ')} ${
SELECT
"User"."id" as "userId"
FROM "Users" as "User"
LEFT OUTER JOIN "User_Candidats" AS "coaches"
ON "User"."id" = "coaches"."coachId"
LEFT OUTER JOIN "Users" AS "coaches->candidat"
ON "coaches"."candidatId" = "coaches->candidat"."id"
AND ("coaches->candidat"."deletedAt" IS NULL)
LEFT OUTER JOIN "Organizations" AS "coaches->candidat->organization"
ON "coaches->candidat"."OrganizationId" = "coaches->candidat->organization"."id"
LEFT OUTER JOIN "Organizations" AS "organization"
ON "User"."OrganizationId" = "organization"."id"
WHERE "User"."deletedAt" IS NULL
AND ${filterOptions.join(' AND ')} ${
search ? `AND ${userSearchQueryRaw(search, true)}` : ''
}
ORDER BY ("User"."firstName", "User"."id") ASC
LIMIT ${limit}
OFFSET ${offset}
GROUP BY "User"."id"
ORDER BY "User"."firstName" ASC
LIMIT ${limit}
OFFSET ${offset}
`,
{
type: QueryTypes.SELECT,
Expand Down
21 changes: 0 additions & 21 deletions src/users/users.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,27 +333,6 @@ export function userSearchQueryRaw(query = '', withOrganizationName = false) {
)`;
}

export function getRawLastCVVersionWhereOptions(
maxVersions: {
candidateId: string;
maxVersion: number;
}[]
) {
if (maxVersions && maxVersions.length > 0) {
return `
AND ("UserId","version") IN (
${maxVersions
.map(({ candidateId, maxVersion }) => {
return `('${candidateId}','${maxVersion}')`;
})
.join(',')}
)
`;
}

return '';
}

export const lastCVVersionWhereOptions: WhereOptions<UserCandidat> = {
version: {
[Op.in]: [
Expand Down

0 comments on commit 240341e

Please sign in to comment.