Skip to content

Commit f537778

Browse files
authored
Separate classes from client code (#1119)
* fix: move route to route folder * fix: move service class to its own file
1 parent 974bcc4 commit f537778

File tree

20 files changed

+523
-508
lines changed

20 files changed

+523
-508
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { CreateExternalConnectionBody } from "@trigger.dev/core";
2+
import { PrismaClientOrTransaction, prisma } from "~/db.server";
3+
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
4+
import { integrationAuthRepository } from "~/services/externalApis/integrationAuthRepository.server";
5+
6+
export class CreateExternalConnectionService {
7+
#prismaClient: PrismaClientOrTransaction;
8+
9+
constructor(prismaClient: PrismaClientOrTransaction = prisma) {
10+
this.#prismaClient = prismaClient;
11+
}
12+
13+
public async call(
14+
accountIdentifier: string,
15+
clientSlug: string,
16+
environment: AuthenticatedEnvironment,
17+
payload: CreateExternalConnectionBody
18+
) {
19+
const externalAccount = await this.#prismaClient.externalAccount.upsert({
20+
where: {
21+
environmentId_identifier: {
22+
environmentId: environment.id,
23+
identifier: accountIdentifier,
24+
},
25+
},
26+
create: {
27+
environmentId: environment.id,
28+
organizationId: environment.organizationId,
29+
identifier: accountIdentifier,
30+
},
31+
update: {},
32+
});
33+
34+
const integration = await this.#prismaClient.integration.findUniqueOrThrow({
35+
where: {
36+
organizationId_slug: {
37+
organizationId: environment.organizationId,
38+
slug: clientSlug,
39+
},
40+
},
41+
});
42+
43+
return await integrationAuthRepository.createConnectionFromToken({
44+
externalAccount: externalAccount,
45+
integration,
46+
token: payload,
47+
});
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import type { ActionFunctionArgs } from "@remix-run/server-runtime";
22
import { json } from "@remix-run/server-runtime";
3-
import {
4-
CreateExternalConnectionBody,
5-
CreateExternalConnectionBodySchema,
6-
ErrorWithStackSchema,
7-
} from "@trigger.dev/core";
3+
import { CreateExternalConnectionBodySchema, ErrorWithStackSchema } from "@trigger.dev/core";
84
import { z } from "zod";
95
import { generateErrorMessage } from "zod-error";
10-
import { PrismaClientOrTransaction, prisma } from "~/db.server";
11-
import { AuthenticatedEnvironment, authenticateApiRequest } from "~/services/apiAuth.server";
12-
import { integrationAuthRepository } from "~/services/externalApis/integrationAuthRepository.server";
6+
import { authenticateApiRequest } from "~/services/apiAuth.server";
7+
import { CreateExternalConnectionService } from "./CreateExternalConnectionService.server";
138

149
const ParamsSchema = z.object({
1510
accountId: z.string(),
@@ -67,48 +62,3 @@ export async function action({ request, params }: ActionFunctionArgs) {
6762
return json({ message: parsedError.data.message }, { status: 500 });
6863
}
6964
}
70-
71-
class CreateExternalConnectionService {
72-
#prismaClient: PrismaClientOrTransaction;
73-
74-
constructor(prismaClient: PrismaClientOrTransaction = prisma) {
75-
this.#prismaClient = prismaClient;
76-
}
77-
78-
public async call(
79-
accountIdentifier: string,
80-
clientSlug: string,
81-
environment: AuthenticatedEnvironment,
82-
payload: CreateExternalConnectionBody
83-
) {
84-
const externalAccount = await this.#prismaClient.externalAccount.upsert({
85-
where: {
86-
environmentId_identifier: {
87-
environmentId: environment.id,
88-
identifier: accountIdentifier,
89-
},
90-
},
91-
create: {
92-
environmentId: environment.id,
93-
organizationId: environment.organizationId,
94-
identifier: accountIdentifier,
95-
},
96-
update: {},
97-
});
98-
99-
const integration = await this.#prismaClient.integration.findUniqueOrThrow({
100-
where: {
101-
organizationId_slug: {
102-
organizationId: environment.organizationId,
103-
slug: clientSlug,
104-
},
105-
},
106-
});
107-
108-
return await integrationAuthRepository.createConnectionFromToken({
109-
externalAccount: externalAccount,
110-
integration,
111-
token: payload,
112-
});
113-
}
114-
}

apps/webapp/app/routes/api.v1.endpoints.$environmentId.$endpointSlug.index.$indexHookIdentifier.ts renamed to apps/webapp/app/routes/api.v1.endpoints.$environmentId.$endpointSlug.index.$indexHookIdentifier/TriggerEndpointIndexHookService.server.ts

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,10 @@
1-
import { ActionFunctionArgs, LoaderFunctionArgs, json } from "@remix-run/server-runtime";
21
import { z } from "zod";
32
import type { PrismaClient } from "~/db.server";
43
import { $transaction, prisma } from "~/db.server";
54
import { logger } from "~/services/logger.server";
65
import { workerQueue } from "~/services/worker.server";
7-
import { safeJsonParse } from "~/utils/json";
86
import { RuntimeEnvironmentType } from "~/database-types";
9-
10-
const ParamsSchema = z.object({
11-
environmentId: z.string(),
12-
endpointSlug: z.string(),
13-
indexHookIdentifier: z.string(),
14-
});
15-
16-
export async function loader({ params }: LoaderFunctionArgs) {
17-
const parsedParams = ParamsSchema.safeParse(params);
18-
19-
if (!parsedParams.success) {
20-
return {
21-
status: 400,
22-
json: {
23-
error: "Invalid params",
24-
},
25-
};
26-
}
27-
28-
const { environmentId, endpointSlug, indexHookIdentifier } = parsedParams.data;
29-
30-
const service = new TriggerEndpointIndexHookService();
31-
32-
await service.call({
33-
environmentId,
34-
endpointSlug,
35-
indexHookIdentifier,
36-
});
37-
38-
return json({
39-
ok: true,
40-
});
41-
}
42-
43-
export async function action({ request, params }: ActionFunctionArgs) {
44-
const parsedParams = ParamsSchema.safeParse(params);
45-
46-
if (!parsedParams.success) {
47-
return {
48-
status: 400,
49-
json: {
50-
error: "Invalid params",
51-
},
52-
};
53-
}
54-
55-
const { environmentId, endpointSlug, indexHookIdentifier } = parsedParams.data;
56-
57-
const body = await request.text();
58-
59-
const service = new TriggerEndpointIndexHookService();
60-
61-
await service.call({
62-
environmentId,
63-
endpointSlug,
64-
indexHookIdentifier,
65-
body: body ? safeJsonParse(body) : undefined,
66-
});
67-
68-
return json({
69-
ok: true,
70-
});
71-
}
7+
import type { ParamsSchema } from "./route";
728

739
type TriggerEndpointDeployHookOptions = z.infer<typeof ParamsSchema> & {
7410
body?: any;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { ActionFunctionArgs, LoaderFunctionArgs, json } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { safeJsonParse } from "~/utils/json";
4+
import { TriggerEndpointIndexHookService } from "./TriggerEndpointIndexHookService.server";
5+
6+
export const ParamsSchema = z.object({
7+
environmentId: z.string(),
8+
endpointSlug: z.string(),
9+
indexHookIdentifier: z.string(),
10+
});
11+
12+
export async function loader({ params }: LoaderFunctionArgs) {
13+
const parsedParams = ParamsSchema.safeParse(params);
14+
15+
if (!parsedParams.success) {
16+
return {
17+
status: 400,
18+
json: {
19+
error: "Invalid params",
20+
},
21+
};
22+
}
23+
24+
const { environmentId, endpointSlug, indexHookIdentifier } = parsedParams.data;
25+
26+
const service = new TriggerEndpointIndexHookService();
27+
28+
await service.call({
29+
environmentId,
30+
endpointSlug,
31+
indexHookIdentifier,
32+
});
33+
34+
return json({
35+
ok: true,
36+
});
37+
}
38+
39+
export async function action({ request, params }: ActionFunctionArgs) {
40+
const parsedParams = ParamsSchema.safeParse(params);
41+
42+
if (!parsedParams.success) {
43+
return {
44+
status: 400,
45+
json: {
46+
error: "Invalid params",
47+
},
48+
};
49+
}
50+
51+
const { environmentId, endpointSlug, indexHookIdentifier } = parsedParams.data;
52+
53+
const body = await request.text();
54+
55+
const service = new TriggerEndpointIndexHookService();
56+
57+
await service.call({
58+
environmentId,
59+
endpointSlug,
60+
indexHookIdentifier,
61+
body: body ? safeJsonParse(body) : undefined,
62+
});
63+
64+
return json({
65+
ok: true,
66+
});
67+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { LogMessage } from "@trigger.dev/core";
2+
import type { PrismaClient } from "@trigger.dev/database";
3+
import { prisma } from "~/db.server";
4+
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
5+
import { logger } from "~/services/logger.server";
6+
7+
export class CreateRunLogService {
8+
#prismaClient: PrismaClient;
9+
10+
constructor(prismaClient: PrismaClient = prisma) {
11+
this.#prismaClient = prismaClient;
12+
}
13+
14+
public async call(environment: AuthenticatedEnvironment, runId: string, logMessage: LogMessage) {
15+
// @ts-ignore
16+
logger.debug(logMessage.message, logMessage.data ?? {});
17+
18+
return logMessage;
19+
}
20+
}

apps/webapp/app/routes/api.v1.runs.$runId.logs.ts renamed to apps/webapp/app/routes/api.v1.runs.$runId.logs/route.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import type { Organization, RuntimeEnvironment } from "@trigger.dev/database";
21
import type { ActionFunctionArgs } from "@remix-run/server-runtime";
32
import { json } from "@remix-run/server-runtime";
4-
import type { LogMessage } from "@trigger.dev/core";
53
import { LogMessageSchema } from "@trigger.dev/core";
64
import { z } from "zod";
7-
import type { PrismaClient } from "~/db.server";
8-
import { prisma } from "~/db.server";
9-
import { authenticateApiRequest, AuthenticatedEnvironment } from "~/services/apiAuth.server";
10-
import { logger } from "~/services/logger.server";
5+
import { authenticateApiRequest } from "~/services/apiAuth.server";
6+
import { CreateRunLogService } from "./CreateRunLogService.server";
117

128
const ParamsSchema = z.object({
139
runId: z.string(),
@@ -53,18 +49,3 @@ export async function action({ request, params }: ActionFunctionArgs) {
5349
return json({ error: "Something went wrong" }, { status: 500 });
5450
}
5551
}
56-
57-
export class CreateRunLogService {
58-
#prismaClient: PrismaClient;
59-
60-
constructor(prismaClient: PrismaClient = prisma) {
61-
this.#prismaClient = prismaClient;
62-
}
63-
64-
public async call(environment: AuthenticatedEnvironment, runId: string, logMessage: LogMessage) {
65-
// @ts-ignore
66-
logger.debug(logMessage.message, logMessage.data ?? {});
67-
68-
return logMessage;
69-
}
70-
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
StatusUpdate,
3+
StatusHistory,
4+
StatusHistorySchema,
5+
StatusUpdateState,
6+
StatusUpdateData,
7+
} from "@trigger.dev/core";
8+
import { PrismaClient } from "@trigger.dev/database";
9+
import { prisma, $transaction } from "~/db.server";
10+
11+
export class SetStatusService {
12+
#prismaClient: PrismaClient;
13+
14+
constructor(prismaClient: PrismaClient = prisma) {
15+
this.#prismaClient = prismaClient;
16+
}
17+
18+
public async call(runId: string, id: string, status: StatusUpdate) {
19+
const statusRecord = await $transaction(this.#prismaClient, async (tx) => {
20+
const existingStatus = await tx.jobRunStatusRecord.findUnique({
21+
where: {
22+
runId_key: {
23+
runId,
24+
key: id,
25+
},
26+
},
27+
});
28+
29+
const history: StatusHistory = [];
30+
const historyResult = StatusHistorySchema.safeParse(existingStatus?.history);
31+
if (historyResult.success) {
32+
history.push(...historyResult.data);
33+
}
34+
if (existingStatus) {
35+
history.push({
36+
label: existingStatus.label,
37+
state: (existingStatus.state ?? undefined) as StatusUpdateState,
38+
data: (existingStatus.data ?? undefined) as StatusUpdateData,
39+
});
40+
}
41+
42+
const updatedStatus = await tx.jobRunStatusRecord.upsert({
43+
where: {
44+
runId_key: {
45+
runId,
46+
key: id,
47+
},
48+
},
49+
create: {
50+
key: id,
51+
runId,
52+
//this shouldn't ever use the id in reality, as the SDK makess it compulsory on the first call
53+
label: status.label ?? id,
54+
state: status.state,
55+
data: status.data as any,
56+
history: [],
57+
},
58+
update: {
59+
label: status.label,
60+
state: status.state,
61+
data: status.data as any,
62+
history: history as any[],
63+
},
64+
});
65+
66+
return updatedStatus;
67+
});
68+
69+
return statusRecord;
70+
}
71+
}

0 commit comments

Comments
 (0)