Skip to content

Commit 0ac6722

Browse files
committed
Extract project settings actions into a service
1 parent 4a0c234 commit 0ac6722

File tree

2 files changed

+224
-147
lines changed

2 files changed

+224
-147
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.settings/route.tsx

Lines changed: 32 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
commitSession,
5555
} from "~/models/message.server";
5656
import { findProjectBySlug } from "~/models/project.server";
57-
import { DeleteProjectService } from "~/services/deleteProject.server";
57+
import { ProjectSettingsService } from "~/services/projectSettings.server";
5858
import { logger } from "~/services/logger.server";
5959
import { requireUserId } from "~/services/session.server";
6060
import {
@@ -77,8 +77,6 @@ import { GitBranchIcon } from "lucide-react";
7777
import { env } from "~/env.server";
7878
import { useEnvironment } from "~/hooks/useEnvironment";
7979
import { DateTime } from "~/components/primitives/DateTime";
80-
import { checkGitHubBranchExists } from "~/services/gitHub.server";
81-
import { tryCatch } from "@trigger.dev/core/utils";
8280
import { TextLink } from "~/components/primitives/TextLink";
8381
import { cn } from "~/utils/cn";
8482

@@ -280,22 +278,12 @@ export const action: ActionFunction = async ({ request, params }) => {
280278
return json(submission);
281279
}
282280

283-
const project = await prisma.project.findFirst({
284-
where: {
285-
slug: projectParam,
286-
organization: {
287-
members: {
288-
some: {
289-
userId,
290-
},
291-
},
292-
},
293-
},
294-
select: {
295-
id: true,
296-
organizationId: true,
297-
},
298-
});
281+
const projectSettingsService = new ProjectSettingsService();
282+
const project = await projectSettingsService.verifyProjectMembership(
283+
projectParam,
284+
organizationSlug,
285+
userId
286+
);
299287

300288
if (!project) {
301289
return json({ errors: { body: "project not found" } }, { status: 404 });
@@ -304,14 +292,7 @@ export const action: ActionFunction = async ({ request, params }) => {
304292
try {
305293
switch (submission.value.action) {
306294
case "rename": {
307-
await prisma.project.update({
308-
where: {
309-
id: project.id,
310-
},
311-
data: {
312-
name: submission.value.projectName,
313-
},
314-
});
295+
await projectSettingsService.renameProject(project.id, submission.value.projectName);
315296

316297
return redirectWithSuccessMessage(
317298
v3ProjectPath({ slug: organizationSlug }, { slug: projectParam }),
@@ -320,9 +301,8 @@ export const action: ActionFunction = async ({ request, params }) => {
320301
);
321302
}
322303
case "delete": {
323-
const deleteProjectService = new DeleteProjectService();
324304
try {
325-
await deleteProjectService.call({ projectSlug: projectParam, userId });
305+
await projectSettingsService.deleteProject(projectParam, userId);
326306

327307
return redirectWithSuccessMessage(
328308
organizationPath({ slug: organizationSlug }),
@@ -341,11 +321,7 @@ export const action: ActionFunction = async ({ request, params }) => {
341321
}
342322
}
343323
case "disconnect-repo": {
344-
await prisma.connectedGithubRepository.delete({
345-
where: {
346-
projectId: project.id,
347-
},
348-
});
324+
await projectSettingsService.disconnectGitHubRepo(project.id);
349325

350326
return redirectBackWithSuccessMessage(
351327
request,
@@ -355,128 +331,37 @@ export const action: ActionFunction = async ({ request, params }) => {
355331
case "update-git-settings": {
356332
const { productionBranch, stagingBranch, previewDeploymentsEnabled } = submission.value;
357333

358-
const existingConnection = await prisma.connectedGithubRepository.findFirst({
359-
where: {
360-
projectId: project.id,
361-
},
362-
include: {
363-
repository: {
364-
include: {
365-
installation: true,
366-
},
367-
},
368-
},
369-
});
370-
371-
if (!existingConnection) {
372-
return redirectBackWithErrorMessage(request, "No connected GitHub repository found");
373-
}
374-
375-
const [owner, repo] = existingConnection.repository.fullName.split("/");
376-
const installationId = Number(existingConnection.repository.installation.appInstallationId);
377-
378-
const existingBranchTracking = BranchTrackingConfigSchema.safeParse(
379-
existingConnection.branchTracking
380-
);
381-
382-
const [error, branchValidationsOrFail] = await tryCatch(
383-
Promise.all([
384-
productionBranch && existingBranchTracking.data?.prod?.branch !== productionBranch
385-
? checkGitHubBranchExists(installationId, owner, repo, productionBranch)
386-
: Promise.resolve(true),
387-
stagingBranch && existingBranchTracking.data?.staging?.branch !== stagingBranch
388-
? checkGitHubBranchExists(installationId, owner, repo, stagingBranch)
389-
: Promise.resolve(true),
390-
])
391-
);
392-
393-
if (error) {
394-
return redirectBackWithErrorMessage(request, "Failed to validate tracking branches");
395-
}
396-
397-
const [productionBranchExists, stagingBranchExists] = branchValidationsOrFail;
398-
399-
if (productionBranch && !productionBranchExists) {
400-
return redirectBackWithErrorMessage(
401-
request,
402-
`Production tracking branch '${productionBranch}' does not exist in the repository`
334+
try {
335+
await projectSettingsService.updateGitSettings(
336+
project.id,
337+
productionBranch,
338+
stagingBranch,
339+
previewDeploymentsEnabled
403340
);
404-
}
405341

406-
if (stagingBranch && !stagingBranchExists) {
407-
return redirectBackWithErrorMessage(
408-
request,
409-
`Staging tracking branch '${stagingBranch}' does not exist in the repository`
410-
);
342+
return redirectBackWithSuccessMessage(request, "Git settings updated successfully");
343+
} catch (error: any) {
344+
return redirectBackWithErrorMessage(request, error.message);
411345
}
412-
413-
await prisma.connectedGithubRepository.update({
414-
where: {
415-
projectId: project.id,
416-
},
417-
data: {
418-
branchTracking: {
419-
prod: productionBranch ? { branch: productionBranch } : {},
420-
staging: stagingBranch ? { branch: stagingBranch } : {},
421-
} satisfies BranchTrackingConfig,
422-
previewDeploymentsEnabled: previewDeploymentsEnabled,
423-
},
424-
});
425-
426-
return redirectBackWithSuccessMessage(request, "Git settings updated successfully");
427346
}
428347
case "connect-repo": {
429348
const { repositoryId, installationId } = submission.value;
430349

431-
const [repository, existingConnection] = await Promise.all([
432-
prisma.githubRepository.findFirst({
433-
where: {
434-
id: repositoryId,
435-
installationId,
436-
installation: {
437-
organizationId: project.organizationId,
438-
},
439-
},
440-
select: {
441-
id: true,
442-
name: true,
443-
defaultBranch: true,
444-
},
445-
}),
446-
prisma.connectedGithubRepository.findFirst({
447-
where: {
448-
projectId: project.id,
449-
},
450-
}),
451-
]);
452-
453-
if (!repository) {
454-
return redirectBackWithErrorMessage(request, "Repository not found");
455-
}
456-
457-
if (existingConnection) {
458-
return redirectBackWithErrorMessage(
459-
request,
460-
"Project is already connected to a repository"
350+
try {
351+
await projectSettingsService.connectGitHubRepo(
352+
project.id,
353+
project.organizationId,
354+
repositoryId,
355+
installationId
461356
);
462-
}
463-
464-
await prisma.connectedGithubRepository.create({
465-
data: {
466-
projectId: project.id,
467-
repositoryId: repositoryId,
468-
branchTracking: {
469-
prod: { branch: repository.defaultBranch },
470-
staging: { branch: repository.defaultBranch },
471-
} satisfies BranchTrackingConfig,
472-
previewDeploymentsEnabled: true,
473-
},
474-
});
475357

476-
return json({
477-
...submission,
478-
success: true,
479-
});
358+
return json({
359+
...submission,
360+
success: true,
361+
});
362+
} catch (error: any) {
363+
return redirectBackWithErrorMessage(request, error.message);
364+
}
480365
}
481366
default: {
482367
return submission.value satisfies never;

0 commit comments

Comments
 (0)