From 51e71a7a9057b316c5d44a1ba9ff39fd33490dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Magalh=C3=A3es?= Date: Mon, 9 May 2022 16:39:49 -0300 Subject: [PATCH] chore: keep the listUsers as deprecated and added listUserPaginated --- graphql/schema.graphql | 7 +++- node/resolvers/Queries/Users.ts | 58 +++++++++++++++++++++++++++++++-- node/resolvers/index.ts | 2 ++ 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/graphql/schema.graphql b/graphql/schema.graphql index bc6e031..fe33846 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -23,8 +23,13 @@ type Query { # User getUser(id: ID!): User @cacheControl(scope: PRIVATE) getUserByEmail(email: String!): User @cacheControl(scope: PRIVATE) + listUsers(organizationId: ID, costCenterId: ID, roleId: ID): [User] + @cacheControl(scope: PRIVATE, maxAge: SHORT) + @deprecated( + reason: "This query is deprecated, use listUsersPaginated query instead." + ) - listUsers( + listUsersPaginated( organizationId: ID costCenterId: ID roleId: ID diff --git a/node/resolvers/Queries/Users.ts b/node/resolvers/Queries/Users.ts index 9146d11..77acd6b 100644 --- a/node/resolvers/Queries/Users.ts +++ b/node/resolvers/Queries/Users.ts @@ -161,6 +161,62 @@ export const getUserByEmail = async (_: any, params: any, ctx: Context) => { } export const listUsers = async ( + _: any, + { + organizationId = '', + costCenterId = '', + roleId = '', + }: { organizationId: string; costCenterId: string; roleId: string }, + ctx: Context +) => { + const { + clients: { masterdata }, + } = ctx + + let res: any = [] + + const whereArray: string[] = [] + + if (organizationId) { + whereArray.push(`orgId=${organizationId}`) + } + + if (costCenterId) { + whereArray.push(`costId=${costCenterId}`) + } + + if (roleId) { + whereArray.push(`roleId=${roleId}`) + } + + const where = whereArray.join(' AND ') + + try { + res = await masterdata.searchDocuments({ + dataEntity: config.name, + fields: [ + 'id', + 'roleId', + 'userId', + 'clId', + 'orgId', + 'costId', + 'name', + 'email', + 'canImpersonate', + ], + schema: config.version, + pagination: { page: 1, pageSize: 50 }, + ...(where && { where }), + }) + + return res + } catch (e) { + return { status: 'error', message: e } + } +} + +export const listUsersPaginated = async ( _: any, { organizationId = '', @@ -243,8 +299,6 @@ export const listUsers = async ( return res } catch (e) { - console.error(e) - return { status: 'error', message: e } } } diff --git a/node/resolvers/index.ts b/node/resolvers/index.ts index 707ebbc..57ca439 100644 --- a/node/resolvers/index.ts +++ b/node/resolvers/index.ts @@ -19,6 +19,7 @@ import { getUser, getUserByEmail, listUsers, + listUsersPaginated, } from './Queries/Users' import { Routes } from './Routes' @@ -46,6 +47,7 @@ export const resolvers = { listFeatures, listRoles, listUsers, + listUsersPaginated, }, Routes, }