|
| 1 | +import { |
| 2 | + type BulkActionGroup, |
| 3 | + type BulkActionStatus, |
| 4 | + type BulkActionType, |
| 5 | +} from "@trigger.dev/database"; |
| 6 | +import { z } from "zod"; |
| 7 | +import { BasePresenter } from "./basePresenter.server"; |
| 8 | + |
| 9 | +const DEFAULT_PAGE_SIZE = 25; |
| 10 | +const MAX_PAGE_SIZE = 100; |
| 11 | + |
| 12 | +export const ApiBulkActionListSearchParams = z.object({ |
| 13 | + "page[size]": z.coerce.number().int().positive().min(1).max(MAX_PAGE_SIZE).optional(), |
| 14 | + "page[after]": z.string().optional(), |
| 15 | + "page[before]": z.string().optional(), |
| 16 | +}); |
| 17 | + |
| 18 | +export type ApiBulkActionListSearchParams = z.infer<typeof ApiBulkActionListSearchParams>; |
| 19 | + |
| 20 | +type BulkActionListCursor = { |
| 21 | + createdAt: Date; |
| 22 | + id: string; |
| 23 | +}; |
| 24 | + |
| 25 | +type BulkActionRow = Pick< |
| 26 | + BulkActionGroup, |
| 27 | + | "id" |
| 28 | + | "friendlyId" |
| 29 | + | "name" |
| 30 | + | "status" |
| 31 | + | "type" |
| 32 | + | "createdAt" |
| 33 | + | "completedAt" |
| 34 | + | "totalCount" |
| 35 | + | "successCount" |
| 36 | + | "failureCount" |
| 37 | +>; |
| 38 | + |
| 39 | +export class ApiBulkActionPresenter extends BasePresenter { |
| 40 | + public async list(environmentId: string, searchParams: ApiBulkActionListSearchParams) { |
| 41 | + const pageSize = searchParams["page[size]"] ?? DEFAULT_PAGE_SIZE; |
| 42 | + const after = searchParams["page[after]"]; |
| 43 | + const before = searchParams["page[before]"]; |
| 44 | + |
| 45 | + if (after && before) { |
| 46 | + throw new Error("Only one of page[after] or page[before] can be provided"); |
| 47 | + } |
| 48 | + |
| 49 | + const cursor = decodeCursor(after ?? before); |
| 50 | + const direction = before ? "backward" : "forward"; |
| 51 | + |
| 52 | + const where = { |
| 53 | + environmentId, |
| 54 | + ...(cursor |
| 55 | + ? direction === "forward" |
| 56 | + ? { |
| 57 | + OR: [ |
| 58 | + { createdAt: { lt: cursor.createdAt } }, |
| 59 | + { createdAt: cursor.createdAt, id: { lt: cursor.id } }, |
| 60 | + ], |
| 61 | + } |
| 62 | + : { |
| 63 | + OR: [ |
| 64 | + { createdAt: { gt: cursor.createdAt } }, |
| 65 | + { createdAt: cursor.createdAt, id: { gt: cursor.id } }, |
| 66 | + ], |
| 67 | + } |
| 68 | + : {}), |
| 69 | + }; |
| 70 | + |
| 71 | + const rows = await this._replica.bulkActionGroup.findMany({ |
| 72 | + select: bulkActionSelect, |
| 73 | + where, |
| 74 | + orderBy: |
| 75 | + direction === "forward" |
| 76 | + ? [{ createdAt: "desc" }, { id: "desc" }] |
| 77 | + : [{ createdAt: "asc" }, { id: "asc" }], |
| 78 | + take: pageSize + 1, |
| 79 | + }); |
| 80 | + |
| 81 | + const hasMore = rows.length > pageSize; |
| 82 | + const pageRows = rows.slice(0, pageSize); |
| 83 | + const dataRows = direction === "forward" ? pageRows : [...pageRows].reverse(); |
| 84 | + |
| 85 | + const first = dataRows.at(0); |
| 86 | + const last = dataRows.at(-1); |
| 87 | + |
| 88 | + return { |
| 89 | + data: dataRows.map(apiBulkActionObject), |
| 90 | + pagination: { |
| 91 | + next: last && (hasMore || direction === "backward") ? encodeCursor(last) : undefined, |
| 92 | + previous: |
| 93 | + first && |
| 94 | + ((direction === "forward" && Boolean(after)) || (direction === "backward" && hasMore)) |
| 95 | + ? encodeCursor(first) |
| 96 | + : undefined, |
| 97 | + }, |
| 98 | + }; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export const bulkActionSelect = { |
| 103 | + id: true, |
| 104 | + friendlyId: true, |
| 105 | + name: true, |
| 106 | + status: true, |
| 107 | + type: true, |
| 108 | + createdAt: true, |
| 109 | + completedAt: true, |
| 110 | + totalCount: true, |
| 111 | + successCount: true, |
| 112 | + failureCount: true, |
| 113 | +} as const; |
| 114 | + |
| 115 | +export function apiBulkActionObject(row: BulkActionRow) { |
| 116 | + return { |
| 117 | + id: row.friendlyId, |
| 118 | + name: row.name ?? undefined, |
| 119 | + type: row.type as BulkActionType, |
| 120 | + status: row.status as BulkActionStatus, |
| 121 | + counts: { |
| 122 | + total: row.totalCount, |
| 123 | + success: row.successCount, |
| 124 | + failure: row.failureCount, |
| 125 | + }, |
| 126 | + createdAt: row.createdAt, |
| 127 | + completedAt: row.completedAt ?? undefined, |
| 128 | + }; |
| 129 | +} |
| 130 | + |
| 131 | +function encodeCursor(row: Pick<BulkActionRow, "createdAt" | "id">) { |
| 132 | + return Buffer.from(JSON.stringify({ createdAt: row.createdAt.getTime(), id: row.id })).toString( |
| 133 | + "base64url" |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +function decodeCursor(cursor: string | undefined): BulkActionListCursor | undefined { |
| 138 | + if (!cursor) { |
| 139 | + return undefined; |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + const parsed = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8")) as { |
| 144 | + createdAt?: unknown; |
| 145 | + id?: unknown; |
| 146 | + }; |
| 147 | + if (typeof parsed.createdAt !== "number" || typeof parsed.id !== "string") { |
| 148 | + throw new Error("Invalid cursor"); |
| 149 | + } |
| 150 | + |
| 151 | + return { createdAt: new Date(parsed.createdAt), id: parsed.id }; |
| 152 | + } catch { |
| 153 | + throw new Error("Invalid cursor"); |
| 154 | + } |
| 155 | +} |
0 commit comments