Skip to content

Commit

Permalink
wip started converting to pure sql
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Nov 2, 2023
1 parent db08ef3 commit 7200ac5
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 89 deletions.
202 changes: 181 additions & 21 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Cache } from 'cache-manager';
import { Op, QueryTypes, WhereOptions } from 'sequelize';
import { literal, Op, QueryTypes, WhereOptions } from 'sequelize';
import { FindOptions, Order } from 'sequelize/types/model';
import { getPublishedCVQuery } from '../cvs/cvs.utils';
import { getFiltersObjectsFromQueryParams } from '../utils/misc';
import { BusinessLine } from 'src/common/business-lines/models';
import { Department } from 'src/common/locations/locations.types';
import { CV } from 'src/cvs/models';
Expand All @@ -25,15 +26,19 @@ import {
CandidateUserRoles,
CoachUserRoles,
CVStatuses,
MemberConstantType,
MemberFilterKey,
MemberFilters,
UserRole,
UserRoles,
} from './users.types';

import {
getCommonMembersFilterOptions,
lastCVVersionWhereOptions,
getLastCVVersionWhereOptions,
getMemberOptions,
userSearchQuery,
userSearchQueryRaw,
} from './users.utils';

@Injectable()
Expand Down Expand Up @@ -70,6 +75,132 @@ export class UsersService {
include: UserCandidatInclude,
});
}

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;
offset: number;
search: string;
order: Order;
role: typeof CandidateUserRoles;
} & FilterParams<MemberFilterKey>
): Promise<User[]> {
const { limit, offset, search, order, ...restParams } = params;

const filtersObj = getFiltersObjectsFromQueryParams<
MemberFilterKey,
MemberConstantType
>(restParams, MemberFilters);

const filterOptions = getMemberOptions(filtersObj);

const reducedFiltersObj = Object.keys(filtersObj).reduce((acc, curr) => {
return {
...acc,
[curr]: filtersObj[curr as MemberFilterKey].map(({ value }) => value),
};
}, {});

const lastCVVersions = await this.findAllLastCVVersions();

return this.userModel.sequelize.query(
`SELECT "User"."id",
"User"."OrganizationId",
"User"."firstName",
"User"."lastName",
"User"."email",
"User"."phone",
"User"."address",
"User"."role",
"User"."adminRole",
"User"."zone",
"User"."gender",
"User"."lastConnection",
"candidat"."coachId" AS "candidat.coachId",
"candidat"."candidatId" AS "candidat.candidatId",
"candidat"."employed" AS "candidat.employed",
"candidat"."hidden" AS "candidat.hidden",
"candidat"."note" AS "candidat.note",
"candidat"."url" AS "candidat.url",
"candidat"."contract" AS "candidat.contract",
"candidat"."endOfContract" AS "candidat.endOfContract",
"candidat"."lastModifiedBy" AS "candidat.lastModifiedBy",
"candidat->cvs"."id" AS "candidat.cvs.id",
"candidat->cvs"."version" AS "candidat.cvs.version",
"candidat->cvs"."status" AS "candidat.cvs.status",
"candidat->cvs"."urlImg" AS "candidat.cvs.urlImg",
"candidat->cvs->businessLines"."id" AS "candidat.cvs.businessLines.id",
"candidat->cvs->businessLines"."name" AS "candidat.cvs.businessLines.name",
"candidat->cvs->businessLines"."order" AS "candidat.cvs.businessLines.order",
"candidat->coach"."id" AS "candidat.coach.id",
"candidat->coach"."OrganizationId" AS "candidat.coach.OrganizationId",
"candidat->coach"."firstName" AS "candidat.coach.firstName",
"candidat->coach"."lastName" AS "candidat.coach.lastName",
"candidat->coach"."email" AS "candidat.coach.email",
"candidat->coach"."phone" AS "candidat.coach.phone",
"candidat->coach"."address" AS "candidat.coach.address",
"candidat->coach"."role" AS "candidat.coach.role",
"candidat->coach"."adminRole" AS "candidat.coach.adminRole",
"candidat->coach"."zone" AS "candidat.coach.zone",
"candidat->coach"."gender" AS "candidat.coach.gender",
"candidat->coach"."lastConnection" AS "candidat.coach.lastConnection",
"candidat->coach->organization"."name" AS "candidat.coach.organization.name",
"candidat->coach->organization"."address" AS "candidat.coach.organization.address",
"candidat->coach->organization"."zone" AS "candidat.coach.organization.zone",
"candidat->coach->organization"."id" AS "candidat.coach.organization.id",
"organization"."name" AS "organization.name",
"organization"."address" AS "organization.address",
"organization"."zone" AS "organization.zone",
"organization"."id" AS "organization.id"
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 ${getLastCVVersionWhereOptions(
lastCVVersions
)})
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"
ON "candidat->cvs->businessLines"."id" =
"candidat->cvs->businessLines->CVBusinessLine"."BusinessLineId"
LEFT OUTER JOIN "Users" AS "candidat->coach"
ON "candidat"."coachId" = "candidat->coach"."id" AND
("candidat->coach"."deletedAt" IS NULL)
LEFT OUTER JOIN "Organizations" AS "candidat->coach->organization"
ON "candidat->coach"."OrganizationId" = "candidat->coach->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" ASC
LIMIT ${limit}
OFFSET ${offset}
`,
{
type: QueryTypes.SELECT,
logging: console.log,
replacements: reducedFiltersObj,
nest: true,
}
);
}

/*
async findAllCandidateMembers(
params: {
limit: number;
Expand All @@ -83,14 +214,18 @@ export class UsersService {
let userCandidatWhereOptions: FindOptions<UserCandidat> = {};
if (filterOptions.associatedUser) {
userCandidatWhereOptions = {
where: {
...userCandidatWhereOptions.where,
...filterOptions.associatedUser.candidat,
},
};
}
const associatedUserWhereOptions = filterOptions.associatedUser
? filterOptions.associatedUser.candidat
: {};
/!* if (filterOptions.associatedUser) {
userCandidatWhereOptions = {
where: {
...userCandidatWhereOptions.where,
...filterOptions.associatedUser.candidat,
},
};
}*!/
if (filterOptions.hidden || filterOptions.employed) {
if (filterOptions.hidden) {
Expand All @@ -111,8 +246,19 @@ export class UsersService {
}
}
const lastCVVersions = await this.findAllLastCVVersions();
const cvWhereOptions = {
...getLastCVVersionWhereOptions(lastCVVersions),
...(filterOptions.cvStatus ? { status: filterOptions.cvStatus } : {}),
};
return this.userModel.findAll({
...options,
where: {
...options.where,
...associatedUserWhereOptions,
},
include: [
{
model: UserCandidat,
Expand All @@ -126,12 +272,11 @@ export class UsersService {
attributes: ['version', 'status', 'urlImg'],
required:
!!filterOptions.cvStatus || !!filterOptions.businessLines,
where: {
...lastCVVersionWhereOptions,
...(filterOptions.cvStatus
? { status: filterOptions.cvStatus }
: {}),
},
...(!_.isEmpty(cvWhereOptions)
? {
where: cvWhereOptions,
}
: {}),
include: [
{
model: BusinessLine,
Expand Down Expand Up @@ -168,6 +313,7 @@ export class UsersService {
],
});
}
*/

async findAllCoachMembers(
params: {
Expand All @@ -184,18 +330,30 @@ export class UsersService {
? filterOptions.associatedUser.coach
: {};

/* let userCandidatWhereOptions: FindOptions<UserCandidat> = {};
if (filterOptions.associatedUser) {
userCandidatWhereOptions = {
where: {
...userCandidatWhereOptions.where,
...filterOptions.associatedUser.coach,
},
};
}
*/
return this.userModel.findAll({
...options,
where: {
/*where: {
...options.where,
...associatedUserWhereOptions,
},
/!* ...associatedUserWhereOptions,*!/
[Op.and]: [literal(`coaches.candidatId is null`)],
},*/
where: literal(`"coaches"."candidatId" is null`),
include: [
{
model: UserCandidat,
as: 'coaches',
attributes: ['coachId', 'candidatId', ...UserCandidatAttributes],
duplicating: false,
include: [
{
model: User,
Expand Down Expand Up @@ -319,6 +477,8 @@ export class UsersService {
? ({ zone } as WhereOptions<CV>)
: {};

const lastCVVersions = await this.findAllLastCVVersions();

const options: FindOptions<User> = {
where: {
...whereOptions,
Expand All @@ -337,7 +497,7 @@ export class UsersService {
as: 'cvs',
attributes: [],
where: {
...lastCVVersionWhereOptions,
/* ...getLastCVVersionWhereOptions(lastCVVersions),*/
status: CVStatuses.PENDING.value,
},
},
Expand Down
Loading

0 comments on commit 7200ac5

Please sign in to comment.