Skip to content

Commit

Permalink
Add better support for applications search (#1092)
Browse files Browse the repository at this point in the history
* Add better support for applications search

Plain user input is now converted to tsquery with prefix support

Add tests for improved search

* Add extended applications search support

Add search by all applicant fields

Add search by alternateContact emai

Add escape of special characters to plainto_tsquery, by default they was changed to AND operators

* Add test for applications search

Co-authored-by: Bart <[email protected]>
  • Loading branch information
bpsushi and Bart authored Apr 12, 2021
1 parent e36dc7f commit 23ae320
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/core/src/applications/applications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ApplicationsService {
orderBy: (qb, { orderBy, order }) => qb.orderBy(orderBy, order),
search: (qb, { search }) =>
qb.andWhere(
`to_tsvector('english', concat_ws(' ', "applicant")) @@ plainto_tsquery(:search)`,
`to_tsvector('english', REGEXP_REPLACE(concat_ws(' ', applicant, alternateContact.emailAddress), '[_]|[-]', '/', 'g')) @@ to_tsquery(CONCAT(CAST(plainto_tsquery(REGEXP_REPLACE(:search, '[_]|[-]', '/', 'g')) as text), ':*'))`,
{
search,
}
Expand Down
45 changes: 45 additions & 0 deletions backend/core/test/applications/applications.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,51 @@ describe("Applications", () => {
expect(res.body.items[0]).toMatchObject(createRes.body)
})

it(`should allow an admin to search for users application using search query param with partial textquery`, async () => {
const body = getTestAppBody(listing1Id)
body.applicant.firstName = "John"
const createRes = await supertest(app.getHttpServer())
.post(`/applications/submit`)
.send(body)
.expect(201)
expect(createRes.body).toMatchObject(body)
expect(createRes.body).toHaveProperty("createdAt")
expect(createRes.body).toHaveProperty("updatedAt")
expect(createRes.body).toHaveProperty("id")

const res = await supertest(app.getHttpServer())
.get(`/applications/?search=joh`)
.set(...setAuthorization(adminAccessToken))
.expect(200)
expect(Array.isArray(res.body.items)).toBe(true)
expect(res.body.items.length).toBe(1)
expect(res.body.items[0].id === createRes.body.id)
expect(res.body.items[0]).toMatchObject(createRes.body)
})

it(`should allow an admin to search for users application using email as textquery`, async () => {
const body = getTestAppBody(listing1Id)
body.applicant.firstName = "John"
body.applicant.emailAddress = "[email protected]"
const createRes = await supertest(app.getHttpServer())
.post(`/applications/submit`)
.send(body)
.expect(201)
expect(createRes.body).toMatchObject(body)
expect(createRes.body).toHaveProperty("createdAt")
expect(createRes.body).toHaveProperty("updatedAt")
expect(createRes.body).toHaveProperty("id")

const res = await supertest(app.getHttpServer())
.get(`/applications/[email protected]`)
.set(...setAuthorization(adminAccessToken))
.expect(200)
expect(Array.isArray(res.body.items)).toBe(true)
expect(res.body.items.length).toBe(1)
expect(res.body.items[0].id === createRes.body.id)
expect(res.body.items[0]).toMatchObject(createRes.body)
})

it(`should allow exporting applications as CSV`, async () => {
const body = getTestAppBody(listing1Id)
const createRes = await supertest(app.getHttpServer())
Expand Down

0 comments on commit 23ae320

Please sign in to comment.