|
| 1 | +import { SQL, and, asc, desc, eq, ilike, inArray } from "drizzle-orm"; |
| 2 | + |
| 3 | +import { ORM_TYPE } from "~/datasources/db"; |
| 4 | +import { scheduleSchema, ScheduleStatus } from "~/datasources/db/schedule"; |
| 5 | +import { |
| 6 | + PaginationOptionsType, |
| 7 | + paginationDBHelper, |
| 8 | +} from "~/datasources/helpers/paginationQuery"; |
| 9 | +import { SortableSchemaFields } from "~/datasources/helpers/sorting"; |
| 10 | +import { sanitizeForLikeSearch } from "~/schema/shared/helpers"; |
| 11 | + |
| 12 | +export type SchedulesSearch = { |
| 13 | + scheduleIds?: string[]; |
| 14 | + eventIds?: string[]; |
| 15 | + title?: string; |
| 16 | + description?: string; |
| 17 | + satus?: ScheduleStatus; |
| 18 | +}; |
| 19 | + |
| 20 | +type SortableFields = |
| 21 | + | "description" |
| 22 | + | "title" |
| 23 | + | "startTimestamp" |
| 24 | + | "endTimestamp"; |
| 25 | +type EventFetcherSort = SortableSchemaFields<SortableFields>; |
| 26 | + |
| 27 | +const getSearchSchedulesQuery = ( |
| 28 | + DB: ORM_TYPE, |
| 29 | + search: SchedulesSearch = {}, |
| 30 | + sort: EventFetcherSort, |
| 31 | +) => { |
| 32 | + const { scheduleIds, title, description, eventIds, satus } = search; |
| 33 | + |
| 34 | + const wheres: SQL[] = []; |
| 35 | + const query = DB.select().from(scheduleSchema); |
| 36 | + |
| 37 | + if (scheduleIds && scheduleIds.length > 0) { |
| 38 | + wheres.push(inArray(scheduleSchema.id, scheduleIds)); |
| 39 | + } |
| 40 | + |
| 41 | + if (eventIds && eventIds.length > 0) { |
| 42 | + wheres.push(inArray(scheduleSchema.eventId, eventIds)); |
| 43 | + } |
| 44 | + |
| 45 | + if (title) { |
| 46 | + wheres.push(ilike(scheduleSchema.title, sanitizeForLikeSearch(title))); |
| 47 | + } |
| 48 | + |
| 49 | + if (description) { |
| 50 | + wheres.push( |
| 51 | + ilike(scheduleSchema.description, sanitizeForLikeSearch(description)), |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if (satus) { |
| 56 | + wheres.push(eq(scheduleSchema.status, satus)); |
| 57 | + } |
| 58 | + |
| 59 | + const orderBy: SQL<unknown>[] = []; |
| 60 | + |
| 61 | + if (sort) { |
| 62 | + const sorts = sort.map(([field, direction]) => { |
| 63 | + const sortDirection = direction === "asc" ? asc : desc; |
| 64 | + |
| 65 | + return sortDirection(scheduleSchema[field]); |
| 66 | + }); |
| 67 | + |
| 68 | + orderBy.push(...sorts); |
| 69 | + } |
| 70 | + |
| 71 | + return query.where(and(...wheres)).orderBy(...orderBy); |
| 72 | +}; |
| 73 | + |
| 74 | +const searchSchedules = async ({ |
| 75 | + DB, |
| 76 | + search = {}, |
| 77 | + sort, |
| 78 | +}: { |
| 79 | + DB: ORM_TYPE; |
| 80 | + search: SchedulesSearch; |
| 81 | + sort?: EventFetcherSort; |
| 82 | +}) => { |
| 83 | + const schedules = await getSearchSchedulesQuery(DB, search, sort).execute(); |
| 84 | + |
| 85 | + return schedules; |
| 86 | +}; |
| 87 | + |
| 88 | +const searchPaginatedSchedules = async ({ |
| 89 | + DB, |
| 90 | + pagination, |
| 91 | + search = {}, |
| 92 | + sort, |
| 93 | +}: { |
| 94 | + DB: ORM_TYPE; |
| 95 | + search: SchedulesSearch; |
| 96 | + pagination: PaginationOptionsType; |
| 97 | + sort?: EventFetcherSort; |
| 98 | +}) => { |
| 99 | + const query = getSearchSchedulesQuery(DB, search, sort); |
| 100 | + |
| 101 | + const results = await paginationDBHelper(DB, query, pagination); |
| 102 | + |
| 103 | + return results; |
| 104 | +}; |
| 105 | + |
| 106 | +export const schedulesFetcher = { |
| 107 | + searchSchedules, |
| 108 | + searchPaginatedSchedules, |
| 109 | +}; |
0 commit comments