Skip to content

Commit

Permalink
fix: Searching on the applications table causes the page to crash (#3408
Browse files Browse the repository at this point in the history
)

* fix: pass proper value to to_tsquery function

* fix: search applications using ILIKE

* fix: change where to orWhere

* feat: add application search by confirmation code
  • Loading branch information
KrissDrawing committed Apr 26, 2023
1 parent 2ba7cc1 commit cb85ee8
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions backend/core/src/applications/services/applications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Scope,
} from "@nestjs/common"
import { InjectRepository } from "@nestjs/typeorm"
import { DeepPartial, QueryFailedError, Repository } from "typeorm"
import { Brackets, DeepPartial, QueryFailedError, Repository } from "typeorm"
import { paginate, Pagination, PaginationTypeEnum } from "nestjs-typeorm-paginate"
import { Request as ExpressRequest } from "express"
import { REQUEST } from "@nestjs/core"
Expand Down Expand Up @@ -266,13 +266,33 @@ export class ApplicationsService {
listingId: (qb, { listingId }) =>
qb.andWhere("application.listing_id = :lid", { lid: listingId }),
orderBy: (qb, { orderBy, order }) => qb.orderBy(orderBy, order, "NULLS LAST"),
search: (qb, { search }) =>
search: (qb, { search }) => {
qb.andWhere(
`to_tsvector('english', REGEXP_REPLACE(concat_ws(' ', applicant, alternateContact.emailAddress), '[_]|[-]', '/', 'g')) @@ to_tsquery(CONCAT(CAST(REGEXP_REPLACE(:search, '[_]|[-]', '/', 'g') as text), ':*'))`,
{
search,
}
),
new Brackets((subQb) => {
subQb.where("application.confirmationCode ILIKE :search", { search: `%${search}%` })
subQb.orWhere("applicant.firstName ILIKE :search", { search: `%${search}%` })
subQb.orWhere("applicant.lastName ILIKE :search", { search: `%${search}%` })
subQb.orWhere("applicant.emailAddress ILIKE :search", {
search: `%${search}%`,
})
subQb.orWhere("applicant.phoneNumber ILIKE :search", { search: `%${search}%` })
subQb.orWhere(
"CONCAT(applicant.firstName, ' ', applicant.lastName, ' ', applicant.emailAddress, ' ', applicant.phoneNumber) ILIKE :search",
{ search: `%${search}%` }
)
subQb.orWhere("alternateContact.firstName ILIKE :search", { search: `%${search}%` })
subQb.orWhere("alternateContact.lastName ILIKE :search", { search: `%${search}%` })
subQb.orWhere("alternateContact.emailAddress ILIKE :search", {
search: `%${search}%`,
})
subQb.orWhere("alternateContact.phoneNumber ILIKE :search", { search: `%${search}%` })
subQb.orWhere(
"CONCAT(alternateContact.firstName, ' ', alternateContact.lastName, ' ', alternateContact.emailAddress, ' ', alternateContact.phoneNumber) ILIKE :search",
{ search: `%${search}%` }
)
})
)
},
}

// --> Build main query
Expand Down

0 comments on commit cb85ee8

Please sign in to comment.