-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathsetDefaultRegion.server.ts
More file actions
75 lines (66 loc) · 1.98 KB
/
setDefaultRegion.server.ts
File metadata and controls
75 lines (66 loc) · 1.98 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
import { isComputeRegionAccessible, resolveComputeAccess } from "~/v3/regionAccess.server";
import { BaseService, ServiceValidationError } from "./baseService.server";
export class SetDefaultRegionService extends BaseService {
public async call({
projectId,
regionId,
isAdmin = false,
}: {
projectId: string;
regionId: string;
isAdmin?: boolean;
}) {
const workerGroup = await this._prisma.workerInstanceGroup.findFirst({
where: {
id: regionId,
},
});
if (!workerGroup) {
throw new ServiceValidationError("Region not found");
}
const project = await this._prisma.project.findFirst({
where: {
id: projectId,
},
include: {
organization: { select: { featureFlags: true } },
},
});
if (!project) {
throw new ServiceValidationError("Project not found");
}
// If their project is restricted, only allow them to set default regions that are allowed
if (!isAdmin) {
if (project.allowedWorkerQueues.length > 0) {
if (!project.allowedWorkerQueues.includes(workerGroup.masterQueue)) {
throw new ServiceValidationError("You're not allowed to set this region as default");
}
} else {
if (workerGroup.hidden) {
throw new ServiceValidationError("This region is not available to you");
}
if (workerGroup.workloadType === "MICROVM") {
const hasComputeAccess = await resolveComputeAccess(
this._prisma,
project.organization.featureFlags
);
if (!isComputeRegionAccessible(workerGroup, hasComputeAccess)) {
throw new ServiceValidationError("This region requires compute access");
}
}
}
}
await this._prisma.project.update({
where: {
id: projectId,
},
data: {
defaultWorkerGroupId: regionId,
},
});
return {
id: workerGroup.id,
name: workerGroup.name,
};
}
}