From 6ed383995bbfd1afaeb8fbb4a56470b6f32403aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mir=C3=B3=20Sorja?= Date: Mon, 6 Jan 2025 14:00:01 +0200 Subject: [PATCH] 4196 - Show invited users in country collaborators (#4198) * 4196 - Show invited users in country collaborators * deepscan * 4196 - Admin/Collaborators: Hide invitations --- .../AdminCollaborators/hooks/useFilters.tsx | 7 +++++++ src/meta/tablePaginated/tablePaginated.ts | 6 ++++-- src/server/api/user/getMany.ts | 5 ++++- .../user/utils/getPropsToQueryParams.ts | 20 ++++++++++++++----- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/client/pages/AdminCollaborators/hooks/useFilters.tsx b/src/client/pages/AdminCollaborators/hooks/useFilters.tsx index 1295b05372..30a66d646d 100644 --- a/src/client/pages/AdminCollaborators/hooks/useFilters.tsx +++ b/src/client/pages/AdminCollaborators/hooks/useFilters.tsx @@ -33,6 +33,13 @@ export const useFilters = (): Returned => { label: '', type: TablePaginatedFilterType.SWITCH, }, + { + defaultValue: false, + fieldName: 'invitations', + hidden: true, + label: '', + type: TablePaginatedFilterType.SWITCH, + }, { fieldName: 'roles', label: t('common.role'), diff --git a/src/meta/tablePaginated/tablePaginated.ts b/src/meta/tablePaginated/tablePaginated.ts index 14574d3568..abeb6a260d 100644 --- a/src/meta/tablePaginated/tablePaginated.ts +++ b/src/meta/tablePaginated/tablePaginated.ts @@ -1,5 +1,5 @@ import { CountryIso } from 'meta/area' -import { RoleName } from 'meta/user' +import { RoleName, UserStatus } from 'meta/user' export enum TablePaginatedOrderByDirection { asc = 'asc', @@ -27,7 +27,9 @@ export type TablePaginatedFilterValues = boolean | string | Array export type UserFilters = { administrators?: boolean countries?: Array + disabled?: boolean fullName?: string + invitations?: boolean roles?: Array - disabled?: boolean + statuses?: Array } diff --git a/src/server/api/user/getMany.ts b/src/server/api/user/getMany.ts index f8e0067e4f..28328c762d 100644 --- a/src/server/api/user/getMany.ts +++ b/src/server/api/user/getMany.ts @@ -1,6 +1,8 @@ import { Response } from 'express' import { CycleRequest } from 'meta/api/request' +import { UserFilters } from 'meta/tablePaginated' +import { UserStatus } from 'meta/user' import { AssessmentController } from 'server/controller/assessment' import { UserController } from 'server/controller/user' @@ -13,7 +15,8 @@ export const getMany = async (req: CycleRequest<{ print: string }>, res: Respons const { assessment, cycle } = await AssessmentController.getOneWithCycle({ assessmentName, cycleName }) - let users = await UserController.getMany({ assessment, cycle, countryIso }) + const filters: UserFilters = { statuses: [UserStatus.active, UserStatus.invitationPending] } + let users = await UserController.getMany({ assessment, cycle, countryIso, filters }) if (print && print === 'true') users = users.filter((user) => !ProcessEnv.fraReportCollaboratorsExcluded.includes(user.email)) diff --git a/src/server/repository/public/user/utils/getPropsToQueryParams.ts b/src/server/repository/public/user/utils/getPropsToQueryParams.ts index 919fd23181..4c0559413d 100644 --- a/src/server/repository/public/user/utils/getPropsToQueryParams.ts +++ b/src/server/repository/public/user/utils/getPropsToQueryParams.ts @@ -10,8 +10,18 @@ type Returned = { whereConditions: Array; queryParams: UserQueryParams } export const getPropsToQueryParams = (props: UsersGetManyProps): Returned => { const { countryIso, filters = {}, limit, offset } = props - const { administrators, countries, fullName, roles, disabled } = filters - const statuses = disabled ? [UserStatus.disabled] : [UserStatus.active] + const { + administrators, + countries, + disabled, + fullName, + invitations = true, + roles, + statuses: defaultStatuses = [UserStatus.active], + } = filters + + // Only return users with active status, unless other statuses specified outside + const statuses = disabled ? [UserStatus.disabled] : defaultStatuses const queryParams: UserQueryParams = {} @@ -32,13 +42,13 @@ export const getPropsToQueryParams = (props: UsersGetManyProps): Returned => { if (!Objects.isNil(limit)) queryParams.limit = limit if (!Objects.isNil(offset)) queryParams.offset = offset + const withInvitations = invitations ? `or (invitation is not null and invitation ->> 'role' in ($(roles:list)))` : '' + const whereConditions = [ fullName && `full_name ilike '%' || $(fullName) || '%'`, selectedRoles && `( - (role is not null and role ->> 'role' in ($(roles:list))) - or - (invitation is not null and invitation ->> 'role' in ($(roles:list))) + (role is not null and role ->> 'role' in ($(roles:list))) ${withInvitations} )`, countryIso && `country_iso = $(countryIso)`, hasCountries && `country_iso in ($(countries:list))`,