mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #1860 from gradido/1840-refactor-to-filter-object
refactor: 🍰 Create Filter Object in GQL And Rename Args
This commit is contained in:
commit
83e59145d2
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -528,7 +528,7 @@ jobs:
|
||||
report_name: Coverage Backend
|
||||
type: lcov
|
||||
result_path: ./backend/coverage/lcov.info
|
||||
min_coverage: 64
|
||||
min_coverage: 66
|
||||
token: ${{ github.token }}
|
||||
|
||||
##########################################################################
|
||||
|
||||
@ -5,15 +5,13 @@ export const searchUsers = gql`
|
||||
$searchText: String!
|
||||
$currentPage: Int
|
||||
$pageSize: Int
|
||||
$filterByActivated: Boolean
|
||||
$filterByDeleted: Boolean
|
||||
$filters: SearchUsersFiltersInput
|
||||
) {
|
||||
searchUsers(
|
||||
searchText: $searchText
|
||||
currentPage: $currentPage
|
||||
pageSize: $pageSize
|
||||
filterByActivated: $filterByActivated
|
||||
filterByDeleted: $filterByDeleted
|
||||
filters: $filters
|
||||
) {
|
||||
userCount
|
||||
userList {
|
||||
|
||||
@ -71,9 +71,11 @@ describe('Creation', () => {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: true,
|
||||
filterByDeleted: false,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -271,9 +273,11 @@ describe('Creation', () => {
|
||||
searchText: 'XX',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: true,
|
||||
filterByDeleted: false,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -288,9 +292,11 @@ describe('Creation', () => {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: true,
|
||||
filterByDeleted: false,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -305,9 +311,11 @@ describe('Creation', () => {
|
||||
searchText: '',
|
||||
currentPage: 2,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: true,
|
||||
filterByDeleted: false,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@ -102,9 +102,11 @@ export default {
|
||||
searchText: this.criteria,
|
||||
currentPage: this.currentPage,
|
||||
pageSize: this.perPage,
|
||||
filters: {
|
||||
filterByActivated: true,
|
||||
filterByDeleted: false,
|
||||
},
|
||||
},
|
||||
fetchPolicy: 'network-only',
|
||||
})
|
||||
.then((result) => {
|
||||
|
||||
@ -7,7 +7,7 @@ const localVue = global.localVue
|
||||
const apolloQueryMock = jest.fn().mockResolvedValue({
|
||||
data: {
|
||||
searchUsers: {
|
||||
userCount: 1,
|
||||
userCount: 4,
|
||||
userList: [
|
||||
{
|
||||
userId: 1,
|
||||
@ -82,9 +82,11 @@ describe('UserSearch', () => {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -101,9 +103,11 @@ describe('UserSearch', () => {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: false,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -121,9 +125,11 @@ describe('UserSearch', () => {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -141,9 +147,11 @@ describe('UserSearch', () => {
|
||||
searchText: '',
|
||||
currentPage: 2,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -161,9 +169,11 @@ describe('UserSearch', () => {
|
||||
searchText: 'search string',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
@ -178,9 +188,11 @@ describe('UserSearch', () => {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@ -97,9 +97,11 @@ export default {
|
||||
searchText: this.criteria,
|
||||
currentPage: this.currentPage,
|
||||
pageSize: this.perPage,
|
||||
filters: {
|
||||
filterByActivated: this.filterByActivated,
|
||||
filterByDeleted: this.filterByDeleted,
|
||||
},
|
||||
},
|
||||
fetchPolicy: 'no-cache',
|
||||
})
|
||||
.then((result) => {
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { ArgsType, Field, Int } from 'type-graphql'
|
||||
import SearchUsersFilters from '@arg/SearchUsersFilters'
|
||||
|
||||
@ArgsType()
|
||||
export default class SearchUsersArgs {
|
||||
@ -11,9 +12,6 @@ export default class SearchUsersArgs {
|
||||
@Field(() => Int, { nullable: true })
|
||||
pageSize?: number
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
filterByActivated?: boolean | null
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
filterByDeleted?: boolean | null
|
||||
@Field(() => SearchUsersFilters, { nullable: true })
|
||||
filters: SearchUsersFilters
|
||||
}
|
||||
|
||||
11
backend/src/graphql/arg/SearchUsersFilters.ts
Normal file
11
backend/src/graphql/arg/SearchUsersFilters.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Field, InputType, ObjectType } from 'type-graphql'
|
||||
|
||||
@ObjectType()
|
||||
@InputType('SearchUsersFiltersInput')
|
||||
export default class SearchUsersFilters {
|
||||
@Field(() => Boolean, { nullable: true, defaultValue: null })
|
||||
filterByActivated?: boolean | null
|
||||
|
||||
@Field(() => Boolean, { nullable: true, defaultValue: null })
|
||||
filterByDeleted?: boolean | null
|
||||
}
|
||||
@ -3,11 +3,11 @@ import { ArgsType, Field } from 'type-graphql'
|
||||
@ArgsType()
|
||||
export default class TransactionLinkFilters {
|
||||
@Field(() => Boolean, { nullable: true, defaultValue: true })
|
||||
withDeleted?: boolean
|
||||
filterByDeleted?: boolean
|
||||
|
||||
@Field(() => Boolean, { nullable: true, defaultValue: true })
|
||||
withExpired?: boolean
|
||||
filterByExpired?: boolean
|
||||
|
||||
@Field(() => Boolean, { nullable: true, defaultValue: true })
|
||||
withRedeemed?: boolean
|
||||
filterByRedeemed?: boolean
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
import { convertObjValuesToArray } from '@/util/utilities'
|
||||
import { testEnvironment, resetToken, cleanDB } from '@test/helpers'
|
||||
import { userFactory } from '@/seeds/factory/user'
|
||||
import { creationFactory } from '@/seeds/factory/creation'
|
||||
@ -11,6 +12,7 @@ import { garrickOllivander } from '@/seeds/users/garrick-ollivander'
|
||||
import {
|
||||
deleteUser,
|
||||
unDeleteUser,
|
||||
searchUsers,
|
||||
createPendingCreation,
|
||||
createPendingCreations,
|
||||
updatePendingCreation,
|
||||
@ -261,6 +263,224 @@ describe('AdminResolver', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('search users', () => {
|
||||
const variablesWithoutTextAndFilters = {
|
||||
searchText: '',
|
||||
currentPage: 1,
|
||||
pageSize: 25,
|
||||
filters: null,
|
||||
}
|
||||
|
||||
describe('unauthenticated', () => {
|
||||
it('returns an error', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
errors: [new GraphQLError('401 Unauthorized')],
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated', () => {
|
||||
describe('without admin rights', () => {
|
||||
beforeAll(async () => {
|
||||
user = await userFactory(testEnv, bibiBloxberg)
|
||||
await query({
|
||||
query: login,
|
||||
variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' },
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanDB()
|
||||
resetToken()
|
||||
})
|
||||
|
||||
it('returns an error', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
errors: [new GraphQLError('401 Unauthorized')],
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with admin rights', () => {
|
||||
const allUsers = {
|
||||
bibi: expect.objectContaining({
|
||||
email: 'bibi@bloxberg.de',
|
||||
}),
|
||||
garrick: expect.objectContaining({
|
||||
email: 'garrick@ollivander.com',
|
||||
}),
|
||||
peter: expect.objectContaining({
|
||||
email: 'peter@lustig.de',
|
||||
}),
|
||||
stephen: expect.objectContaining({
|
||||
email: 'stephen@hawking.uk',
|
||||
}),
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
admin = await userFactory(testEnv, peterLustig)
|
||||
await query({
|
||||
query: login,
|
||||
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
||||
})
|
||||
|
||||
await userFactory(testEnv, bibiBloxberg)
|
||||
await userFactory(testEnv, stephenHawking)
|
||||
await userFactory(testEnv, garrickOllivander)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanDB()
|
||||
resetToken()
|
||||
})
|
||||
|
||||
describe('without any filters', () => {
|
||||
it('finds all users', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
searchUsers: {
|
||||
userCount: 4,
|
||||
userList: expect.arrayContaining(convertObjValuesToArray(allUsers)),
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('all filters are null', () => {
|
||||
it('finds all users', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
searchUsers: {
|
||||
userCount: 4,
|
||||
userList: expect.arrayContaining(convertObjValuesToArray(allUsers)),
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('filter by unchecked email', () => {
|
||||
it('finds only users with unchecked email', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
filters: {
|
||||
filterByActivated: false,
|
||||
filterByDeleted: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
searchUsers: {
|
||||
userCount: 1,
|
||||
userList: expect.arrayContaining([allUsers.garrick]),
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('filter by deleted users', () => {
|
||||
it('finds only users with deleted account', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
filters: {
|
||||
filterByActivated: null,
|
||||
filterByDeleted: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
searchUsers: {
|
||||
userCount: 1,
|
||||
userList: expect.arrayContaining([allUsers.stephen]),
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('filter by deleted account and unchecked email', () => {
|
||||
it('finds no users', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: searchUsers,
|
||||
variables: {
|
||||
...variablesWithoutTextAndFilters,
|
||||
filters: {
|
||||
filterByActivated: false,
|
||||
filterByDeleted: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
searchUsers: {
|
||||
userCount: 0,
|
||||
userList: [],
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('creations', () => {
|
||||
const variables = {
|
||||
email: 'bibi@bloxberg.de',
|
||||
|
||||
@ -52,23 +52,19 @@ export class AdminResolver {
|
||||
@Query(() => SearchUsersResult)
|
||||
async searchUsers(
|
||||
@Args()
|
||||
{
|
||||
searchText,
|
||||
currentPage = 1,
|
||||
pageSize = 25,
|
||||
filterByActivated = null,
|
||||
filterByDeleted = null,
|
||||
}: SearchUsersArgs,
|
||||
{ searchText, currentPage = 1, pageSize = 25, filters }: SearchUsersArgs,
|
||||
): Promise<SearchUsersResult> {
|
||||
const userRepository = getCustomRepository(UserRepository)
|
||||
|
||||
const filterCriteria: ObjectLiteral[] = []
|
||||
if (filterByActivated !== null) {
|
||||
filterCriteria.push({ emailChecked: filterByActivated })
|
||||
if (filters) {
|
||||
if (filters.filterByActivated !== null) {
|
||||
filterCriteria.push({ emailChecked: filters.filterByActivated })
|
||||
}
|
||||
|
||||
if (filterByDeleted !== null) {
|
||||
filterCriteria.push({ deletedAt: filterByDeleted ? Not(IsNull()) : IsNull() })
|
||||
if (filters.filterByDeleted !== null) {
|
||||
filterCriteria.push({ deletedAt: filters.filterByDeleted ? Not(IsNull()) : IsNull() })
|
||||
}
|
||||
}
|
||||
|
||||
const userFields = ['id', 'firstName', 'lastName', 'email', 'emailChecked', 'deletedAt']
|
||||
@ -442,11 +438,11 @@ export class AdminResolver {
|
||||
} = {
|
||||
userId,
|
||||
}
|
||||
if (!filters.withRedeemed) where.redeemedBy = null
|
||||
if (!filters.withExpired) where.validUntil = MoreThan(new Date())
|
||||
if (!filters.filterByRedeemed) where.redeemedBy = null
|
||||
if (!filters.filterByExpired) where.validUntil = MoreThan(new Date())
|
||||
const [transactionLinks, count] = await dbTransactionLink.findAndCount({
|
||||
where,
|
||||
withDeleted: filters.withDeleted,
|
||||
withDeleted: filters.filterByDeleted,
|
||||
order: {
|
||||
createdAt: order,
|
||||
},
|
||||
|
||||
@ -107,6 +107,35 @@ export const unDeleteUser = gql`
|
||||
}
|
||||
`
|
||||
|
||||
export const searchUsers = gql`
|
||||
query (
|
||||
$searchText: String!
|
||||
$currentPage: Int
|
||||
$pageSize: Int
|
||||
$filters: SearchUsersFiltersInput
|
||||
) {
|
||||
searchUsers(
|
||||
searchText: $searchText
|
||||
currentPage: $currentPage
|
||||
pageSize: $pageSize
|
||||
filters: $filters
|
||||
) {
|
||||
userCount
|
||||
userList {
|
||||
userId
|
||||
firstName
|
||||
lastName
|
||||
email
|
||||
creation
|
||||
emailChecked
|
||||
hasElopage
|
||||
emailConfirmationSend
|
||||
deletedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
export const createPendingCreations = gql`
|
||||
mutation ($pendingCreations: [CreatePendingCreationArgs!]!) {
|
||||
createPendingCreations(pendingCreations: $pendingCreations) {
|
||||
|
||||
@ -29,7 +29,7 @@ const context = {
|
||||
}
|
||||
|
||||
export const cleanDB = async () => {
|
||||
// this only works as lond we do not have foreign key constraints
|
||||
// this only works as long we do not have foreign key constraints
|
||||
for (let i = 0; i < entities.length; i++) {
|
||||
await resetEntity(entities[i])
|
||||
}
|
||||
|
||||
5
backend/src/util/utilities.ts
Normal file
5
backend/src/util/utilities.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export const convertObjValuesToArray = (obj: { [x: string]: string }): Array<string> => {
|
||||
return Object.keys(obj).map(function (key) {
|
||||
return obj[key]
|
||||
})
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user