-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathapi.v1.projects.$projectRef.environments.ts
More file actions
71 lines (65 loc) · 2.45 KB
/
Copy pathapi.v1.projects.$projectRef.environments.ts
File metadata and controls
71 lines (65 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { json } from "@remix-run/server-runtime";
import { type GetProjectEnvironmentsResponseBody } from "@trigger.dev/core/v3";
import { z } from "zod";
import { $replica } from "~/db.server";
import { findProjectByRef } from "~/models/project.server";
import { createLoaderPATApiRoute } from "~/services/routeBuilders/apiBuilder.server";
import { sortEnvironments } from "~/utils/environmentSort";
const ParamsSchema = z.object({
projectRef: z.string(),
});
export const loader = createLoaderPATApiRoute(
{
params: ParamsSchema,
corsStrategy: "all",
// Resolve projectRef → org so the PAT plugin can ground its role-floor
// calculation. Membership is enforced by the plugin (`authenticatePat`
// rejects users who aren't members of the target org) and again by
// `findProjectByRef` below.
context: async (params) => {
const project = await $replica.project.findFirst({
where: { externalRef: params.projectRef },
select: { organizationId: true },
});
return project ? { organizationId: project.organizationId } : {};
},
authorization: { action: "read", resource: () => ({ type: "environments" }) },
},
async ({ params, authentication }) => {
const project = await findProjectByRef(params.projectRef, authentication.userId);
if (!project) {
return json({ error: "Project not found" }, { status: 404 });
}
const environments = await $replica.runtimeEnvironment.findMany({
where: {
projectId: project.id,
// Only base/parent environments. Branch children (preview branches)
// are excluded — syncs target the parent and branches override elsewhere.
parentEnvironmentId: null,
archivedAt: null,
OR: [
{ type: { in: ["STAGING", "PRODUCTION", "PREVIEW"] } },
// dev is per-user: only return the caller's own dev environment
{ type: "DEVELOPMENT", orgMember: { userId: authentication.userId } },
],
},
select: {
id: true,
slug: true,
type: true,
isBranchableEnvironment: true,
branchName: true,
paused: true,
},
});
const result: GetProjectEnvironmentsResponseBody = sortEnvironments(environments).map((env) => ({
id: env.id,
slug: env.slug,
type: env.type,
isBranchableEnvironment: env.isBranchableEnvironment,
branchName: env.branchName,
paused: env.paused,
}));
return json(result);
}
);