From b675c1adfb662ae085d37fcfdd0c4964db571c7f Mon Sep 17 00:00:00 2001 From: Andres Date: Mon, 7 Aug 2023 16:51:31 +0200 Subject: [PATCH] fix orderby --- src/infrastructure/common/helpers/get-order-by-object.ts | 5 ++++- src/modules/cards/infrastructure/cards.repository.ts | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/infrastructure/common/helpers/get-order-by-object.ts b/src/infrastructure/common/helpers/get-order-by-object.ts index acc87c7..b83e2d0 100644 --- a/src/infrastructure/common/helpers/get-order-by-object.ts +++ b/src/infrastructure/common/helpers/get-order-by-object.ts @@ -1,7 +1,10 @@ type OrderByDirection = 'asc' | 'desc' export function createPrismaOrderBy(input: string | null) { - const { key, direction, relation, field } = getOrderByObject(input) + const orderByObject = getOrderByObject(input) + + if (!orderByObject) return null + const { key, direction, relation, field } = orderByObject if (relation && field) { return { diff --git a/src/modules/cards/infrastructure/cards.repository.ts b/src/modules/cards/infrastructure/cards.repository.ts index cda2f4d..6f78c14 100644 --- a/src/modules/cards/infrastructure/cards.repository.ts +++ b/src/modules/cards/infrastructure/cards.repository.ts @@ -62,7 +62,7 @@ export class CardsRepository { question = undefined, currentPage, itemsPerPage, - orderBy, + orderBy = 'updated-desc', }: GetAllCardsInDeckDto ): Promise { try { @@ -78,7 +78,7 @@ export class CardsRepository { }, } - const { key, direction } = getOrderByObject(orderBy) + const { key, direction } = getOrderByObject(orderBy) || {} if (key === 'grade') { const start = (currentPage - 1) * itemsPerPage @@ -122,7 +122,7 @@ export class CardsRepository { const result = await this.prisma.$transaction([ this.prisma.card.count({ where }), this.prisma.card.findMany({ - orderBy: createPrismaOrderBy(orderBy) || { updated: 'desc' }, + orderBy: createPrismaOrderBy(orderBy), where, include: { grades: {