Skip to content

Commit 6f5ba81

Browse files
Move API handler implementations to service directory (#6)
1 parent 483646a commit 6f5ba81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3856
-3135
lines changed

backend/src/db/models.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
DeploymentSource,
33
DeploymentStatus,
4+
GitHubOAuthAction,
45
ImageBuilder,
56
PermissionLevel,
67
WebhookEvent,
@@ -157,3 +158,11 @@ export interface RepoImportState {
157158
userId: number;
158159
orgId: number;
159160
}
161+
162+
export interface GitHubOAuthState {
163+
id: number;
164+
random: string;
165+
userId: number;
166+
orgId: number;
167+
action: GitHubOAuthAction;
168+
}

backend/src/db/repo/appGroup.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { PrismaClientType } from "../index.ts";
1+
import { PrismaClientKnownRequestError } from "../../generated/prisma/internal/prismaNamespace.ts";
2+
import { ConflictError, type PrismaClientType } from "../index.ts";
23
import type { AppGroup } from "../models.ts";
34

45
export class AppGroupRepo {
@@ -9,16 +10,24 @@ export class AppGroupRepo {
910
}
1011

1112
async create(orgId: number, name: string, isMono: boolean) {
12-
const group = await this.client.appGroup.create({
13-
data: {
14-
orgId: orgId,
15-
name: name,
16-
isMono: isMono,
17-
},
18-
select: { id: true },
19-
});
13+
try {
14+
const group = await this.client.appGroup.create({
15+
data: {
16+
orgId: orgId,
17+
name: name,
18+
isMono: isMono,
19+
},
20+
select: { id: true },
21+
});
2022

21-
return group.id;
23+
return group.id;
24+
} catch (e) {
25+
if (e instanceof PrismaClientKnownRequestError && e.code === "P2002") {
26+
// P2002 is "Unique Constraint Failed" - https://www.prisma.io/docs/orm/reference/error-reference#p2002
27+
throw new ConflictError("name", e);
28+
}
29+
throw e;
30+
}
2231
}
2332

2433
async getById(appGroupId: number): Promise<AppGroup> {

backend/src/db/repo/invitation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ export class InvitationRepo {
9191
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
9292
// https://www.prisma.io/docs/orm/reference/error-reference#p2025
9393
// "An operation failed because it depends on one or more records that were required but not found."
94-
throw new NotFoundError("organization");
94+
throw new NotFoundError("organization", e);
9595
}
9696
if (e instanceof PrismaClientKnownRequestError && e.code === "P2002") {
9797
// Unique constraint failed
98-
throw new ConflictError("user");
98+
throw new ConflictError("user", e);
9999
}
100100
throw e;
101101
}

backend/src/handlers/acceptInvitation.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { db, NotFoundError } from "../db/index.ts";
1+
import { acceptInvitation } from "../service/acceptInvitation.ts";
2+
import { InvitationNotFoundError } from "../service/common/errors.ts";
23
import { json, type HandlerMap } from "../types.ts";
34
import type { AuthenticatedRequest } from "./index.ts";
45

5-
export const acceptInvitation: HandlerMap["acceptInvitation"] = async (
6+
export const acceptInvitationHandler: HandlerMap["acceptInvitation"] = async (
67
ctx,
78
req: AuthenticatedRequest,
89
res,
910
) => {
1011
try {
11-
await db.invitation.accept(
12+
await acceptInvitation(
1213
ctx.request.params.invId,
1314
ctx.request.params.orgId,
1415
req.user.id,
1516
);
1617
} catch (e: any) {
17-
if (e instanceof NotFoundError) {
18+
if (e instanceof InvitationNotFoundError) {
1819
return json(404, res, { code: 404, message: "Invitation not found." });
1920
}
2021
throw e;

backend/src/handlers/claimOrg.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { db, NotFoundError } from "../db/index.ts";
1+
import { InstallationNotFoundError } from "../lib/octokit.ts";
2+
import { claimOrg } from "../service/claimOrg.ts";
3+
import { OrgNotFoundError } from "../service/common/errors.ts";
24
import { json, type HandlerMap } from "../types.ts";
35
import type { AuthenticatedRequest } from "./index.ts";
46

5-
export const claimOrg: HandlerMap["claimOrg"] = async (
7+
export const claimOrgHandler: HandlerMap["claimOrg"] = async (
68
ctx,
79
req: AuthenticatedRequest,
810
res,
@@ -11,27 +13,19 @@ export const claimOrg: HandlerMap["claimOrg"] = async (
1113
ctx.request.requestBody.unclaimedInstallationId;
1214
const orgId = ctx.request.params.orgId;
1315
try {
14-
await db.org.claimInstallation(
15-
orgId,
16-
unassignedInstallationId,
17-
req.user.id,
18-
);
16+
await claimOrg(orgId, unassignedInstallationId, req.user.id);
1917
} catch (e) {
20-
if (e instanceof NotFoundError) {
21-
switch (e.message) {
22-
case "installation":
23-
return json(404, res, {
24-
code: 404,
25-
message: "Installation does not exist.",
26-
});
27-
case "organization":
28-
return json(404, res, {
29-
code: 404,
30-
message: "Organization does not exist.",
31-
});
32-
}
18+
if (e instanceof InstallationNotFoundError) {
19+
return json(404, res, {
20+
code: 404,
21+
message: "Installation does not exist.",
22+
});
23+
} else if (e instanceof OrgNotFoundError) {
24+
return json(404, res, {
25+
code: 404,
26+
message: "Organization does not exist.",
27+
});
3328
}
34-
3529
throw e;
3630
}
3731
return json(200, res, {});

0 commit comments

Comments
 (0)