diff --git a/packages/api/src/services/__tests__/ticket.test.ts b/packages/api/src/services/__tests__/ticket.test.ts index 97764eaa0..e03cce2de 100644 --- a/packages/api/src/services/__tests__/ticket.test.ts +++ b/packages/api/src/services/__tests__/ticket.test.ts @@ -77,9 +77,9 @@ describe('TicketService', () => { expect(ticketRepo.findMany).toHaveBeenCalledTimes(1); expect(ticketRepo.findMany).toHaveBeenCalledWith({ - limit: undefined, - orderBy: asc(schema.tickets.id), - where: and(and(undefined)), + limit: 51, + orderBy: [asc(schema.tickets.id)], + where: and(undefined), with: { assignedTo: undefined, createdBy: undefined, diff --git a/packages/api/src/services/ticket.ts b/packages/api/src/services/ticket.ts index 26c513738..f966dc4d2 100644 --- a/packages/api/src/services/ticket.ts +++ b/packages/api/src/services/ticket.ts @@ -11,7 +11,7 @@ import { TicketStatusDetail, TicketTimelineEntryType, } from '@cs/kyaku/models'; -import { FindConfig, GetConfig } from '@cs/kyaku/types/query'; +import { Direction, FindConfig, GetConfig } from '@cs/kyaku/types/query'; import { KyakuError } from '@cs/kyaku/utils'; import { @@ -76,19 +76,27 @@ export default class TicketService extends BaseService { async list>( filters: TicketFilters, - config?: FindConfig + config: FindConfig = { + limit: 50, + direction: Direction.Forward, + } ) { const cursor = this.decodeCursor(config?.cursor); - const cursorOrderByClause = this.getCursorOrderByClause(config); - const cursorWhereClause = this.getCursorWhereClause(cursor, config); - const whereClause = this.getWhereClause(filters); - const filteredTickets = await this.ticketRepository.findMany({ - where: and(whereClause, cursorWhereClause), + where: and( + this.getWhereClause(filters), + this.getCursorWhereClause(cursor, config), + cursor + ? filterByDirection(config.direction)(schema.tickets.id, cursor.id) + : undefined + ), with: this.getWithClause(config?.relations), - limit: config?.limit ? config.limit + 1 : undefined, - orderBy: cursorOrderByClause, + limit: config.limit + 1, + orderBy: [ + ...this.getOrderByClause(config), + sortByDirection(config.direction)(schema.tickets.id), + ], }); if (config?.limit) { @@ -517,6 +525,8 @@ export default class TicketService extends BaseService { } private getWhereClause(filters: TicketFilters) { + if (!Object.keys(filters).length) return undefined; + return and( filters.assignedToUser ? inclusionFilterOperator( @@ -541,56 +551,34 @@ export default class TicketService extends BaseService { private getCursorWhereClause>( cursor: TicketCursor | undefined, - config?: FindConfig + config: FindConfig ) { - if (!config) return undefined; - - const idWhereClause = cursor - ? filterByDirection(config.direction)(schema.tickets.id, cursor.id) - : undefined; - - if (!config?.sortBy) { - return idWhereClause; - } + if (!config.sortBy) return undefined; if ('statusChangedAt' in config.sortBy) { - return and( - cursor?.statusChangedAt - ? filterBySortDirection( - config.sortBy.statusChangedAt, - config.direction - )(schema.tickets.statusChangedAt, new Date(cursor?.statusChangedAt)) - : undefined, - idWhereClause - ); + return cursor?.statusChangedAt + ? filterBySortDirection( + config.sortBy.statusChangedAt, + config.direction + )(schema.tickets.statusChangedAt, new Date(cursor?.statusChangedAt)) + : undefined; } if ('createdAt' in config.sortBy) { - return and( - cursor?.createdAt - ? filterBySortDirection(config.sortBy.createdAt, config.direction)( - schema.tickets.createdAt, - new Date(cursor.createdAt) - ) - : undefined, - idWhereClause - ); + return cursor?.createdAt + ? filterBySortDirection(config.sortBy.createdAt, config.direction)( + schema.tickets.createdAt, + new Date(cursor.createdAt) + ) + : undefined; } - return idWhereClause; + return undefined; } - private getCursorOrderByClause>( - config?: FindConfig + private getOrderByClause>( + config: FindConfig ) { - if (!config) return asc(schema.tickets.id); - - const idOrderByClause = sortByDirection(config.direction)( - schema.tickets.id - ); - - if (!config?.sortBy) { - return idOrderByClause; - } + if (!config.sortBy) return []; if ('statusChangedAt' in config.sortBy) { return [ @@ -598,7 +586,6 @@ export default class TicketService extends BaseService { config.sortBy.statusChangedAt, config.direction )(schema.tickets.statusChangedAt), - idOrderByClause, ]; } if ('createdAt' in config.sortBy) { @@ -607,11 +594,10 @@ export default class TicketService extends BaseService { config.sortBy.createdAt, config.direction )(schema.tickets.createdAt), - idOrderByClause, ]; } - return idOrderByClause; + return []; } private decodeCursor(cursor?: string) { diff --git a/packages/kyaku/types/query.ts b/packages/kyaku/types/query.ts index d9bd3fa00..d43652cad 100644 --- a/packages/kyaku/types/query.ts +++ b/packages/kyaku/types/query.ts @@ -8,7 +8,7 @@ export enum Direction { export type FindConfig = { cursor?: string; - limit?: number; + limit: number; direction: Direction; relations?: TRelations; sortBy?: TSort;