11import { json , type LoaderFunctionArgs } from "@remix-run/server-runtime" ;
22import { type GetProjectEnvResponse } from "@trigger.dev/core/v3" ;
3- import { type RuntimeEnvironment } from "@trigger.dev/database" ;
43import { z } from "zod" ;
5- import { prisma } from "~/db.server" ;
64import { env as processEnv } from "~/env.server" ;
7- import { logger } from "~/services/logger.server" ;
8- import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server" ;
5+ import {
6+ authenticatedEnvironmentForAuthentication ,
7+ authenticateRequest ,
8+ } from "~/services/apiAuth.server" ;
99
1010const ParamsSchema = z . object ( {
1111 projectRef : z . string ( ) ,
@@ -15,14 +15,6 @@ const ParamsSchema = z.object({
1515type ParamsSchema = z . infer < typeof ParamsSchema > ;
1616
1717export async function loader ( { request, params } : LoaderFunctionArgs ) {
18- logger . info ( "projects get env" , { url : request . url } ) ;
19-
20- const authenticationResult = await authenticateApiRequestWithPersonalAccessToken ( request ) ;
21-
22- if ( ! authenticationResult ) {
23- return json ( { error : "Invalid or Missing Access Token" } , { status : 401 } ) ;
24- }
25-
2618 const parsedParams = ParamsSchema . safeParse ( params ) ;
2719
2820 if ( ! parsedParams . success ) {
@@ -31,162 +23,24 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
3123
3224 const { projectRef, env } = parsedParams . data ;
3325
34- const project = await prisma . project . findFirst ( {
35- where : {
36- externalRef : projectRef ,
37- organization : {
38- members : {
39- some : {
40- userId : authenticationResult . userId ,
41- } ,
42- } ,
43- } ,
44- } ,
45- } ) ;
46-
47- if ( ! project ) {
48- return json ( { error : "Project not found" } , { status : 404 } ) ;
49- }
50-
51- const envResult = await getEnvironmentFromEnv ( {
52- projectId : project . id ,
53- userId : authenticationResult . userId ,
54- env,
55- } ) ;
26+ const authenticationResult = await authenticateRequest ( request ) ;
5627
57- if ( ! envResult . success ) {
58- return json ( { error : envResult . error } , { status : 404 } ) ;
28+ if ( ! authenticationResult ) {
29+ return json ( { error : "Invalid or Missing API key" } , { status : 401 } ) ;
5930 }
6031
61- const runtimeEnv = envResult . environment ;
32+ const environment = await authenticatedEnvironmentForAuthentication (
33+ authenticationResult ,
34+ projectRef ,
35+ env
36+ ) ;
6237
6338 const result : GetProjectEnvResponse = {
64- apiKey : runtimeEnv . apiKey ,
65- name : project . name ,
39+ apiKey : environment . apiKey ,
40+ name : environment . project . name ,
6641 apiUrl : processEnv . API_ORIGIN ?? processEnv . APP_ORIGIN ,
67- projectId : project . id ,
42+ projectId : environment . project . id ,
6843 } ;
6944
7045 return json ( result ) ;
7146}
72-
73- export async function getEnvironmentFromEnv ( {
74- projectId,
75- userId,
76- env,
77- branch,
78- } : {
79- projectId : string ;
80- userId : string ;
81- env : ParamsSchema [ "env" ] ;
82- branch ?: string ;
83- } ) : Promise <
84- | {
85- success : true ;
86- environment : RuntimeEnvironment ;
87- }
88- | {
89- success : false ;
90- error : string ;
91- }
92- > {
93- if ( env === "dev" ) {
94- const environment = await prisma . runtimeEnvironment . findFirst ( {
95- where : {
96- projectId,
97- orgMember : {
98- userId : userId ,
99- } ,
100- } ,
101- } ) ;
102-
103- if ( ! environment ) {
104- return {
105- success : false ,
106- error : "Dev environment not found" ,
107- } ;
108- }
109-
110- return {
111- success : true ,
112- environment,
113- } ;
114- }
115-
116- let slug : "stg" | "prod" | "preview" = "prod" ;
117- switch ( env ) {
118- case "staging" :
119- slug = "stg" ;
120- break ;
121- case "prod" :
122- slug = "prod" ;
123- break ;
124- case "preview" :
125- slug = "preview" ;
126- break ;
127- default :
128- break ;
129- }
130-
131- if ( slug === "preview" ) {
132- const previewEnvironment = await prisma . runtimeEnvironment . findFirst ( {
133- where : {
134- projectId,
135- slug : "preview" ,
136- } ,
137- } ) ;
138-
139- if ( ! previewEnvironment ) {
140- return {
141- success : false ,
142- error : "Preview environment not found" ,
143- } ;
144- }
145-
146- // If no branch is provided, just return the parent preview environment
147- if ( ! branch ) {
148- return {
149- success : true ,
150- environment : previewEnvironment ,
151- } ;
152- }
153-
154- const branchEnvironment = await prisma . runtimeEnvironment . findFirst ( {
155- where : {
156- parentEnvironmentId : previewEnvironment . id ,
157- branchName : branch ,
158- } ,
159- } ) ;
160-
161- if ( ! branchEnvironment ) {
162- return {
163- success : false ,
164- error : `Preview branch ${ branch } not found` ,
165- } ;
166- }
167-
168- return {
169- success : true ,
170- environment : branchEnvironment ,
171- } ;
172- }
173-
174- const environment = await prisma . runtimeEnvironment . findFirst ( {
175- where : {
176- projectId,
177- slug,
178- } ,
179- } ) ;
180-
181- if ( ! environment ) {
182- return {
183- success : false ,
184- error : `${ env === "staging" ? "Staging" : "Production" } environment not found` ,
185- } ;
186- }
187-
188- return {
189- success : true ,
190- environment,
191- } ;
192- }
0 commit comments