From 69f400a9300740cd1531c79fa586a4118fe005bd Mon Sep 17 00:00:00 2001 From: einhorn_b Date: Fri, 18 Aug 2023 21:19:19 +0200 Subject: [PATCH] rewrite findContributions using QueryBuilder --- backend/jest.config.js | 1 + .../resolver/util/findContributions.ts | 96 ++++++++----------- backend/src/seeds/creation/index.ts | 8 ++ 3 files changed, 51 insertions(+), 54 deletions(-) diff --git a/backend/jest.config.js b/backend/jest.config.js index d282f8361..8b6f53f9f 100644 --- a/backend/jest.config.js +++ b/backend/jest.config.js @@ -20,6 +20,7 @@ module.exports = { '@model/(.*)': '/src/graphql/model/$1', '@union/(.*)': '/src/graphql/union/$1', '@repository/(.*)': '/src/typeorm/repository/$1', + '@typeorm/(.*)': '/src/typeorm/$1', '@test/(.*)': '/test/$1', '@entity/(.*)': // eslint-disable-next-line n/no-process-env diff --git a/backend/src/graphql/resolver/util/findContributions.ts b/backend/src/graphql/resolver/util/findContributions.ts index f8fd581e4..48d66d883 100644 --- a/backend/src/graphql/resolver/util/findContributions.ts +++ b/backend/src/graphql/resolver/util/findContributions.ts @@ -1,4 +1,5 @@ -import { In, Like, Not } from '@dbTools/typeorm' +/* eslint-disable security/detect-object-injection */ +import { Brackets, In, Like, Not, SelectQueryBuilder } from '@dbTools/typeorm' import { Contribution as DbContribution } from '@entity/Contribution' import { Paginated } from '@arg/Paginated' @@ -11,6 +12,21 @@ interface Relations { [key: string]: boolean | Relations } +function joinRelationsRecursive( + relations: Relations, + queryBuilder: SelectQueryBuilder, + currentPath: string, +): void { + for (const key in relations) { + // console.log('leftJoin: %s, %s', `${currentPath}.${key}`, key) + queryBuilder.leftJoinAndSelect(`${currentPath}.${key}`, key) + if (typeof relations[key] === 'object') { + // If it's a nested relation + joinRelationsRecursive(relations[key] as Relations, queryBuilder, key) + } + } +} + export const findContributions = async ( paginate: Paginated, filter: SearchContributionsFilterArgs, @@ -21,61 +37,33 @@ export const findContributions = async ( if (!connection) { throw new LogError('Cannot connect to db') } - const requiredWhere = { + const queryBuilder = connection.getRepository(DbContribution).createQueryBuilder('Contribution') + if (relations) joinRelationsRecursive(relations, queryBuilder, 'Contribution') + if (withDeleted) queryBuilder.withDeleted() + queryBuilder.where({ ...(filter.statusFilter?.length && { contributionStatus: In(filter.statusFilter) }), ...(filter.userId && { userId: filter.userId }), ...(filter.noHashtag && { memo: Not(Like(`%#%`)) }), - } - const queryBuilder = connection.getRepository(DbContribution).createQueryBuilder('Contribution') - queryBuilder.where(requiredWhere) - return queryBuilder.getManyAndCount() - /* - - - let where = - filter.query && relations?.user - ? [ - { - ...requiredWhere, // And - user: { - firstName: Like(`%${filter.query}%`), - }, - }, // Or - { - ...requiredWhere, - user: { - lastName: Like(`%${filter.query}%`), - }, - }, // Or - { - ...requiredWhere, // And - user: { - emailContact: { - email: Like(`%${filter.query}%`), - }, - }, - }, // Or - { - ...requiredWhere, // And - memo: Like(`%${filter.query}%`), - }, - ] - : requiredWhere - - if (!relations?.user && filter.query) { - where = [{ ...requiredWhere, memo: Like(`%${filter.query}%`) }] - } - - return DbContribution.findAndCount({ - relations, - where, - withDeleted, - order: { - createdAt: paginate.order, - id: paginate.order, - }, - skip: (paginate.currentPage - 1) * paginate.pageSize, - take: paginate.pageSize, }) - */ + queryBuilder.printSql() + if (filter.query) { + const queryString = '%' + filter.query + '%' + queryBuilder.andWhere( + new Brackets((qb) => { + qb.where({ memo: Like(queryString) }) + if (relations?.user) { + qb.orWhere('user.first_name LIKE :firstName', { firstName: queryString }) + .orWhere('user.last_name LIKE :lastName', { lastName: queryString }) + .orWhere('emailContact.email LIKE :emailContact', { emailContact: queryString }) + .orWhere({ memo: Like(queryString) }) + } + }), + ) + } + return queryBuilder + .orderBy('Contribution.createdAt', paginate.order) + .addOrderBy('Contribution.id', paginate.order) + .skip((paginate.currentPage - 1) * paginate.pageSize) + .take(paginate.pageSize) + .getManyAndCount() } diff --git a/backend/src/seeds/creation/index.ts b/backend/src/seeds/creation/index.ts index 3f2a545a4..c22a99b0c 100644 --- a/backend/src/seeds/creation/index.ts +++ b/backend/src/seeds/creation/index.ts @@ -136,6 +136,14 @@ export const creations: CreationInterface[] = [ confirmed: true, moveCreationDate: 12, }, + { + email: 'bibi@bloxberg.de', + amount: 1000, + memo: '#Hexen', + creationDate: nMonthsBefore(new Date()), + confirmed: true, + moveCreationDate: 12, + }, ...bobsTransactions, { email: 'raeuber@hotzenplotz.de',