Skip to content

Commit

Permalink
feat: add search by surname to proposal table (#728)
Browse files Browse the repository at this point in the history
Co-authored-by: Farai Mutambara <71100224+mutambaraf@users.noreply.github.com>
  • Loading branch information
ellen-wright and mutambaraf authored Oct 15, 2024
1 parent 86c1697 commit 620e7a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
26 changes: 25 additions & 1 deletion apps/backend/src/datasources/postgres/ProposalDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,22 @@ export default class PostgresProposalDataSource implements ProposalDataSource {
offset?: number,
sortField?: string,
sortDirection?: string,
searchText?: string
searchText?: string,
principleInvestigator?: number[]
): Promise<{ totalCount: number; proposalViews: ProposalView[] }> {
const principalInvestigator = principleInvestigator
? principleInvestigator
: [];

return database
.select(['*', database.raw('count(*) OVER() AS full_count')])
.from('proposal_table_view')
.join(
'users',
'users.user_id',
'=',
'proposal_table_view.principal_investigator'
)
.modify((query) => {
if (filter?.callId) {
query.where('call_id', filter?.callId);
Expand Down Expand Up @@ -427,6 +438,10 @@ export default class PostgresProposalDataSource implements ProposalDataSource {
.orWhereRaw('proposal_id ILIKE ?', `%${searchText}%`)
.orWhereRaw('title ILIKE ?', `%${searchText}%`)
.orWhereRaw('proposal_status_name ILIKE ?', `%${searchText}%`)
.orWhere('users.email', 'ilike', `%${searchText}%`)
.orWhere('users.firstname', 'ilike', `%${searchText}%`)
.orWhere('users.lastname', 'ilike', `%${searchText}%`)
.orWhere('principal_investigator', 'in', principalInvestigator)
// NOTE: Using jsonpath we check the jsonb (instruments) field if it contains object with name equal to searchText case insensitive
.orWhereRaw(
'jsonb_path_exists(instruments, \'$[*].name \\? (@.type() == "string" && @ like_regex :searchText: flag "i")\')',
Expand Down Expand Up @@ -569,6 +584,12 @@ export default class PostgresProposalDataSource implements ProposalDataSource {
return database
.select(['*', database.raw('count(*) OVER() AS full_count')])
.from('proposal_table_view')
.join(
'users',
'users.user_id',
'=',
'proposal_table_view.principal_investigator'
)
.where(function () {
if (user.currentRole?.shortCode === Roles.INTERNAL_REVIEWER) {
// NOTE: Using jsonpath we check the jsonb (technical_reviews) field if it contains internalReviewers array of objects with id equal to user.id
Expand All @@ -595,6 +616,9 @@ export default class PostgresProposalDataSource implements ProposalDataSource {
.orWhereRaw('title ILIKE ?', `%${filter.text}%`)
.orWhereRaw('proposal_id ILIKE ?', `%${filter.text}%`)
.orWhereRaw('proposal_status_name ILIKE ?', `%${filter.text}%`)
.orWhereRaw('users.email ILIKE', `%${filter.text}%`)
.orWhereRaw('users.firstname ILIKE', `%${filter.text}%`)
.orWhereRaw('users.lastname ILIKE', `%${filter.text}%`)
// NOTE: Using jsonpath we check the jsonb (instruments) field if it contains object with name equal to searchText case insensitive
.orWhereRaw(
'jsonb_path_exists(instruments, \'$[*].name \\? (@.type() == "string" && @ like_regex :searchText: flag "i")\')',
Expand Down
29 changes: 27 additions & 2 deletions apps/backend/src/datasources/stfc/StfcProposalDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export default class StfcProposalDataSource extends PostgresProposalDataSource {
first?: number,
offset?: number
): Promise<{ totalCount: number; proposals: ProposalView[] }> {
const stfcUserIds: number[] = filter?.text
? [
...(
await stfcUserDataSource.getUsers({ filter: filter.text })
).users.map((user) => user.id),
]
: [];

const proposals = database
.select('proposal_pk')
.from('proposal_table_view')
Expand Down Expand Up @@ -59,10 +67,15 @@ export default class StfcProposalDataSource extends PostgresProposalDataSource {
);
}
});

const result = database
.select(['*', database.raw('count(*) OVER() AS full_count')])
.from('proposal_table_view')
.join(
'users',
'users.user_id',
'=',
'proposal_table_view.principal_investigator'
)
.whereIn('proposal_pk', proposals)
.orderBy('proposal_pk', 'desc')
.modify((query) => {
Expand All @@ -71,6 +84,10 @@ export default class StfcProposalDataSource extends PostgresProposalDataSource {
this.where('title', 'ilike', `%${filter.text}%`)
.orWhere('proposal_id', 'ilike', `%${filter.text}%`)
.orWhere('proposal_status_name', 'ilike', `%${filter.text}%`)
.orWhere('users.email', 'ilike', `%${filter.text}%`)
.orWhere('users.firstname', 'ilike', `%${filter.text}%`)
.orWhere('users.lastname', 'ilike', `%${filter.text}%`)
.orWhere('principal_investigator', 'in', stfcUserIds)
// NOTE: Using jsonpath we check the jsonb (instruments) field if it contains object with name equal to searchText case insensitive
.orWhereRaw(
'jsonb_path_exists(instruments, \'$[*].name \\? (@.type() == "string" && @ like_regex :searchText: flag "i")\')',
Expand Down Expand Up @@ -156,13 +173,21 @@ export default class StfcProposalDataSource extends PostgresProposalDataSource {
sortDirection?: string,
searchText?: string
): Promise<{ totalCount: number; proposalViews: ProposalView[] }> {
const stfcUserIds: number[] = searchText
? [
...(
await stfcUserDataSource.getUsers({ filter: searchText })
).users.map((ids) => ids.id),
]
: [];
const proposals = await super.getProposalsFromView(
filter,
first,
offset,
sortField,
sortDirection,
searchText
searchText,
stfcUserIds
);

const technicalReviewers = removeDuplicates(
Expand Down

0 comments on commit 620e7a0

Please sign in to comment.