-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathorganization.server.ts
More file actions
148 lines (134 loc) · 3.72 KB
/
Copy pathorganization.server.ts
File metadata and controls
148 lines (134 loc) · 3.72 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
import type {
Organization,
OrgMember,
Prisma,
Project,
RuntimeEnvironment,
User,
} from "@trigger.dev/database";
import { customAlphabet } from "nanoid";
import { generate } from "random-words";
import slug from "slug";
import { prisma, type PrismaClientOrTransaction } from "~/db.server";
import { env } from "~/env.server";
import { featuresForUrl } from "~/features.server";
import { createApiKeyForEnv, createPkApiKeyForEnv, envSlug } from "./api-key.server";
import { getDefaultEnvironmentConcurrencyLimit } from "~/services/platform.v3.server";
import { enqueueAttioWorkspaceSync } from "~/services/attio.server";
export type { Organization };
const nanoid = customAlphabet("1234567890abcdef", 4);
export async function createOrganization(
{
title,
userId,
companySize,
onboardingData,
avatar,
}: Pick<Organization, "title" | "companySize"> & {
userId: User["id"];
onboardingData?: Prisma.InputJsonValue;
avatar?: Prisma.InputJsonValue;
},
attemptCount = 0
): Promise<Organization> {
if (typeof process.env.BLOCKED_USERS === "string" && process.env.BLOCKED_USERS.includes(userId)) {
throw new Error("Organization could not be created.");
}
const uniqueOrgSlug = `${slug(title)}-${nanoid(4)}`;
const orgWithSameSlug = await prisma.organization.findFirst({
where: { slug: uniqueOrgSlug },
});
if (attemptCount > 100) {
throw new Error(`Unable to create organization with slug ${uniqueOrgSlug} after 100 attempts`);
}
if (orgWithSameSlug) {
return createOrganization(
{
title,
userId,
companySize,
onboardingData,
avatar,
},
attemptCount + 1
);
}
const features = featuresForUrl(new URL(env.APP_ORIGIN));
const organization = await prisma.organization.create({
data: {
title,
slug: uniqueOrgSlug,
companySize,
onboardingData: onboardingData ?? undefined,
avatar: avatar ?? undefined,
maximumConcurrencyLimit: env.DEFAULT_ORG_EXECUTION_CONCURRENCY_LIMIT,
members: {
create: {
userId: userId,
role: "ADMIN",
},
},
v3Enabled: true,
},
include: {
members: true,
},
});
// Fire-and-forget; never blocks org creation.
void enqueueAttioWorkspaceSync({
orgId: organization.id,
title: organization.title,
slug: organization.slug,
companySize: organization.companySize,
createdAt: organization.createdAt,
adminUserId: userId,
});
return { ...organization };
}
export async function createEnvironment({
organization,
project,
type,
isBranchableEnvironment = false,
member,
prismaClient = prisma,
}: {
organization: Pick<Organization, "id" | "maximumConcurrencyLimit">;
project: Pick<Project, "id">;
type: RuntimeEnvironment["type"];
isBranchableEnvironment?: boolean;
member?: OrgMember;
prismaClient?: PrismaClientOrTransaction;
}) {
const slug = envSlug(type);
const apiKey = createApiKeyForEnv(type);
const pkApiKey = createPkApiKeyForEnv(type);
const shortcode = createShortcode().join("-");
const limit = await getDefaultEnvironmentConcurrencyLimit(organization.id, type);
return await prismaClient.runtimeEnvironment.create({
data: {
slug,
apiKey,
pkApiKey,
shortcode,
autoEnableInternalSources: type !== "DEVELOPMENT",
maximumConcurrencyLimit: limit,
organization: {
connect: {
id: organization.id,
},
},
project: {
connect: {
id: project.id,
},
},
orgMember: member ? { connect: { id: member.id } } : undefined,
type,
isBranchableEnvironment,
},
});
}
function createShortcode() {
return generate({ exactly: 2 });
}