Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRouyer committed Apr 1, 2024
1 parent 440456e commit 13206ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
6 changes: 3 additions & 3 deletions packages/api/src/services/__tests__/ticket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
90 changes: 38 additions & 52 deletions packages/api/src/services/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,19 +76,27 @@ export default class TicketService extends BaseService {

async list<T extends TicketWith<T>>(
filters: TicketFilters,
config?: FindConfig<T, TicketSort>
config: FindConfig<T, TicketSort> = {
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) {
Expand Down Expand Up @@ -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(
Expand All @@ -541,64 +551,41 @@ export default class TicketService extends BaseService {

private getCursorWhereClause<T extends TicketWith<T>>(
cursor: TicketCursor | undefined,
config?: FindConfig<T, TicketSort>
config: FindConfig<T, TicketSort>
) {
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<T extends TicketWith<T>>(
config?: FindConfig<T, TicketSort>
private getOrderByClause<T extends TicketWith<T>>(
config: FindConfig<T, TicketSort>
) {
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 [
sortBySortDirection(
config.sortBy.statusChangedAt,
config.direction
)(schema.tickets.statusChangedAt),
idOrderByClause,
];
}
if ('createdAt' in config.sortBy) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kyaku/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum Direction {

export type FindConfig<TRelations, TSort> = {
cursor?: string;
limit?: number;
limit: number;
direction: Direction;
relations?: TRelations;
sortBy?: TSort;
Expand Down

0 comments on commit 13206ad

Please sign in to comment.