-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathproject.server.ts
More file actions
159 lines (141 loc) · 3.93 KB
/
project.server.ts
File metadata and controls
159 lines (141 loc) · 3.93 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { nanoid, customAlphabet } from "nanoid";
import slug from "slug";
import { $replica, prisma } from "~/db.server";
import type { Project } from "@trigger.dev/database";
import { Organization, createEnvironment } from "./organization.server";
import { env } from "~/env.server";
import { projectCreated } from "~/services/platform.v3.server";
export type { Project } from "@trigger.dev/database";
const externalRefGenerator = customAlphabet("abcdefghijklmnopqrstuvwxyz", 20);
type Options = {
organizationSlug: string;
name: string;
userId: string;
version: "v2" | "v3";
};
export class ExceededProjectLimitError extends Error {
constructor(message: string) {
super(message);
this.name = "ExceededProjectLimitError";
}
}
export async function createProject(
{ organizationSlug, name, userId, version }: Options,
attemptCount = 0
): Promise<Project & { organization: Organization }> {
//check the user has permissions to do this
const organization = await prisma.organization.findFirst({
select: {
id: true,
slug: true,
v3Enabled: true,
maximumConcurrencyLimit: true,
maximumProjectCount: true,
},
where: {
slug: organizationSlug,
members: { some: { userId } },
},
});
if (!organization) {
throw new Error(
`User ${userId} does not have permission to create a project in organization ${organizationSlug}`
);
}
if (version === "v3") {
if (!organization.v3Enabled) {
throw new Error(`Organization can't create v3 projects.`);
}
}
const projectCount = await prisma.project.count({
where: {
organizationId: organization.id,
deletedAt: null,
},
});
if (projectCount >= organization.maximumProjectCount) {
throw new ExceededProjectLimitError(
`This organization has reached the maximum number of projects (${organization.maximumProjectCount}).`
);
}
//ensure the slug is globally unique
const uniqueProjectSlug = `${slug(name)}-${nanoid(4)}`;
const projectWithSameSlug = await prisma.project.findFirst({
where: { slug: uniqueProjectSlug },
});
if (attemptCount > 100) {
throw new Error(`Unable to create project with slug ${uniqueProjectSlug} after 100 attempts`);
}
if (projectWithSameSlug) {
return createProject(
{
organizationSlug,
name,
userId,
version,
},
attemptCount + 1
);
}
const project = await prisma.project.create({
data: {
name,
slug: uniqueProjectSlug,
organization: {
connect: {
slug: organizationSlug,
},
},
externalRef: `proj_${externalRefGenerator()}`,
version: version === "v3" ? "V3" : "V2",
},
include: {
organization: {
include: {
members: true,
},
},
},
});
// Create the dev and prod environments
await createEnvironment({
organization,
project,
type: "PRODUCTION",
isBranchableEnvironment: false,
});
for (const member of project.organization.members) {
await createEnvironment({
organization,
project,
type: "DEVELOPMENT",
isBranchableEnvironment: false,
member,
});
}
await projectCreated(organization, project);
return project;
}
export async function findProjectBySlug(orgSlug: string, projectSlug: string, userId: string) {
// Find the project scoped to the organization, making sure the user belongs to that org
return await $replica.project.findFirst({
where: {
slug: projectSlug,
organization: {
slug: orgSlug,
members: { some: { userId } },
},
},
});
}
export async function findProjectByRef(externalRef: string, userId: string) {
// Find the project scoped to the organization, making sure the user belongs to that org
return await $replica.project.findFirst({
where: {
externalRef,
organization: {
members: { some: { userId } },
},
},
});
}