|
1 | 1 | import { ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
2 | 2 | import { |
| 3 | + ApiDeploymentListSearchParams, |
3 | 4 | InitializeDeploymentRequestBody, |
4 | 5 | InitializeDeploymentResponseBody, |
5 | 6 | } from "@trigger.dev/core/v3"; |
| 7 | +import { $replica } from "~/db.server"; |
6 | 8 | import { authenticateApiRequest } from "~/services/apiAuth.server"; |
7 | 9 | import { logger } from "~/services/logger.server"; |
| 10 | +import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server"; |
8 | 11 | import { ServiceValidationError } from "~/v3/services/baseService.server"; |
9 | 12 | import { InitializeDeploymentService } from "~/v3/services/initializeDeployment.server"; |
10 | 13 |
|
@@ -60,3 +63,117 @@ export async function action({ request, params }: ActionFunctionArgs) { |
60 | 63 | } |
61 | 64 | } |
62 | 65 | } |
| 66 | + |
| 67 | +export const loader = createLoaderApiRoute( |
| 68 | + { |
| 69 | + searchParams: ApiDeploymentListSearchParams, |
| 70 | + allowJWT: true, |
| 71 | + corsStrategy: "none", |
| 72 | + authorization: { |
| 73 | + action: "read", |
| 74 | + resource: () => ({ deployments: "list" }), |
| 75 | + superScopes: ["read:deployments", "read:all", "admin"], |
| 76 | + }, |
| 77 | + findResource: async () => 1, // This is a dummy function, we don't need to find a resource |
| 78 | + }, |
| 79 | + async ({ searchParams, authentication }) => { |
| 80 | + const limit = Math.max(Math.min(searchParams["page[size]"] ?? 20, 100), 5); |
| 81 | + |
| 82 | + const afterDeployment = searchParams["page[after]"] |
| 83 | + ? await $replica.workerDeployment.findFirst({ |
| 84 | + where: { |
| 85 | + friendlyId: searchParams["page[after]"], |
| 86 | + environmentId: authentication.environment.id, |
| 87 | + }, |
| 88 | + }) |
| 89 | + : undefined; |
| 90 | + |
| 91 | + const deployments = await $replica.workerDeployment.findMany({ |
| 92 | + where: { |
| 93 | + environmentId: authentication.environment.id, |
| 94 | + ...(afterDeployment ? { id: { lt: afterDeployment.id } } : {}), |
| 95 | + ...getCreatedAtFilter(searchParams), |
| 96 | + ...(searchParams.status ? { status: searchParams.status } : {}), |
| 97 | + }, |
| 98 | + orderBy: { |
| 99 | + id: "desc", |
| 100 | + }, |
| 101 | + take: limit + 1, |
| 102 | + }); |
| 103 | + |
| 104 | + const hasMore = deployments.length > limit; |
| 105 | + const nextCursor = hasMore ? deployments[limit - 1].friendlyId : undefined; |
| 106 | + const data = hasMore ? deployments.slice(0, limit) : deployments; |
| 107 | + |
| 108 | + return json({ |
| 109 | + data: data.map((deployment) => ({ |
| 110 | + id: deployment.friendlyId, |
| 111 | + createdAt: deployment.createdAt, |
| 112 | + shortCode: deployment.shortCode, |
| 113 | + version: deployment.version.toString(), |
| 114 | + runtime: deployment.runtime, |
| 115 | + runtimeVersion: deployment.runtimeVersion, |
| 116 | + status: deployment.status, |
| 117 | + deployedAt: deployment.deployedAt, |
| 118 | + git: deployment.git, |
| 119 | + error: deployment.errorData ?? undefined, |
| 120 | + })), |
| 121 | + pagination: { |
| 122 | + next: nextCursor, |
| 123 | + }, |
| 124 | + }); |
| 125 | + } |
| 126 | +); |
| 127 | + |
| 128 | +import parseDuration from "parse-duration"; |
| 129 | + |
| 130 | +function getCreatedAtFilter(searchParams: ApiDeploymentListSearchParams) { |
| 131 | + if (searchParams.period) { |
| 132 | + const duration = parseDuration(searchParams.period, "ms"); |
| 133 | + |
| 134 | + if (!duration) { |
| 135 | + throw new ServiceValidationError( |
| 136 | + `Invalid search query parameter: period=${searchParams.period}`, |
| 137 | + 400 |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + return { |
| 142 | + createdAt: { |
| 143 | + gte: new Date(Date.now() - duration), |
| 144 | + lte: new Date(), |
| 145 | + }, |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + if (searchParams.from && searchParams.to) { |
| 150 | + const fromDate = safeDateFromString(searchParams.from, "from"); |
| 151 | + const toDate = safeDateFromString(searchParams.to, "to"); |
| 152 | + |
| 153 | + return { |
| 154 | + createdAt: { |
| 155 | + gte: fromDate, |
| 156 | + lte: toDate, |
| 157 | + }, |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + if (searchParams.from) { |
| 162 | + const fromDate = safeDateFromString(searchParams.from, "from"); |
| 163 | + return { |
| 164 | + createdAt: { |
| 165 | + gte: fromDate, |
| 166 | + }, |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + return {}; |
| 171 | +} |
| 172 | + |
| 173 | +function safeDateFromString(value: string, paramName: string) { |
| 174 | + const date = new Date(value); |
| 175 | + if (isNaN(date.getTime())) { |
| 176 | + throw new ServiceValidationError(`Invalid search query parameter: ${paramName}=${value}`, 400); |
| 177 | + } |
| 178 | + return date; |
| 179 | +} |
0 commit comments